Creating a new Website dynamically in SharePoint using Visual Studio

31 03 2010
Hiya,
Here is a small sample code which creates a new website dynamically (during execution) in SharePoint. I put this down because I myself had a lot of problems searching for this excerpt on the net. Hope this post helps reduce the effort by others in the future. The sample code is as follows:
SPSite _portalSite = new SPSite(“http://localhost”);     //localhost is your main parent site.
_portalSite.AllWebs.Add(webUrl);    //webUrl is a string that serves as the Url path for the website being created. Here webUrl = “ShivaSite”;
SPControl.SetContextSite(System.Web.HttpContext.Current, this._portalSite);    //Setting the context to the main parent Site.
this._newSite = SPControl.GetContextWeb(System.Web.HttpContext.Current).Webs[webUrl];
//getting the Context for site created. We need this to set the main intricate properties of the Website that is created.
this._newSite.Title = webTitle;
this._newSite.Description = webDescription;
this._newSite.ApplyWebTemplate(SPWebTemplate.WebTemplateSTS);
this._newSite.Locale = culture;     //culture is as System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(“en-us”);
this._newSite.Update();   // We must update the Site.
this._newSite.QuickLaunchEnabled = true;    //enables the site to be available at QuickLaunch.
There are other overloaded functions for the AllWebs.Add() which include all the properties as arguments. The details for that may be found in the MSDN library available online.


Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: