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.