Hi all,
If you want to apply a theme to a site programmatically then the code is pretty straight forward. Have a look at it:
using (SPSite parentSite = new SPSite(SPContext.Current.Site.Url))
{
//Allow the unsafe updates in site from the current user
parentSite.AllowUnsafeUpdates = true;
using (SPWeb currentWeb = parentSite.AllWebs[SPContext.Current.Web.Name])
{
using (SPWeb childWeb = currentWeb.Webs[name])
{
childWeb.AllowUnsafeUpdates = true;
//The code line shown below will apply the theme of the parent site to the child site
childWeb.ApplyTheme(currentWeb.Theme);
childWeb.Update();
childWeb.AllowUnsafeUpdates = false;
}
}
parentSite.AllowUnsafeUpdates = false;
}
Although this code works fine when you use it for applying theme to a site which already exists in the site collection, but this may not work (atleast in my case)when i tried applying a theme to a site which i am creating programmatically.
So if you are also facing same problem then instead of applying theme just after creating the site, move the code outside the create method and take reference of the newly created site and then call the above code. It will work !
In case you are still not able to understand it then write to us at sharepointkings@gmail.com or write a comment to this post.
Read More…
If you want to apply a theme to a site programmatically then the code is pretty straight forward. Have a look at it:
using (SPSite parentSite = new SPSite(SPContext.Current.Site.Url))
{
//Allow the unsafe updates in site from the current user
parentSite.AllowUnsafeUpdates = true;
using (SPWeb currentWeb = parentSite.AllWebs[SPContext.Current.Web.Name])
{
using (SPWeb childWeb = currentWeb.Webs[name])
{
childWeb.AllowUnsafeUpdates = true;
//The code line shown below will apply the theme of the parent site to the child site
childWeb.ApplyTheme(currentWeb.Theme);
childWeb.Update();
childWeb.AllowUnsafeUpdates = false;
}
}
parentSite.AllowUnsafeUpdates = false;
}
Although this code works fine when you use it for applying theme to a site which already exists in the site collection, but this may not work (atleast in my case)when i tried applying a theme to a site which i am creating programmatically.
So if you are also facing same problem then instead of applying theme just after creating the site, move the code outside the create method and take reference of the newly created site and then call the above code. It will work !
In case you are still not able to understand it then write to us at sharepointkings@gmail.com or write a comment to this post.