Monday, April 23, 2012

Implement SharePoint 2010 Site event receivers


In SharePoint 2010 we now have a new type of receiver which is related to site. We now can track when a web is adding, provisioned, moving, moved, deleting and deleted.

All receivers that end with ing are synchronous and those which ends with ed are asynchronous.

So let us go ahead and explore some of the receiver types and try to understand how it works.

Open up visual studio 2010 and select SharePoint 2010 and select event receivers in the template type.



Connect with the site and select farm solution.



Select Web receivers from the list. Let us select these four options and explore them. Rest you can check on your own and let us know if you face any issue.



You then will be presented with four receivers.



Go ahead and add following code in each of them. Now build and deploy the solution. And now try creating site, and then delete the site.

public override void WebDeleting(SPWebEventProperties properties)
       {
           //This code actually triggers for sub sites that is being deleted. Hence we have taken reference of parent web to write the entry.

           SPList lstTracking = properties.Web.ParentWeb.Lists["Track Different Events"];

           properties.Web.ParentWeb.AllowUnsafeUpdates = true;

           SPListItem item = lstTracking.Items.Add();

           item["Title"] = "Deleting the Web";
          
           item.Update();

           properties.Web.ParentWeb.AllowUnsafeUpdates = false;
         
       }

       ///

       /// A site is being provisioned.
       ///

       public override void WebAdding(SPWebEventProperties properties)          
       {
          
           //This handler will trigger at the time of web being added as a sub site. Track Different Events list is in the parent site.

           SPList lstTracking = properties.Web.Lists["Track Different Events"];

           properties.Web.AllowUnsafeUpdates = true;

           SPListItem item = lstTracking.Items.Add();

           item["Title"] = "Adding the Web";

           item.Update();

           properties.Web.AllowUnsafeUpdates = false;

          
       }

       ///

       /// A site was deleted.
       ///

       public override void WebDeleted(SPWebEventProperties properties)
       {
           //This code actually triggers for sub sites that is deleted. Hence we have taken reference of parent web to write the entry by
           //instantiating SPSite. properties.Web returns null as web has been deleted by this time.

           SPSite site = new SPSite("");

           SPWeb web = site.AllWebs["ECMA"];

           SPList lstTracking = web.Lists["Track Different Events"];

           web.AllowUnsafeUpdates = true;

           SPListItem item = lstTracking.Items.Add();

           item["Title"] = "Web has been deleted";

           item.Update();

           web.AllowUnsafeUpdates = false;
       }

       ///

       /// A site was provisioned.
       ///

       public override void WebProvisioned(SPWebEventProperties properties)
       {
           //This code actually triggers for sub sites that is created. Hence we have taken reference of parent web to write the entry.          

           SPList lstTracking = properties.Web.ParentWeb.Lists["Track Different Events"];

           properties.Web.ParentWeb.AllowUnsafeUpdates = true;

           SPListItem item = lstTracking.Items.Add();

           item["Title"] = "Web has been provisioned";

           item.Update();

           properties.Web.ParentWeb.AllowUnsafeUpdates = false;
       }

We’ve created a site. So now we can see two entries in the list.




Now delete the sub site and we get this.






2 comments:

Sudheer said...

Hi,
WebDeleting,WebDeleted didnt get fired.That is why in Track Different Events list of parent web,site deleted info is not getting updated.Please let me know if i am making any mistake

SharePoint Kings said...

@Sudheer, its works for us not sure where you are going wrong.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com