Tuesday, February 24, 2009

How to Create site programmatically

Code :

using(SPSite oSiteCollection = new SPSite("http://"Server_Name")){

SPContext.Current.Web.AllowUnsafeUpdates = true;

SPWebCollection objWebs = oSiteCollection .Webs;

objWebs.Add("strUrl", "strTitle", "strDescription", 1033,"STS#0", true, false);

SPContext.Current.Web.AllowUnsafeUpdates = false;

}

There are three overload for creating site. First overload simply give the name of the site that you want to create which does not give us much flexibility.Rest two overloaded method has only one difference. Difference is of specifying the site template. you want to use team site, blank site, wiki, blog. What kind of a site you want to create goes here.

Parameters used in the above code :


strWebUrl :A string that contains the new Web site URL relative to the root Web site in the site collection. For example, to create a Web site at http://MyServer/sites/MySiteCollection/MyNewWebsite, specify MyNewWebsite, or to create a Web site one level lower at http://MyServer/sites/MySiteCollection/Website/MyNewWebsite, specify Website/MyNewWebsite.
strTitle : A string that contains the title.
strDescription : A string that contains the description.
nLCID : A 32-bit unsigned integer that specifies the locale ID.
strWebTemplate :A string that contains the name of the site definition configuration or site template. The following table shows the values for the default site definition configurations that are included in an installation of Windows SharePoint Services.
STS#0 -Team Site
STS#1 -Blank Site
STS#2 -Document Workspace
MPS#0 -Basic Meeting Workspace
MPS#1 -Blank Meeting Workspace
MPS#2 -Decision Meeting Workspace
MPS#3 -Social Meeting Workspace
MPS#4 -Multipage Meeting Workspace
WIKI#0 -Wiki
BLOG#0 -Blog
By default, a global site template (GLOBAL#0) is added to all site definitions. You cannot explicitly create a site based on a global site template.
useUniquePermissions : true to create a subsite that does not inherit permissions from another site; otherwise, false .
bConvertIfThere : true to convert an existing folder of the same name to a SharePoint site. false to throw an exception that indicates that a URL path with the specified site name already exists.
[Return ValueAn SPWEB object that represents the new Web site.]

No comments: