Friday, February 6, 2009

create a site programmatically in moss

Hi All,

Sometimes we often requires to create a site programmatically in code.

So here is a simple way we can create any site from code.

for that first you need to gain an access on Web or Site under which you want to create your site.

so i am writing a code in webpart just to demonstrate you the things, you can change the code accordingly where ever you are writing your code like in event handler or feature receiver.

SPContext.Current.Web.AllowUnsafeUpdates = true;
SPWebCollection objWebs = SPContext.Current.Web.Webs;
objWebs.Add("GAME ZONE", "Games Site", "This is the Games Site", 1033,"STS#0", true, false);
SPContext.Current.Web.AllowUnsafeUpdates = false;


Let us understand the parameters for this.

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.

So one overloaded method with the SPWEbTemplate uses enumerations to choose from and the other overload which gives you an option to write down String for site template.


Now here is a list of Strings that you can place in the parameter as in the example it is STS#0 for Team Site.

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

and these are additional web template IDs

BDR#0 Document center—A central document management location for an enterprise

OFFILE#0 Records center—A central location in which records

OFFILE#1 managers can define routes for incoming files

CMSPUBLISHING#0 Publishing site

BLANKINTERNET#0 Publishing site—A site for publishing web pages on a schedule with workflow features enabled

BLANKINTERNET#1 Press releases site

BLANKINTERNET#2 Publishing site with workflow—A publishing site for web pages using approval workflows

SPSNHOME#0 A site for publishing news and articles

SPSREPORTCENTER#0 Report center—A site for creating, managing, and delivering web pages, dashboards, and Key Performance Indicators (KPIs)

SPSPORTAL#0 A starter hierarchy for an intranet divisional portal

PROFILES#0 A profile site that includes page layouts with zones

BLANKINTERNETCONTAINER#0 Publishing portal—a site collection preconfigured for revision-controlled, secure content creation and publication

SPSMYSITEHOST#0 My Site host—keep in mind that only one of these can be provisioned per Shared Services Provider

SRCHCENTERLITE#0 Search center—A site designed to deliver the search query and results experience

SRCHCENTERLITE#1 Search center—A superset of the previous; does not appear in navigation bars


These are the available templates.


1033 is for site in English language.


That's it. Your job is done. you have created site dynamically.

1 comment:

Juan Diego Raimondi said...

Hi, I extended this code to make it more extensible and easy to use.

I hope it is useful to you:

http://cv.alphasmanifesto.com/2011/05/11/create-sharepoint-sites-programatically/




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com