Apply a new Theme to a SharePoint site using Client Object Model C#

In my previous post, https://realmpksharepoint.wordpress.com/2014/02/13/get-the-current-theme-properties-of-a-sharepoint-website-using-client-object-model-c/, I had described how one can get all the available themes for a SharePoint site using CSOM. Now, here, I am going to demonstrate how to Change/Modify/Apply a new Theme to a SharePoint site.Actually, applying a theme is relatively easier. There is one method in the Web class of the Microsoft.SharePoint.Client namespace which will set the theme of the site. You just have to call this method with proper parameters.

private void ApplyTheme(string colorUrl, string fontUrl, string backImageUrl)
{
  if (!String.IsNullOrEmpty(colorUrl))
  {
    this.ctx.Web.ApplyTheme(colorUrl, fontUrl, backImageUrl, false);
    this.ctx.ExecuteQuery();
  }
}

Here, few things are noteworthy:

  • First of all I am not sure about the parameter shareGenerated [I have passed false to it]. You should test it in your application to get the desired result.
  • Out of the parameters, colorUrl, fontUrl, backImageUrl, colorUrl is mandatory. You can’t set it Empty or null. If your color is null then don’t apply the theme, it’s as good as setting the default theme.
  • The other two optional parameters, fontUrl, & backImageUrl also cannot be set as String.Empty. If you do this, then, you might encounter an error Invalid Url. Hence, if you don’t want to set any, or, both of this parameters then set them as NULL.

Get the current Theme Properties of a SharePoint Website using Client Object Model C#

Well, I guess you might have discovered as of now that, there is no direct property/field in the Web class which can give you all the information about the theme of your site. SO to get the theme info, you need to know what are the components that a SharePoint site’s theme consist of.

  • MasterPage
  • CustomMasterPage
  • Theme
  • Background-Image
  • Font
So to know the site current theme using CSOM you need to get the List, Composed Looks, for the current site. Composed Looks will have various items, each of them are a different theme that can be applied to the current site. One of the items it has is, Current. It is this ListItem, which contains the current site theme info. You need to query this item to get the current theme details of a site.
ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord); 

SP.List list = ctx.Web.Lists.GetByTitle("Composed Looks");
ctx.Load(list);
CamlQuery cQuery = new CamlQuery();
ListItemCollection ltItemCollection = list.GetItems(cQuery);

//Load only the required fields
ctx.Load(ltItemCollection, w => w.Include(
p => p.DisplayName, 
p => p.FieldValuesAsText["FontSchemeUrl"], 
p => p.FieldValuesAsText["ImageUrl"], 
p => p.FieldValuesAsText["ThemeUrl"]));

ctx.Load(ctx.Web, w => w.MasterUrl, w => w.CustomMasterUrl);

ctx.ExecuteQuery();
ListItem ltItemTemp = ltItemCollection.FirstOrDefault(p => p.DisplayName == "Current");

So, finally, you’ll have the following:-

MasterPage ctx.Web.MasterUrl
CustomMasterPage ctx.Web.CustomMasterUrl
Theme ltItemTemp.FieldValuesAsText[“ThemeUrl”]
Background-Image ltItemTemp.FieldValuesAsText[“ImageUrl”]
Font ltItemTemp.FieldValuesAsText[“FontSchemeUrl”]

To apply a new theme, check my next blog at, https://realmpksharepoint.wordpress.com/2014/02/24/apply-a-new-theme-to-a-sharepoint-site-using-client-object-model-c/