SPWebEventReceiver class provides a way to register and trap general events for a Web site. SharePoint 2010 supports the following events at web or site level:
-
-
-
- SiteDeleting: Fires before site collection is deleted (synchronous)
- SiteDeleted: Fires after the site collection is deleted (asynchronous)
- WebDeleting: Fires before the site is deleted (synchronous)
- WebDeleted: Fires after the site is deleted (asynchronous)
- WebMoving: Fires before the site has been renamed or moved (synchronous)
- WebMoved: Fires after the site has been moved to another collection (asynchronous)
- WebAdding: Fires before the site has been created (synchronous)
- WebProvisioned: Fires after the site has been created and is fully provisioned (asynchronous)
Prerequisites:
- You should have a site template with required two lists, source list should contain the data; otherwise it will not copy any data
- After completing the Event Receiver deployment, you need to create the site with the required template
- Open Visual Studio 2010, Select “Empty SharePoint Project” and give a name to the project and click on “OK”
- Make sure framework should be “.Net Framework 3.5” and Platform target as “x64”
- In the “SharePoint Customization Wizard”, enter your site URL and select trust level as “Deploy as a form solution” then click on“Finish”
- In the solution explorer, Right click on Features and select Add feature
- Rename your features as you required and provide description to identify the feature easily
- Here we are performing operation after creating the site so scope should be “Web”
- Note: WebProvisioned Event will fire at Web level only
- We created feature successfully. Now our job is to create Event receiver
- To add Event Receiver, Right Click on the ProjectàAddàNew Item
- Select Event Receiver Template, give proper name and click on Add
- In the “SharePoint Customization Wizard”, Select “Web Events” and check the “A site was Provisioned” Check box then click on Finish
- It will show with default code in the .cs file
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace AttachEventTpWeb.ListFillEventReceiver
{
/// <summary>
/// Web Events
/// </summary>
public class ListFillEventReceiver : SPWebEventReceiver
{
/// <summary>
/// A site was provisioned.
/// </summary>
public override void WebProvisioned(SPWebEventProperties properties)
{
base.WebProvisioned(properties);
}
}
}
|
- Add your own code to do your required operation, in my case copying data from one list to other
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace AttachEventTpWeb.ListFillEventReceiver
{
/// <summary>
/// Web Events
/// </summary>
public class ListFillEventReceiver : SPWebEventReceiver
{
/// <summary>
/// A site was provisioned.
/// </summary>
public override void WebProvisioned(SPWebEventProperties properties)
{
base.WebProvisioned(properties);
CopyList(properties);
}
public void CopyList(SPWebEventProperties properties)
{
//write your logic to copy list details from one list to other
}
}
}
|
- Build the project
- Deploy the project
- Now feature will be added into the site collection
- Activate the feature
- Now create a site and check whether it copied the data from one list to other, it will copy the data successfully
No comments:
Post a Comment