I struggled with the title of this post, because there are several different uses for the Event Receiver I’m going to talk about. In SharePoint 2007, it was common to use a Feature Receiver that was stapled to a Site/Site Definition for performing additional tasks after a Web was provisioned. We leveraged a Feature Receiver, because there didn’t exist an Event Receiver that handled the “Web Added” event. This is where the Web Provisioned event comes into play for SharePoint 2010 development. And a huge thanks to whoever was on the MS team that made that happen.
So let me setup a scenario I was working on recently and discuss the options for how that might be achieved.
Requirement: There exists a team site called Projects. Any sub sites created under Projects need to automatically inherit it’s parent site theme and navigation. We can get a little more complicated by doing things like adding web parts to the default.aspx page automatically or automatically adding an entry to the parent quick launch bar that includes a link to newly provisioned sites, but I’ll cover that in another post.
- Within Visual Studio, you want to create a new Project. On the left, expand the SharePoint heading and select Event Receiver on the right. Click OK to create the new project.
- The next screen will ask for the debugging url and whether or not you want this to be a sandboxed solution. Fill those in as required.
- The next screen is where we get to define what type of receiver we’re looking for. In this case, we’re going to use one of the NEW SharePoint 2010 receivers. In the drop down asking What type of event receiver we want, select Web Events. Now check the box next to A site was provisioned. Click Finish.
- Visual Studio will take care of all the setup for the Event Receiver, and once loaded, it should default you to the EventReceiver1.cs file.
- Now let’s add the code that will be responsible for changing the navigation of the new site to inherit from it’s parent. And let’s also make the change so the sub site is themed like the parent.
- Go ahead and build the solution. Right click on the project in the Solution Explorer and click Deploy.
- Once the solution is deployed, open up your browser and navigate to your site url. Notice from the screenshot below, I have a Projects site with the Azure theme applied.
- An important piece to understand is that the Web Provisioned event needs to be activated at the Projects level, because we want the code we wrote to run on any sub sites created under Projects. So go ahead and navigate to Site Actions, Site Settings, Manage site features and activate our new feature.
- Go ahead and create a new sub site. You might notice that it appears as though nothing happened. For example, why does it appear as though the theme was not set? Well, if you hit refresh, you’ll see that the theme was in fact set. There is a property in the Event Receiver’s Elements.xml file that will fix this, so it doesn’t require a refresh. Take a look at the before and after for the xml listed below. I’ve added a line that sets the Synchronization property. The default is asynchronous, however, we want it set to synchronous. Asynchronous means that events typically run at the same time, so the custom code is run, but instead of waiting for it to finish, the page loads and we might not have finished setting it’s properties yet. By specifying that we run it synchronously, it will wait for the code to complete before attempting to load the page.
- Re-Deploy the solution and create another sub site. Test that it properly sets the values when the page loads. If it doesn’t work immediately, deactivate and reactivate the feature. I find I often have to do that after deploying a new version of a solution.
public class EventReceiver1 : SPWebEventReceiver { /// /// A site was provisioned. /// public override void WebProvisioned(SPWebEventProperties properties) { base.WebProvisioned(properties); } }
public override void WebProvisioned(SPWebEventProperties properties) { base.WebProvisioned(properties); SPWeb web = properties.Web; // set navigation to parent nav web.Navigation.UseShared = true; // set theme to parent theme ThmxTheme.SetThemeUrlForWeb(web, ThmxTheme.GetThemeUrlForWeb(web.ParentWeb)); web.Update(); }
Before
<?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>WebAddedEvent.EventReceiver1.EventReceiver1</Class> <SequenceNumber>10000</SequenceNumber> </Receiver> </Receivers> </Elements>
After
<?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>WebAddedEvent.EventReceiver1.EventReceiver1</Class> <SequenceNumber>10000</SequenceNumber> <Synchronization>Synchronous</Synchronization> </Receiver> </Receivers> </Elements>
That’s it folks, a really simple overview of the new Web Provisioned event receiver. Please post a comment if anything is unclear.



