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.]

Don't want to create new version or want to edit fields like Author, Modified, etc
When you have versioning enabled on document library or list and when you do not want to create a new version even you are updating an item in the list or document library at that time you can use this method because Update will create version for the item.

Many times i also found that in workflow when you want to update any item on which workflow is running and if you use Update method on the SPListItem, it throws Error in terms on Error Starting in Workflow. When i use this UpdateOverwriteVersion, it didn't give me.

With the help of this method, you can also set certain properties like Author, Editor and Modified. Remember you need to change both Author as well as Editor on these properties to take effect.

Example :

using (SPSite objSite = new SPSite("http://server/"))
{
using (SPWeb objWeb = objSite.OpenWeb())
{
SPList objDocLib = objWeb.Lists["Shared Documents"];
SPListItem objItem = objDocLib.Items[0];
objItem ["Created"] = new DateTime(2008, 5, 5);
objItem ["Author"] = "{author}";
objItem ["Editor"] = "{editor}";
objItem ["Modified"] = new DateTime(2008, 5, 6);
objItem.UpdateOverwriteVersion();
}
}
How to Hide fields in Newform.aspx/editform.aspx

Suppose you don't want to show a column/field in Newform.aspx or Editform.aspx for a list. There are couple of way to hide a column from Newform.aspx/Editform.aspx. I have used the javascript in Content Editor webpart to hide the column in Newform.aspx. I open the Newform.aspx in the browser. By default the "Editpage" option for this page will be disabled. To the URL of the page add "&Toolpaneview=2". You will be able to edit the page. Click on "Add a new webpart" and select "Content Editor Webpart". In the "source view" add the following code :

//
script language="javascript" type="text/javascript">_spBodyOnLoadFunctionNames.push("hideFields");
function hideFields() {var control = getTagFromIdentifierAndTitle("Input","TextField","Misc");control.parentNode.parentNode.parentNode.style.display="none";}
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {var tempString = tags[i].id;if (tags[i].title == title && (identifier == "" tempString.indexOf(identifier) == tempString.length - len)) {return tags[i];}}return null;}
//
//
The Above code assumes that you have a Textfield named "Misc".