Search This Blog

Friday, December 19, 2014

Web Event to perform operations after creating the web (Event Receiver Concept)



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:

-
  1. SiteDeleting: Fires before site collection is deleted (synchronous)
  2. SiteDeleted:  Fires after the site collection is deleted (asynchronous)
  3. WebDeleting: Fires before the site is deleted (synchronous)
  4. WebDeleted: Fires after the site is deleted (asynchronous)
  5. WebMoving: Fires before the site has been renamed or moved (synchronous)
  6. WebMoved: Fires after the site has been moved to another collection (asynchronous)
  7. WebAdding: Fires before the site has been created (synchronous)
  8. WebProvisioned: Fires after the site has been created and is fully provisioned (asynchronous)
Requirement: doing an operation (Example: copying list data from root site to sub site) after creating the sub site under a site collection

Prerequisites
  1. You should have a site template with required two lists, source list should contain the data; otherwise it will not copy any data
  2. After completing the Event Receiver deployment, you need to create the site with the required template
To achieve this I implemented WebProvisioned Event. I followed the below steps to implement WebProvisioned Event:
  • 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