Programmatically create site collection in sharepoint 2010

In this post you will see a generic console application which will read through an xml file(which is a file that contain site collection Properties like template, admin password etc.) and will create a site collection as per the data in the xml file.

Here is the xml file that can be prepared by the administrator

<?xml version="1.0" encoding="utf-8" ?>
<SiteDefinition>
<Url>/sites/BlankInternetSite</Url>
<Title>My Custom Site Collection</Title>
<Description>This site is being created by a Client OM.</Description>
<LCID>1033</LCID>
<WebTemplate></WebTemplate>
<OwnerLogin>Administrator</OwnerLogin>
<OwnerName>Administrator</OwnerName>
<OwnerEmail>funnyadmin@learningsharePoint.com</OwnerEmail>
</SiteDefinition>

as you can see above the xml file is nothing but set of properties that we wiil need in our console app.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Xml.Linq;

namespace Apress.SP2010.CreateSiteCollection
{
class Program
{
static void Main(string[] args)
{
System.Environment.ExitCode = CreateSiteCollection();
}
private static int CreateSiteCollection()
{
try
{
string srvUrl = "http://sharepointserve";
using (SPSite site = new SPSite(srvUrl))
{
// Current collection
SPSiteCollection coll = site.WebApplication.Sites;
XDocument definition = XDocument.Load("SiteDefinition.xml");
XElement root = definition.Element("SiteDefinition");
SPSite newSite = coll.Add(
root.Element("Url").Value,
root.Element("Title").Value,
root.Element("Description").Value,
Convert.ToUInt32(root.Element("LCID").Value),
(String.IsNullOrEmpty(root.Element("WebTemplate").Value) ?
null : root.Element("WebTemplate").Value),
root.Element("OwnerLogin").Value,
root.Element("OwnerName").Value,
root.Element("OwnerEmail").Value
);
return 0;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 1;
}}}}

0 comments:

Post a Comment

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com