Event Receiver for Sites in Sharepoint 2010

These are several events available for sites or webs, defined in the Microsoft.SharePoint.SPWebEventReceiver class.

The list of the events are :
WebDeleting: Fired before the site is deleted (synchronous)
WebDeleted: Fired after the site is deleted (asynchronous)
WebMoving: Fired before the site has been renamed or moved (synchronous)
WebMoved: Fired after the site has been moved to another collection(asynchronous)
WebAdding: Fired before the site has been created (synchronous)
WebProvisioned: Fired after the site has been created and is fully provisioned(asynchronous)

In this post however, I will create an event receiver that modifies the title of the created site after provisioning. The code precisely does the following :
* The code simply adds the current user’s full name (including the domain) to the site’s title. ( To show who created the site).
* In the web-deleting event it tries to prevent the action from being processed if a list named Data still exists. This is a common technique to prevent users from accidentally removing a site with crucial data.

Lets look at the code first

A Web Event Receiver class-

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace WebProvisioning.SiteEventReceiver
{
public class WebEventReceiver : SPWebEventReceiver
{
public override void WebDeleting(SPWebEventProperties properties)
{
try
{
if (properties.Web.Lists["Data"] != null)
{
properties.Cancel = true;
}
}
catch
{
}
base.WebDeleting(properties);
}
public override void WebAdding(SPWebEventProperties properties)
{
base.WebAdding(properties);
}
public override void WebProvisioned(SPWebEventProperties properties)
{
properties.Web.Title += String.Format(” [Created By: {0}]“,properties.UserDisplayName);
properties.Web.AllowUnsafeUpdates = true;
properties.Web.Update();
base.WebAdding(properties);
}
}
}

Defining the Event Receiver – Lets look at the elements.xml file

<elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<receivers>
<receiver>
<name>SiteEventReceiverWebDeleting</name>
<type>WebDeleting</type>
<assembly>$SharePoint.Project.AssemblyFullName$</assembly>
<class>WebProvisioning.SiteEventReceiver.SiteEventReceiver</class>
<sequencenumber>10000</sequencenumber>
</receiver>
<receiver>
<name>SiteEventReceiverWebAdding</name>
<type>WebAdding</type>
<assembly>$SharePoint.Project.AssemblyFullName$</assembly>
<class>WebProvisioning.SiteEventReceiver.SiteEventReceiver</class>
<sequencenumber>10000</sequencenumber>
</receiver>
<receiver>
<name>SiteEventReceiverWebProvisioned</name>
<type>WebProvisioned</type>
<assembly>$SharePoint.Project.AssemblyFullName$</assembly>
<class>WebProvisioning.SiteEventReceiver.SiteEventReceiver</class>
<sequencenumber>10000</sequencenumber>
<synchronization>Synchronous</synchronization>
</receiver>
</receivers>
</elements>

If there are several event handlers attached to the same event, you can use the sequence number element to define the order. If you create the file using Visual Studio 2010, the $SharePoint.Project.AssemblyFullName$ placeholder is used to reference the current project’s assembly. If you create the file manually, you must enter the fully qualified assembly name in the <assembly> element.

Ads by google