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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.