Skip to main content
Tweeted twitter.com/StackSharePoint/status/807646167018467328
added 5604 characters in body
Source Link
John John
  • 1
  • 49
  • 258
  • 603

EDIT

here is my full event receiver code, which mainly will fire when an item is added to a list. where the event receiver will create a sub-site :-

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

    namespace CreateSubSiteforProjects.EventReceiver1
    {
        /// <summary>
        /// List Item Events
        /// </summary>
        public class EventReceiver1 : SPItemEventReceiver
        {
            /// <summary>
            /// An item is being added.
            /// </summary>
    
            public override void ItemAdded(SPItemEventProperties properties)
            {
                   base.ItemAdded(properties);
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite(properties.SiteId))
            {
                string currentweburl = properties.WebUrl;
               
                using (SPWeb spCurrentSite = site.OpenWeb(currenweburl))
                {
    
                    // Get the web where the event was raised
    
                    //SPWeb  = new ;//properties.OpenWeb();
    
                   
    
                    //Get the name of the list where the event was raised         
    
                    String curListName = properties.ListTitle;
    
    
    
                    //If the list is our list named SubSites the create a new subsite directly below the current site
    
                    if (curListName.ToLower() == "projects123")
                    {
    
                        //Get the SPListItem object that raised the event
    
                        SPListItem curItem = properties.ListItem;
    
                        //Get the Title field from this item. This will be the name of our new subsite
    
                        String curItemSiteName = properties.AfterProperties["Title"].ToString();
    
                        //Get the Description field from this item. This will be the description for our new subsite
    
                        //string curItemDescription = properties.AfterProperties["Description"].ToString();
    
                        //Update the SiteUrl field of the item, this is the URL of our new subsite
    
                        //properties.AfterProperties["SiteUrl"] = spCurrentSite.Url + "/" + curItemSiteName;
    
    
    
                        // Get the rootweb where we will find the available templates
    
                        SPWeb rootWeb = spCurrentSite.Site.RootWeb;
    
                        //Get all the webtemplates available on this site collection
    
                        SPWebTemplateCollection webTemplates = rootWeb.GetAvailableWebTemplates(1033);
    
                        SPWebTemplate webTemplate = null;
    
                        String webTemplateName = "NewProject"; //this is the name of the template we will use
    
                        String webTemplateSearchName = "";
    
    
                        //Get the template based on name. In this example 'ProjectTemplate'
    
                        //Loop through the available templates
                        for (int i = 0; i < webTemplates.Count; i++)
                        {
    
                            webTemplateSearchName = webTemplates[i].Name.ToString();
    
                            //Compare to see if this is the 'ProjectTemplate' template
                            if (webTemplateSearchName.Contains(webTemplateName))
                            {
                                //when we have found the matching template,set webTemplate to the foundt template and exit the if loop
                                webTemplate = webTemplates[webTemplateSearchName];
    
                                break;
    
                            }
    
                        }
    
                        //Create the subsite based on the template from the Solution Gallery
                       
                       SPWeb newSite = spCurrentSite.Webs.Add(curItemSiteName, curItemSiteName, "123", Convert.ToUInt16(1033), webTemplate, false, false);
                        
                        //Set the new subsite to inherit it's top navigation from the parent site, Use false if you do not want this.
    
                        newSite.Navigation.UseShared = true;
                        string masterPage = "TDMCustom.master";
                        var masterUri = new Uri(rootWeb.Url + "/_catalogs/masterpage/" + masterPage);
    
                        newSite.MasterUrl = masterUri.AbsolutePath;
    
                        newSite.CustomMasterUrl = masterUri.AbsolutePath;
                        newSite.Update();
                        newSite.Close();
    
    
    
    
    
                    }
                }
            }
        });
    
            }
    
           
        }
    }

EDIT

here is my full event receiver code, which mainly will fire when an item is added to a list. where the event receiver will create a sub-site :-

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

    namespace CreateSubSiteforProjects.EventReceiver1
    {
        /// <summary>
        /// List Item Events
        /// </summary>
        public class EventReceiver1 : SPItemEventReceiver
        {
            /// <summary>
            /// An item is being added.
            /// </summary>
    
            public override void ItemAdded(SPItemEventProperties properties)
            {
                   base.ItemAdded(properties);
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite(properties.SiteId))
            {
                string currentweburl = properties.WebUrl;
               
                using (SPWeb spCurrentSite = site.OpenWeb(currenweburl))
                {
    
                    // Get the web where the event was raised
    
                    //SPWeb  = new ;//properties.OpenWeb();
    
                   
    
                    //Get the name of the list where the event was raised         
    
                    String curListName = properties.ListTitle;
    
    
    
                    //If the list is our list named SubSites the create a new subsite directly below the current site
    
                    if (curListName.ToLower() == "projects123")
                    {
    
                        //Get the SPListItem object that raised the event
    
                        SPListItem curItem = properties.ListItem;
    
                        //Get the Title field from this item. This will be the name of our new subsite
    
                        String curItemSiteName = properties.AfterProperties["Title"].ToString();
    
                        //Get the Description field from this item. This will be the description for our new subsite
    
                        //string curItemDescription = properties.AfterProperties["Description"].ToString();
    
                        //Update the SiteUrl field of the item, this is the URL of our new subsite
    
                        //properties.AfterProperties["SiteUrl"] = spCurrentSite.Url + "/" + curItemSiteName;
    
    
    
                        // Get the rootweb where we will find the available templates
    
                        SPWeb rootWeb = spCurrentSite.Site.RootWeb;
    
                        //Get all the webtemplates available on this site collection
    
                        SPWebTemplateCollection webTemplates = rootWeb.GetAvailableWebTemplates(1033);
    
                        SPWebTemplate webTemplate = null;
    
                        String webTemplateName = "NewProject"; //this is the name of the template we will use
    
                        String webTemplateSearchName = "";
    
    
                        //Get the template based on name. In this example 'ProjectTemplate'
    
                        //Loop through the available templates
                        for (int i = 0; i < webTemplates.Count; i++)
                        {
    
                            webTemplateSearchName = webTemplates[i].Name.ToString();
    
                            //Compare to see if this is the 'ProjectTemplate' template
                            if (webTemplateSearchName.Contains(webTemplateName))
                            {
                                //when we have found the matching template,set webTemplate to the foundt template and exit the if loop
                                webTemplate = webTemplates[webTemplateSearchName];
    
                                break;
    
                            }
    
                        }
    
                        //Create the subsite based on the template from the Solution Gallery
                       
                       SPWeb newSite = spCurrentSite.Webs.Add(curItemSiteName, curItemSiteName, "123", Convert.ToUInt16(1033), webTemplate, false, false);
                        
                        //Set the new subsite to inherit it's top navigation from the parent site, Use false if you do not want this.
    
                        newSite.Navigation.UseShared = true;
                        string masterPage = "TDMCustom.master";
                        var masterUri = new Uri(rootWeb.Url + "/_catalogs/masterpage/" + masterPage);
    
                        newSite.MasterUrl = masterUri.AbsolutePath;
    
                        newSite.CustomMasterUrl = masterUri.AbsolutePath;
                        newSite.Update();
                        newSite.Close();
    
    
    
    
    
                    }
                }
            }
        });
    
            }
    
           
        }
    }
Source Link
John John
  • 1
  • 49
  • 258
  • 603

Differences between SPItemEventProperties.SiteId & SPItemEventProperties.Web.Site.ID

I am working on an Event Receiver inside my sharepoint 2013. the event receiver will get fired when an item is Added and it is scoped at the Web level. but i am not sure if there are any differences between using :-

public override void ItemAdded(SPItemEventProperties properties)
 {
    base.ItemAdded(properties);
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
using (SPSite site = new SPSite(properties.Web.Site.ID))
        {
            
            using (SPWeb spCurrentSite = site.OpenWeb(properties.Web.ID))
            {

Or using

public override void ItemAdded(SPItemEventProperties properties)
  {
        base.ItemAdded(properties);
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
using (SPSite site = new SPSite(properties.siteID))
        {
            
            string currenweburl = properties.RelativeWebUrl;
           
            using (SPWeb spCurrentSite = site.OpenWeb(currentweburl))
            {

where on the first method i am using new SPSite(properties.Web.Site.ID) & site.OpenWeb(properties.Web.ID). while on the second i am using new SPSite(properties.siteID) & site.OpenWeb(currentweburl) ?? or they are the same ?