To use WebProvisioned event in SharePoint 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 sub site in the site collection get created. Here we will see an example that will change the Theme of the sub site when ever the sub site 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. 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. To do this we have create an event receiver and we have to write code in the WebProvisioned event, that will responsible 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 as shown in the figure below:

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 and the dialog should be as like below figure.

 

Now if you will look at the solution then it should be like the below figure:

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.
Then build the solution and then deploy the solution. It will automatically attach the event receiver. Else for deployment you can follow this :

Then try to create a site and see the site's theme after that.

Related Posts