How to apply theme programatically ?

Jan 7, 2009 at 6:41 AM
Hi,

i am a newbie in sharepoint.
I wanna change the theme of the sharepoint site progrmatically based on the user's profile.
just like the theme of the GMAIL/ORKUT

from the help file i could see a piece of code....
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
web.ApplyTheme("Contoso");
}

where should i write this code...

i did somethign like this, here below i am providign the code.... here i am gettign warnings on the places where i made Italics,Underlined.
and after deploying the web part wen i tried to put the same in the page, i am getting an error "Cannot import the web part".
please help me with the necessary details so that i can work with this...


using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

namespace Theme

{

[Guid("7d9a708e-3086-4b85-b4e9-001d12b96464")]

public class ChangeTheme : SPFeatureReceiver

{

public ChangeTheme()

{

}

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

SPWeb web = properties.Feature.Parent as SPWeb;

web.ApplyTheme("PETAL");

}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

throw new NotImplementedException();

}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)

{

throw new NotImplementedException();

}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)

{

throw new NotImplementedException();

}

}

}

Coordinator
Jan 8, 2009 at 12:15 AM

The first thing to do is remove the "throw new NotImplementedException();" from your feature receiver methods. Next you could set the theme back to a standard theme in the FeatureDeactivating method (optional).

I don't know why you are having problems with web parts, but let's separate these issues. You could simply start with an empty solution, add a feature, and add your feature receiver that applies the theme.

Jan 8, 2009 at 5:52 AM
Thanks a lot for your response.

here below i am putting my code.

 protected override void OnPreRender(EventArgs e)
        {
            using (SPSite oSPSite = SPContext.Current.Site)
            {
                string theme = GetThemeBasedOnUser();
                SPWebCollection webColl = oSPSite.RootWeb.Webs;

                foreach (SPWeb web in webColl)
                {

                    web.AllowUnsafeUpdates = true;
                    web.ApplyTheme(theme);
                    web.Update();
                }
                SPWeb rootWeb = oSPSite.RootWeb;
                rootWeb.AllowUnsafeUpdates = true;
                rootWeb.ApplyTheme(theme);
                rootWeb.Update();

            }
            base.OnPreRender(e);
        }

using this piece of code i am not getting any error.
I have one main site which has 2 sub sites..
the issue is that i am able to change the theme by  refreshing the page. for the first time its like you know i am getting the default theme, then on clikc of the home menu theme of the site get changed.

Can you please tell me where i went wrong..

Coordinator
Jan 8, 2009 at 8:21 PM
Let me raise up a level.
Why are you trying to update the site's theme using a web part?
Is that really what you want to achieve? I'd rather put that code in a feature receiver.
Jan 9, 2009 at 4:17 AM
Edited Jan 9, 2009 at 4:30 AM
I am a newbie in this. I am trying all the scenario which works,,so came to web part also.
I dont know where to write the code for the feature receiver (put that code in a feature receiver).

In your previous post you have mentioned " You could simply start with an empty solution, add a feature, and add your feature receiver that applies the theme.", but really i dont know how to do this......
do you have any piece of code which demonstrates this....
Developer
Feb 6, 2009 at 6:43 AM
You can create an empty solution, add a feature with the feature receiver, and apply your them following the instructions in my blog:
http://blogs.msdn.com/hanz/archive/2009/02/06/how-to-create-a-feature-receiver-that-update-a-theme.aspx

Let me know if you have any issues.