Monday 6 January 2014

Event Handlers to create subsites when items adding into Lists: Sharepoint(2007/2010)

These are the simple steps to create an event handler to create subsites when we add items into a lists.

For this first we need to create a teamsite in sharepoint. For that
Click the site action link on the right top side of your sharepoint site and select create then select Sites and Work spaces under Web pages pane.
 Then save this site as template by clicking Save site as template on Look and feel pane.
the we are re directing to a new page. On this page write the template name and file name

Then add a custom list on your share point site and set the Columns Title,Description,SiteUrl
Set the SiteUrl column type as HyperLink.

Create The Event Handler

     Next step is to create the Event Handler. For this open your Visual Studio 2010
Select File>New>Project
On Project types Select WSP builder and then select WSP Builder project template
Type the project Name as ListEvents. 
Then  right click the ListEvents Project and add new Item then select Event Handler Templates from WSPBuilder and write the name.
Here I put MyEventHandler as name.
Select MyEventHandler.cs file and put these codes in ItemAdding event

public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
            SPWeb spCurrentSite = properties.OpenWeb();
            string curListName = properties.ListTitle;
            if (curListName == "SubSites")//My list Name
            {
                SPListItem curItem = properties.ListItem;
                string curItemSiteName = properties.AfterProperties["Title"].ToString(); //Column name in List
//Column name in List
                string curItemDescription = properties.AfterProperties["Description"].ToString();

//Column name in List
                properties.AfterProperties["SiteUrl"] = spCurrentSite.Url + "/" + curItemSiteName;
              
                SPWeb rootWeb = spCurrentSite.Site.RootWeb;
                SPWebTemplateCollection webTemplates = rootWeb.GetAvailableWebTemplates(1033);
               
                string webTemplateName = "MyTemplate";//Template name
                string webTemplateSearchName = "";
//List all the templates and select our template
                for (int i = 0; i < webTemplates.Count; i++)
                {
                    webTemplateSearchName = webTemplates[i].Name.ToString();
                    if (webTemplateSearchName.Contains(webTemplateName))
                    {
                        webTemplate = webTemplates[webTemplateSearchName];
                        break;
                    }
                }

                SPWeb newSite = spCurrentSite.Webs.Add(curItemSiteName, curItemSiteName, curItemDescription, Convert.ToUInt16(1033), webTemplate, false, false);
                newSite.Navigation.UseShared = true;
                newSite.Close();

            }

Save this code.
Then on elements.XML file add this code
  <Receiver>
      <Name>AddingEventHandler</Name>
      <Type>ItemAdding</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>ListEvents, Version=1.0.0.0, Culture=neutral, PublicKeyToken=17cc11051bd641bb</Assembly>
      <Class>ListEvents.MyEventHandler</Class>
      <Data></Data>
      <Filter></Filter>
    </Receiver>
  </Receivers>

Or you can add this event on sharepoint site using
Eventhandler administration.exe.



Right click the ListEvents project Build WSP on WSPBuilder.
Then Deploy the project.
Then in your site SiteActions>SiteAdministration>SiteFeatures
Active your MyEventHandler


Then add items into your List the event will work.. :)