Search code examples
javascriptsharepointsharepoint-2013sharepoint-apps

SharePoint 2013 - create site collection programmatically


With the introduction of Apps, and solutions going away. What is the best way to create a site collection programmatically? I am on-premise. Can it be done with JavaScript? I can create sub-sites with js, but what about Site Collections?

I have found some c# code to create a site collection, but where do I run it from? In an App?


Solution

  • I have successfully created a site collection programmatically using an event receiver (c#). Microsoft has confirmed that it is not possible to create a site collection with JavaScript. I thought I would share my total solution since I don't see complete documentation from start to finish.

    1. I followed these steps to setup a proper dev environment for SP2013 http://msdn.microsoft.com/en-us/library/fp179923.aspx
    2. I created a new VS2012 empty sharepoint 2013 project, I chose farm solution
    3. Add Web Reference
      • Right-click on Service References
      • Add Service References
      • Advanced Button
      • Add Web Reference Button
      • http://{SharePointServer}:{Port}/_vti_adm/Admin.asmx
      • login with farm account
      • name the reference "SPAdminService"
      • Add Reference Button
    4. Add Event Receiver

      • Right-click on the project
      • add
      • new item
      • event receiver
    5. Here is the code for the eventreceiver.cs file:

      using System;
      using System.Security.Permissions;
      using Microsoft.SharePoint;
      using Microsoft.SharePoint.Utilities;
      using Microsoft.SharePoint.Workflow;
      
      
      namespace SharePointProject5.EventReceiver1
      {
      /// <summary>
      /// List Item Events
      /// </summary>
      public class EventReceiver1 : SPItemEventReceiver
      {
              /// <summary>
              /// An item is being added.
              /// </summary>
              public override void ItemAdding(SPItemEventProperties properties)
      {
          base.ItemAdding(properties);
          //create admin service reference
          SPAdminService.Admin admService = new SPAdminService.Admin();
          //grant proper admin credentials to admin service
          admService.Credentials = new System.Net.NetworkCredential("Farmaccount",         "Password", "Domain");            
          try
          {
              //specify new site collection path
              String SitePath = "http://twv101sp13/pm/new2013";
              //setup properties to create the site, see reference below for more info
              admService.CreateSite(SitePath, "new site", "description", 1033, "STS#1", "bluebunny\\sp13admin", "System Account", "cslee@bluebunny.com", "", "");                           
          }
          catch (System.Web.Services.Protocols.SoapException ex)
          {
              //I added this section so any errors would be logged in the server application log
              SPSecurity.RunWithElevatedPrivileges(
              delegate()
              {
                  System.Diagnostics.EventLog.WriteEntry("Application", "Message:\n" + ex.MRessage + "\nDetail:\n" +
                      ex.Detail.InnerText +
                      "\nStackTrace:\n" + ex.StackTrace);
              });
          }
       }
      
      
       }
      }
      
    6. So you only want this to run when a certain list adds an item so you have to modify the Elements.xml file.

    7. Open the Elements.xml, replace this line <Receivers ListTemplateId="101"> with <Receivers ListUrl="http://{SharepointSite}/{targetListName}"> - this is so we can target only that list and not all lists on the the site

    8. Right-click your project and deploy

    9. You should be able to create a new item and then check for your new site collection.

    One issue that I experienced is sites cannot start with a numerical character.

    At the bottom of this article it tells how to debug a feature event receiver. Without that I would have been dead in the water: http://msdn.microsoft.com/en-us/library/ee231550.aspx

    These three articles helped a lot:

    1. http://msdn.microsoft.com/en-us/library/websvcadmin.admin.createsite.aspx

    2. http://onceinawhilescribble.blogspot.com/2013/05/creating-simple-event-receiver-in.html

    3. http://www.sharepointpals.com/post/How-to-create-a-custom-list-level-event-receiver-in-SharePoint-2013-and-SharePoint-2010