Manage SharePoint SPWeb Programmatically
In order to create SPWeb programmatically you need to make sure that your code that uses SharePoint object model executes on a SharePoint front end server within the SharePoint farm. The SPWeb creation is possible by the user with the sufficient rights to the SharePoint farm and membership in Microsoft.SharePoint.Administration group is a preferred choice.
First, you need to make sure that Assembly reference is set correctly within your Visual Studio 2010 IDE. You will link Microsoft.SharePoint library to your class library references within your project. The SPWeb programmatically provisioning is similar to STSADM -O CREATEWEB command.
It is confusing at times to distinguish between SPSite and SPWeb. However, we create one web site every time we provision SPSite and SPSite is a collection of SPWebs. The SharePoint SPSite requires at least one SPWeb created during SPSite provisioning.
using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using System; class CreateSite { static void Main(string[] args) { if ((args.Length == 0)) { args = new string[7]; args = GetParams(ref args); } AddSPSiteCollection(args); } private static bool AddSPSiteCollection(string[] args) { try { string[] arrSitePath = args[0].Split('/'); string strServerPath = (arrSitePath[0] + ("//" + arrSitePath[2])); SPSite site = new SPSite(strServerPath); SPSiteCollection siteCollection = site.WebApplication.Sites; siteCollection.Add(args[0], args[1], args[2], 1033, args[3], args[4], args[5], args[6]); Console.WriteLine(("SPSite \'" + (args[0] + "\' created."))); SPWeb web = site.AllWebs.Add( args[1], args[2], args[3], (uint)1033, args[4], false, false); web.Dispose(); site.Dispose(); return true; } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } } }