Add a new Site to SharePoint Toplevel site using a Webservice
Posted by: Harsh Bardhan ,
on 4/13/2009,
in
Category SharePoint
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.
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
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.
That's it!
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.
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 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 Book 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 the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) 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!
Was this article worth reading? Share it with fellow developers too. Thanks!