Programmatically generating site collections has many advantages in terms of promoting
consistency of organization. For example, you may want to make sure that the naming of new
site collections follows a corporate standard and that these collections are created by using
a particular path. You may also want to ensure that a specific account is assigned to the site
collection administrators group. The following recipe shows you how to accomplish these
programming goals by using the object model and a .NET console application. The functionality
is similar to that found in the STSADM –O CREATESITE command, with one twist—this recipe
will prompt the user for missing parameters. However, you can build on this recipe to expand
its functionality as needed.
Assembly References
• Windows SharePoint Services .NET assembly
Class Library References
• Microsoft.SharePoint library
• Microsoft.SharePoint.Administration library
Special Considerations
• SharePoint requires that site collections be created by accounts that have sufficient privileges.
The following recipe is designed to be used in a .NET console application, and can
be run either manually or via the Windows Task Scheduler. Console applications are
particularly useful in combination with the Windows Task Scheduler application, enabling
you to create processes that run against SharePoint at predefined intervals.
• Although this recipe essentially duplicates what is available through the STSADM –O
CREATESITE command, it opens up many possibilities for customizations (such as
prompting the user for missing parameters).
• Because of security restrictions that SharePoint imposes on ASP.NET web applications
and services, members of the Microsoft.SharePoint.Administration library are best
used through console or Windows applications, where the security context is directly
inherited from the user. The limitation is that these applications must be run from a
front-end web server that is part of the SharePoint farm. This is appropriate for administrative
tools, but not convenient if you want to make this functionality available to end
users. In a related recipe using the SharePoint web service, you’ll see how to build an
ASP.NET web application to add new site collections.
1. The first step is to determine whether the user passed in the necessary arguments to
this process. If so, they will be used when calling the SPSiteCollection.Add() method.
If not, you need to get that information from the user now.
2. Prompt the user for the necessary data by using the WriteLine() and Read() methods.
3. Using the supplied inputs, call the SPSiteCollection.Add() method to add the new site
collection.
4. If the SPSiteCollection.Add() method throws an exception, go to step 6; otherwise go
to step 5.
5. Display a success message to the console. Go to step 7.
6. Display an error message, including a description of the exception thrown, to the console.
7. Write all parameters back to the console so the user can see exactly what was received
and acted on by the application.
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration
Module Module1
Sub Main (ByVal args() As String)
'Steps 1-2: If no arguments have been passed,
'prompt user to enter them now
If args.Length = 0 Then
ReDim args(7)
args = GetParams(args)
End If
'Add the new site collection using the provided arguments
AddSiteCollection(args)
End Function
Private Function AddSiteCollection(ByVal args() As String) As Integer
Try
'Get a handle to the root site on server
Dim arrSitePath As String() = args(0).Split("/")
Dim strServerPath As String = arrSitePath(0) & "//" & _
arrSitePath(2)
Dim site As New SPSite(strServerPath)
'Get the list of site collections for the web application
Dim siteCollection As SPSiteCollection = _
site.WebApplication.Sites
'Step 3: Add the site collection
'args(0) = Site url
'args(1) = Title
'args(2) = Description
'args(3) = Web template
'args(4) = Owner login
'args(5) = Owner name
'args(6) = Owner email
siteCollection.Add( _
args(0), _
args(1), _
args(2), _
1033, _
args(3), _
args(4), _
args(5), _
args(6))
'Step 5: Confirm site collection information
Console.WriteLine()
Console.WriteLine("Site collection '" & args(0) & _
"' successfully created.")
Console.WriteLine()
DisplayParams(args)
'Release memory used by SPSite object
site.Dispose()
Return 0
Catch ex As Exception
'Step 6: If error occurs, display parameters and error message
Console.WriteLine()
Console.WriteLine("** ERROR OCCURRED **")
Console.WriteLine()
Console.WriteLine(ex.Message)
Console.WriteLine()
DisplayParams(args)
Console.WriteLine()
End Try
End Sub
Private Sub DisplayParams(ByVal args)
Try
'Step 7: Display parameters to console
Console.WriteLine("Site url: " & args(0))
Console.WriteLine("Title: " & args(1))
Console.WriteLine("Description: " & args(2))
Console.WriteLine("Template name: " & args(3))
Console.WriteLine("Owner login: " & args(4))
Console.WriteLine("Owner name: " & args(5))
Console.WriteLine("Owner email: " & args(6))
Catch ex As Exception
'If error occurred, display the error message
Console.WriteLine()
Console.WriteLine(ex.Message)
Console.WriteLine()
End Try
End Sub
Private Function GetParams(ByRef args() As String) As String()
Try
'Step 2: Get parameters from user
Console.WriteLine()
Console.Write("Site url: ")
args(0) = Console.ReadLine()
Console.Write("Title: ")
args(1) = Console.ReadLine()
Console.Write("Description: ")
args(2) = Console.ReadLine()
Console.Write("Template name: ")
args(3) = Console.ReadLine()
Console.Write("Owner login: ")
args(4) = Console.ReadLine()
Console.Write("Owner name: ")
args(5) = Console.ReadLine()
Console.Write("Owner email: ")
args(6) = Console.ReadLine()
Catch ex As Exception
'If an error occurred, display the error message
Console.WriteLine()
Console.WriteLine(ex.Message)
Console.WriteLine()
End Try
Return args
End Function
End Module