Pages

Monday, October 10, 2011

Sharepoint Web Events and Branding

Sharepoint Branding is one of the important thing in Sharepoint development. Often times there is a requirement to create branding features and feature stapler to achieve required branding. I think feature stapler is definetly a great idea, but in sharepoint 2010 there a new concept that is introduced in solution deployment. In sharepoint 2007 we did not had any sandbox solution. In a nut shell Sharepoint Sandbox solution is a way to deploy solution that can be used only in site collection that is deployed. These sandbox solution doesnot have access to file system. Here comes the problem because it cannot access the file system feature stapler cannot be used to add new features. Another change or improvement in sharepoint 2010 is web templates. Web templates replaces site templates in sharpoint 2007. These web templates are again deployed as Sandbox solution.hence feature stapler cannot be used to attach any branding as most often web templates are used for creating sites with similar look and feel and branding.

After researching quite a bit I felt that there is less documentation on how to achieve branding or change certain functionality in sandbox solution using feature stapler. Having used feature stapler heavily in MOSS 2007 I felt hard to get away with it. I did experiment with some of the events and see other ways to achieve this. I found the Web Events that are introduced in Sharepoint 2010 is a better way to achieve this. In sharepoint 2010 we can use "Site was provisioned" which is an Asynchronous event to achieve some of the branding stuff. I recently created this to set master pages for the sites created using Sandbox Web templates.

Code 

public class SetMasterPageEvent : SPWebEventReceiver
{
       /// <summary>
       /// A site was provisioned.
       /// </summary>
       public override void WebProvisioned(SPWebEventProperties properties)
       {
           base.WebProvisioned(properties);

           using (SPSite site = new SPSite(properties.FullUrl))
           {
               using (SPWeb web = site.OpenWeb())
               {
                   web.MasterUrl = web.Site.RootWeb.MasterUrl;
                   web.CustomMasterUrl = web.Site.RootWeb.CustomMasterUrl;
                   web.AlternateCssUrl = web.Site.RootWeb.AlternateCssUrl;
                   web.SiteLogoUrl = web.Site.RootWeb.SiteLogoUrl;
                   web.Update();
               }
           }
       }


    }


In the example I am setting the master page of the newly created site to the master page of root site. You can register this event by creating a site collection feature if you want the operation to be performed on all the sites that are created under the site collection or a web feature if an operation needs to performed on a specific site.


No comments:

Post a Comment