Add a new Site to SharePoint Toplevel site using a Webservice

Posted by: Harsh Bardhan , on 4/13/2009, in Category SharePoint
Views: 98719
Abstract: At times, it is very useful to allow users to create a site in SharePoint dynamically using code. SharePoint sites are a group of Libraries and Lists. In this article we will discuss how to create a SharePoint site through a WebService.
Add a new Site to SharePoint Toplevel site using a Webservice
                      
At times, it is very useful to allow users to create a site in SharePoint dynamically using code. SharePoint sites are a group of Libraries and Lists. In this article we will discuss how to create a SharePoint site through a WebService.
 
Note: This article will discuss only  about creating a SharePoint site using Webservices, however we can do the same task using WCF Service/Webpages.
                  
Step 1: Create a New ASP.NET WebService Application. Open Visual Studio > File > New Project > Select the language (C# or VB.NET) > Select Web Template > Select ASP.NETWebService Application.


Sharepoint New Site 
Step 2: Name the application as ‘CreateSiteService’ and specify the location where you want to save that project. This step will create a Service1.asmx file.
Step 3: The Service1.asmx file by default contains a HelloWorld method.
C#
 
[WebMethod]
public string HelloWorld()
{
      return "Hello World";
}
 
VB.NET       
 
<WebMethod> _
Public Function HelloWorld() As String
       Return "Hello World"
End Function
 
We will rename the method to ‘CreateNewSite' and return type to string. Our method will look similar to the following. The method will return a message indicating success or error occurred.
 
C#
 
[WebMethod]
public string CreateNewSite()
{
}
 
VB.NET
 
<WebMethod> _
Public Function CreateNewSite() As String
End Function
 
Step 4: Add a reference to the Microsoft.SharePoint dll as shown in screenshot below and add a namespace in your .cs or .vb file.
 
C#
 
using Microsoft.SharePoint;
 
VB.NET
 
Imports Microsoft.SharePoint
 
Add Reference
 
Step 5: Now add code in the ‘CreateNewSite’ method for connecting to your SharePoint  top level site and adding a site to that.
 
Our Complete web method will look like this. The comments explain the code.
 
C#
 
[WebMethod]
public string CreateNewSite()
{
 
    //Reference of top level site object
    //Change this url according to your site URI
    SPSite siteObject = new SPSite("http://localhost");
 
    //Setting security setting to allow user to Create Site
 
    SPWebApplication webApplication = siteObject.WebApplication;
    webApplication.FormDigestSettings.Enabled = false;
    try
    {
        //Here we are passing Different parameters to create site directly
        //We can create a user screen also from which we can pass data
        //1033 is For English Locale
        SPWeb webObject =siteObject.AllWebs.Add("TestSite", "New Sharepoint Site ", "Creating a new sharepoint site",((System.UInt32)(1033)), SPWebTemplate.WebTemplateSTS,false, false);
 
        //Disposing all objects all objects
        webObject.Dispose();
        siteObject.Dispose();
        return "Success";
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}       
 
VB.NET
 
<WebMethod> _
Public Function CreateNewSite() As String
 
      'Reference of top level site object
      'Change this url according to your site URI
      Dim siteObject As New SPSite("http://localhost")
 
      'Setting security setting to allow user to Create Site
 
      Dim webApplication As SPWebApplication = siteObject.WebApplication
      webApplication.FormDigestSettings.Enabled = False
      Try
            'Here we are passing Different parameters to create site directly
            'We can create a user screen also from which we can pass data
            '1033 is For English Locale
            Dim webObject As SPWeb =siteObject.AllWebs.Add("TestSite", "New Sharepoint Site ", "Creating a new sharepoint site",(CUInt(1033)), SPWebTemplate.WebTemplateSTS,False, False)
 
            'Disposing all objects all objects
            webObject.Dispose()
            siteObject.Dispose()
            Return "Success"
      Catch ex As Exception
            Return ex.Message
      End Try
End Function    
 
 
Run this application and click on View All Site Content link of your site. You will find your new site in ‘Sites and Workspaces’ section of your site like the one shown below.
 
Sites and Workspace preview
 
That's it!
If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email  
Harsh Bardhan (MCTS) has developed IT solutions with a diverse background in server side and client side development. He is currently working with SymphonyServices as a Software developer. Harsh continues to be an integral part of open forums on cutting-edge technology like Silverlight, including the .NET Framework and Web Services. Harsh has expertise with many Microsoft technologies, including .NET, LINQ, WCF Service, Silverlight and a strong background in SQLServer. 
 

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Author bio not available


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Rajeev on Tuesday, April 14, 2009 5:55 AM
Nice Article.
Comment posted by Kapros on Monday, April 27, 2009 9:29 AM
Clean and Simple. I am looking for more tips from you
Comment posted by Harsh Bardhan on Monday, April 27, 2009 11:08 AM
If you are looking for some particular topic you can mention that.
Comment posted by roshan on Sunday, August 16, 2009 8:49 AM
nice topic...thsnk u very much
Comment posted by Ash on Monday, November 5, 2012 2:59 PM
I tried it  and added remote reference to microsoft.sharepoint.dll on the remote machine but I am getting Could not load file or assembly 'Microsoft.SharePoint.Search. Any suggestions
Comment posted by Ganesh on Tuesday, November 13, 2012 6:28 AM
I got this page, after click the INVOKE button..

System.IO.FileNotFoundException: The Web application at http://ganesh:28703/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
   at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
   at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
   at CreateSiteService13.Service1.CreateNewSite() in c:\users\administrator\documents\visual studio 2010\Projects\CreateSiteService13\CreateSiteService13\Service1.asmx.cs:line 29

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook