none
Build SubSites Programmically

    Question

  • Hi everyone!

    I have tons of sub-sites to build and I thought it would be nice to create a simple form that allows me to just enter the name of a site and have it build my sub-sites based on a template I made.  It seems I was not alone and I found many tutorials that did not work for me.  The steps I took first were building a site, creating a template from that site with a custom list, and building a custom form in SharePoint Designer.  My form has three fields with one containing SiteUrl, Description, and Title. Lastly, I created a Visual Studio 2010 project using the .net 3.5 framework.  I chose event and custom list from my options.  Of course my code below does not recognize my forms fields.  I am lost at this point.  Can someone assist?

    My resources:

    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/7e967b7e-2f34-493f-8d70-ba8893a4985a/is-it-possible-to-programmatically-create-new-subsites-based-on-a-defenition

    http://msdn.microsoft.com/en-us/library/ms441339.aspx

    My Code:

           public override void ItemAdding(SPItemEventProperties properties)
           {
               base.ItemAdding(properties);
               SPWeb mySite = SPContext.Current.Web;
               SPWebCollection subSites = mySite.Webs;
               string currentTemplate = "{10L1ZDB5-3732-44VD-9AC8-CF9D060J4354}#test";
    
               string siteUrl = SiteUrl.Text.ToString();
               string siteTitle = Description.Text.ToString();
               string siteDescription = Title.Text.ToString();
    
               subSites.Add(siteUrl, siteTitle, siteDescription, 1033,
                  currentTemplate, true, false);
           }

    -Thanks in Advance!

    Thursday, October 03, 2013 1:15 PM

Answers

  • You can create the site with following code:

    public static bool CreateSite(
        string parentSiteURL, string siteURLRequested, 
        string siteTitle, string siteTemplateName)
    {
        bool returnCondition = false; // Assume failure.
    
        const Int32 LOCALE_ID_ENGLISH = 1033;
    
        using (SPSite siteCollection = new SPSite(parentSiteURL))
        {
            SPWeb parentWeb = siteCollection.OpenWeb();
            SPWebTemplateCollection Templates = 
                siteCollection.GetWebTemplates(Convert.ToUInt32(LOCALE_ID_ENGLISH));
            SPWebTemplate siteTemplate = Templates[siteTemplateName];
            if (parentWeb.Webs[siteURLRequested].Exists)
            {
                parentWeb.Webs.Delete(siteURLRequested);
            }
    
            parentWeb.Webs.Add(
                siteURLRequested, 
                siteTitle, 
                "", 
                Convert.ToUInt32(LOCALE_ID_ENGLISH), 
                siteTemplate, 
                false, false);
            
            // All is good? 
            returnCondition = true;
        }
    
        return returnCondition;
    }


    Tarek Yehia

    Thursday, October 03, 2013 2:29 PM
  • SPContext.Current is null in event and feature receivers. Using properties.Web.
    Thursday, October 03, 2013 7:37 PM

All replies

  • Try properties.AfterProperties["SiteUrl"] instead of SiteUrl (and so on for other 2). In event receiver you cannot access the form controls. I am not sure how they are defined in your receiver...?

    If AfterProperties are null, try ItemAdded and access ListItem of properties object.

    Thursday, October 03, 2013 2:02 PM
  • You can create the site with following code:

    public static bool CreateSite(
        string parentSiteURL, string siteURLRequested, 
        string siteTitle, string siteTemplateName)
    {
        bool returnCondition = false; // Assume failure.
    
        const Int32 LOCALE_ID_ENGLISH = 1033;
    
        using (SPSite siteCollection = new SPSite(parentSiteURL))
        {
            SPWeb parentWeb = siteCollection.OpenWeb();
            SPWebTemplateCollection Templates = 
                siteCollection.GetWebTemplates(Convert.ToUInt32(LOCALE_ID_ENGLISH));
            SPWebTemplate siteTemplate = Templates[siteTemplateName];
            if (parentWeb.Webs[siteURLRequested].Exists)
            {
                parentWeb.Webs.Delete(siteURLRequested);
            }
    
            parentWeb.Webs.Add(
                siteURLRequested, 
                siteTitle, 
                "", 
                Convert.ToUInt32(LOCALE_ID_ENGLISH), 
                siteTemplate, 
                false, false);
            
            // All is good? 
            returnCondition = true;
        }
    
        return returnCondition;
    }


    Tarek Yehia

    Thursday, October 03, 2013 2:29 PM
  • How would you implement that Tarek?  Bare in mind I am new to SharePoint dev.  I tried adding the AfterProperties but recieve the following error in sharepoint when trying to save:

    The form cannot be submitted.
    Object reference not set to an instance of an object.
    
    
    
    An entry has been added to the Windows event log of the server. 
    Log ID:5567

    When debugging it is complaining about this command:

    SPWeb mySite = SPContext.Current.Web;

    
    
    Thursday, October 03, 2013 3:43 PM
  • SPContext.Current is null in event and feature receivers. Using properties.Web.
    Thursday, October 03, 2013 7:37 PM
  • Is  not it better to consume the web services from SharePoint to create your Site . You can consume the sites.asmx provided by sharePoint and consume methods to achieve this ..

    Share Point web services are always on vti bin folder  "/_vti_bin/Sites.asmx"

    Just a thought..


    Thursday, October 03, 2013 9:53 PM
  • Let me know if you need help with this ..

    _vti_bin/Sites.asmx?op=CreateWeb should help you to create it.  Just look for some information in google around this.

    Thursday, October 03, 2013 9:56 PM
  • I have been at this still for a while.  Can someone please point me in the right direction to add this or a tutorial that may explain in detail how to use this and set up my project correctly?
    Monday, November 11, 2013 1:48 PM