Thursday, May 12, 2011

Creating windows service for creating sites

Hi,
I am posting three ways of approaches that I followed to create windows service for creating sites
First Approach: In this approach I created a xml where I have saved the process names, owner login, owner name. From this xml I am taking process names and creating the sites in Console Application
Here through code I have taken care of displaying the message if site already exists
o Deployment : Xml file, Console Application exe and Console Application exe config files to be moved from test server to dev/production server
o Solution file : CreateSiteCA.zip

Second Approach : In second approach I created a bat file and I have written stsadm commands to create the sites
o Deployment : Bat file needs to be moved from test server to dev/production server
o Solution file : createsite.bat

Third Approach : In this approach I have created list in which I have saved process names, owner login, owner names. From this list I am fetching the process names and creating the sites
Here through code I have taken care of displaying the message if site already exists
o Deployment : List template, Console Application exe and Console Application exe config files to be moved from test server to dev/production server
o Solution file : CreateSitefromListCA.zip
----------------------------CreateSiteCA.zip--------------------

// App.config, Configurations.xml can be found as below




//Program.cs
using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

using System.Configuration;
using System.Xml;

namespace CreateSiteCA
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite(ConfigurationSettings.AppSettings["SiteCollectionURL"]))
{
SPWebApplication webApp = site.WebApplication;
//Random rand = new Random();
//int index= rand.Next(1000);
//SPSite newSite= webApp.Sites.Add("/sites/TestSite" + index, "This is my Test Site",
// "This is the First Site Added Programmatically", 1033,
// "STS#0", @"domain\username", "username", "username@domain.com");

XmlDocument doc = new XmlDocument();
doc.Load("Configurations.xml");

XmlNodeList list = doc.SelectNodes("Sites/Site");
foreach (XmlNode node in list)
{
if (webApp.Sites["/sites/" + node.Attributes["name"].Value] != null)
{
Console.WriteLine("Site by name '" + node.Attributes["name"].Value + "' already exists " + Environment.NewLine);
continue;
}
Console.WriteLine("Please wait currently creating new site.." + Environment.NewLine);
SPSite newSite = webApp.Sites.Add("/sites/" + node.Attributes["name"].Value, node.Attributes["title"].Value,
node.Attributes["description"].Value, Convert.ToUInt32(node.Attributes["nLCID"].Value), node.Attributes["webTemplate"].Value,
node.Attributes["ownerLogin"].Value, node.Attributes["ownerName"].Value, node.Attributes["ownerEmail"].Value);

if (newSite != null)
{
Console.WriteLine("Site Created: " + newSite.Url + Environment.NewLine);
newSite.Dispose();
}
else
{
Console.WriteLine("Error Creating:" + node.Attributes["name"].Value + Environment.NewLine);
}
}
Console.WriteLine("Site Creation Compleated. Please press any key to continue..");
Console.ReadLine();
}
}
}
}

-------------------------------------------------------- createsite-----------------------------------------------------
//createsite.bat
@echo off

echo ===============================================================

echo Creating the sites

echo ===============================================================

cd %COMMONPROGRAMFILES%\Microsoft Shared\web server extensions\12\BIN

@echo off

stsadm -o createsite -url "http://servername:portno/sites/testsite6" -owneremail "username@domain.com" -ownerlogin "domain\username" -title "test site" -description "test site"

echo completed

--------------------------------------------------- CreateSitefromListCA-----------------------------------------

// App.config can be found as below



//Program.cs
using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

using System.Configuration;

namespace CreateSitefromListCA
{
class Program
{
static void Main(string[] args)
{
using (SPSite objsite = new SPSite(ConfigurationSettings.AppSettings["SiteCollectionURL"]))
{

using (SPWeb objweb = objsite.OpenWeb())
{
SPList objlist = objweb.Lists["Process List"];
SPListItemCollection objitmcollection = objlist.Items;
SPWebApplication objwebapp = objsite.WebApplication;

for (int i = 0; i < objlist.ItemCount; i++)
{
string processname = Convert.ToString(objitmcollection[i]["Title"]);
string strownerlogin = Convert.ToString(objitmcollection[i]["owner login"]);
string strowneremail = Convert.ToString(objitmcollection[i]["owneremail"]);
//if ((objwebapp.Sites.WebApplication.Name)!=processname.n)
//{
try
{
objwebapp.Sites.Add("/sites/" + processname, "This is my Test Site", "", 1033, "STS#0", @"domain\username", strownerlogin, strowneremail);
Console.WriteLine(processname + " Site Created");
}
catch (System.Exception _Ex)
{
Console.WriteLine("A Site with title: " + processname + " already exists");
}

// Console.ReadLine();
//}


}
}


}
}
}
}