Monday, October 5, 2009

Sharepoint Object Model Programming #1

Creating a Site Collection

Assembly References - Windows SharePoint Services

Class Library References -
• Microsoft.SharePoint;
• Microsoft.SharePoint.Administration;

Code -

// Create an object to the root site on server
SPSite site = new SPSite("ServerPath");

// Get the list of site collections for the web application
SPSiteCollection siteCollection = site.WebApplication.Sites;

//Add the site collection

siteCollection.Add("siteUrl",
"title",
"description",
"LCID",
"webTemplate",
"ownerLogin",
"ownerName",
"ownerEmail");

/* Parameter details -
siteUrl - string - server relative url for the site collection,
title - string - site title,
description - string - description of the site collection,
LCID - unsigned 32bit integer - locale ID. http://www.microsoft.com/globaldev/reference/lcid-all.mspx

webTemplate - string - site definition or site template code,Some of default codes are as follows
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


ownerLogin - string - user name of the site owner (domain\user),
ownerName - string - Display name of site owner,
ownerEmail - string email address of site owner
*/

// Release memory used by SPSite object
site.Dispose();

Examples :

SPSite site = new SPSite("http://localhost");
SPSiteCollection siteCollection = site.WebApplication.Sites;
siteCollection.Add("http://localhost/sites/MyNewSite",
"My New Site",
"My New Site Description",
(uint)1033,
"STS#0",
"domain\\user",
"Administrator",
"admin@xxx.com");
site.Dispose();
---------------------------------------------------------------------------------
Creating a Site/Web

Assembly References - Windows SharePoint Services

Class Library References
• Microsoft.SharePoint;

Code-

// Create an object of the site collection to which the new site will be added.
SPSite site = new SPSite("SitePath");

//Add the web in site collection
SPWeb web = site.AllWebs.Add("webUrl",
"title",
"description",
"LCID",
"webtemplate",
"useUniquePermissions",
"bConvertIfThere");

/* Parameter details -
webUrl - string - site relative url for the web,
title - string - web title,
description - string - description of the web,
LCID - unsigned 32bit integer - locale ID.
webTemplate - string - site definition or site template code,e.g. STS#0 for Team Site
useUniquePermissions - bool - true to create a subsite that does not inherit permission from parent site,otherwise false,
bConvertIfThere - bool - true to convert an existing folder of the same name to a sharepoint site , false to throw an exception indicating that a URL path with the specified site name already exist.
*/

web.Dispose();
site.Dispose();

Examples :

SPSite site = new SPSite("http://localhost/sites/MyNewSite");
SPWeb web = site.AllWebs.Add("MySubSite",
"My Sub Site",
"My Sub Site Description",
(uint)1033,
"STS#0",
false,
false);

web.Dispose();
site.Dispose();

---------------------------------------------------------------------------------
Creating a Site/Web using custom template (.stp)


SPSite site = new SPSite("SitePath");
SPWebTemplateCollection templates = oSiteCollection.GetCustomWebTemplates("LCID");
SPWebTemplate template = templates["STP file name"];
SPWeb web = site.AllWebs.Add(
"webUrl",
"title",
"Description",
"LCID",
"webTemplate",
"useUniquePermissions",
"bConvertIfThere"
);

/*Parameter Details -
webTemplate - Microsoft.Sharepoint.SPWebTemplate object - represent the site definition or site template.

All others are same as in previous section
*/

web.Dispose();
site.Dispose();

Examples :

SPSite site = new SPSite("http://localhost/sites/MyNewSite");
SPWebTemplateCollection templates = site.GetCustomWebTemplates(1033);
SPWebTemplate template = templates["MyWebTemplate"];
SPWeb web = site.AllWebs.Add("MySubSiteUsingSTP",
"My Sub Site Using STP",
"My Sub Site Using STP Description",
(uint)1033,
template,
false,
false);

web.Dispose();
site.Dispose();

---------------------------------------------------------------------------------
Add/Update/Delete Site Properties

Assembly References - Windows SharePoint Services

Class Library References -
• Microsoft.SharePoint;

Code -
// Create an object of the site collection
SPSite site = new SPSite("http://localhost/sites/MyNewSite");

//Get Web
SPWeb web = site.RootWeb;

// Get all the properties in a key/value datatable
foreach (object key in web.Properties.Keys)
{
Console.WriteLine("Key : " + key + "; Value : " + web.Properties[key.ToString()]);
}

//add/Update Property
web.AllowUnsafeUpdates = true;
if (web.Properties.ContainsKey("PropertyName"))
{
web.Properties["PropertyName"] = "ChangeValue";
}
else
{
web.Properties["PropertyName"] = "NewValue";
}

//remove property
web.Properties.Remove("PropertyName");

web.Properties.Update();
web.AllowUnsafeUpdates = false;

web.Dispose();
site.Dispose();


More - Sharepoint Object Model Programming

2 comments:

Chris said...

Hi GJ.

Do you have an example web part that utilises your create site collection code and provides the fields to the user to enter the specific details to be used?

If you have I would love to see this please?

Thanks,

Chris.

Ganesh Jat said...

Hi Chris,
The example i have given in this article is working example.You can use this code in a webpart. Your webpart will have input controls for all the parameters.and on submit button click you can use my code to create a site collection.

Unfortunatily i don't have any webpart.i have tested this code in a console application.

Thanks
GJ