How to use WebProvisioned event in SharePoint 2010?

In this SharePoint 2010 tutorial, we will how to use the webprovisioned event in SharePoint 2010 using Visual Studio 2010.

First of all, let us understand what the event is. This is event comes with SharePoint 2010, that allows us to execute something when a subsite in the site collection gets created. Here we will see an example that will change the Theme of the subsite whenever the subsite will get created.

A point to remember is this event does not fire when the root web site of a new site collection is created in SharePoint 2010. This method can be configured to operate in either synchronous or asynchronous modes. And this can be set in Synchronization tag in the Elements.xml file.

WebProvisioned event in SharePoint

To do this we have created an event receiver and we have to write code in the WebProvisioned event, that will respond to change the Theme of the newly created site.

First open Visual Studio 2010 -> FileĀ  -> New Project -> Then from the New Project dialog box go to SharePoint 2010 and choose Event Receiver from the list of Templates. Give the name and location and click on ok.

In the next step give the local deployment URL and click on deploy as a farm solution and click on Ok.

Then the SharePoint Customization Wizard will come and here it is important to choose the event receiver settings. Here we will choose the event receiver type as Web Events and then we can choose the events which all we want. Here we only needed the A site was provisioned event. And then click on Finish.

Visual Studio automatically added Feature as Feature1, Package and our event receiver class as EventReceiver1.

First of all double click on the Feature and change the scope, Here I am changing the scope as Site.
Then lets give a look at the Elements the XML file. Initially Visual studio automatically place the below tag to the elements.xml file:

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Receivers >
<Receiver>
<Name>EventReceiver1WebProvisioned</Name>
<Type>WebProvisioned</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>EventReceiverProject2.EventReceiver1.EventReceiver1</Class>
<SequenceNumber>10000</SequenceNumber>
</Receiver>
</Receivers>
</Elements>

My suggestion is do not modify the code. Here one thing to remember is that we can make the event receiver as synchronous or asynchronous. By default it is ssynchronous. If you will make this to Synchronous then any UI changes you will do, it will affect immeditely after the site is created. So lets make the event to Synchronous by adding in Synchronization tag, So our Elements.xml file should look like below now.

And also another point is we need to define the scope here with the Scope attribute of Receivers tag. So the full code will look like below.

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Receivers Scope=”Site”>
<Receiver>
<Name>EventReceiver1WebProvisioned</Name>
<Type>WebProvisioned</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>EventReceiverProject2.EventReceiver1.EventReceiver1</Class>
<SequenceNumber>10000</SequenceNumber>
<Synchronization>Synchronous</Synchronization>
</Receiver>
</Receivers>
</Elements>

Now lets discuss the EventReceiver1 class.

Visual studio automatically put the below code, with the override WebProvisioned method. The code is: using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace EventReceiverProject2.EventReceiver1
{ ///

/// Web Events
///

public class EventReceiver1 : SPWebEventReceiver
{
///

/// A site was provisioned.
///

public override void WebProvisioned(SPWebEventProperties properties)
{
base.WebProvisioned(properties);
}
}
}

In the override method we will define the code that will change the theme. So the full code will be

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace EventReceiverProject2.EventReceiver1
{
///

/// Web Events
///

public class EventReceiver1 : SPWebEventReceiver
{
///

/// A site was provisioned.
///

public override void WebProvisioned(SPWebEventProperties properties)
{
try
{
base.WebProvisioned(properties);
SPWeb web = properties.Web;
web.Navigation.UseShared = true;
string ThemeURL = “[Your Site URL]/_catalogs/theme/Mission.thmx”;
ThmxTheme.SetThemeUrlForWeb(web, ThemeURL);
web.Update();
}
catch (Exception ex)
{
throw;
}
}
}
}

Here you can check the theme url from the Theme galery. That is it.

You may like following SharePoint tutorials:

Then build the solution and then deploy the solution. It will automatically attach the event receiver. Else for deployment, you can follow Deploy web part to the production server using PowerShell in SharePoint. Then try to create a site and see the site’s theme after that.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>