Tuesday, May 20, 2014

Creating subsite programmatically in SharePoint 2010 or Code to create subsite programmatically in SharePoint 2010

I had this as a console but the same code would work fine even if used in Server Object Model


My Console Application Code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace CreateMySiteConsole
{
class Program
{
static void Main(string[] args)
{
try
{
string subSiteUrl = string.Empty;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite osite = new SPSite("http://myserver03:8989"))
{
using (SPWeb oweb = osite.OpenWeb())
{
 
uint webLcid = oweb.Language;//u can use this,the value will be 1033 
//SPWeb newWebSubSite = osite.AllWebs.Add("mysiteTest", "mysiteTest", "mysiteTest", webLcid, "STS#0", false, false);//1 
SPWeb newWebSubSite = osite.AllWebs.Add("mysite""mysite""mysite", 1033, "STS#0"falsefalse); //2 --- Both 1 & 2 would create site in 1st case I have passed webLcid and in 2nd case I have hard-coded the language code as 1033
newWebSubSite.Update();
subSiteUrl = newWebSubSite.Url;
newWebSubSite.Dispose();
Console.WriteLine("Subsite created" + subSiteUrl);
Console.ReadLine();
 
}
}
});
 
}
catch (Exception ex)
{
throw ex;
}
}
}
}



Additional information on different site template codes which can be used : 

  • WSS Templates 
  • Team Site: STS#0 
  • Blank Site: STS#1 
  • Document Workspace: STS#2 
  • Wiki Site: WIKI#0 
  • Blog Site: BLOG#0 
  • Basic Meeting Workspace: MPS#0 
  • Blank Meeting Workspace: MPS#1 
  • Decision Meeting Workspace: MPS#2 
  • Social Meeting Workspace: MPS#3 
  • Multiple Meeting Workspace: MPS#4 
  • MOSS Templates 
  • Document Center: BDR#0 
  • Site Directory: SPSSITE#0 
  • Report Center: SPSREPORTCENTER#0 
  • Search Center with Tabs: SRCHCEN#0 
  • My Site Host: SPSMSITEHOST#0
  • Search Center: SRCHCENTERLITE#0 
  • Personalisation Site: SPSMSITE#0 
  • Collaboration Portal: SPSPORTAL#0 
  • Publishing Portal: BLANKINTERNETCONTAINER#0 
  • Publishing Site: CMSPUBLISHING#0 
  • Publishing Site with Workflow: BLANKINTERNET#2 
  • News Site: SPSNHOME#0 
References : http://www.shareesblog.com/?p=601 








No comments:

Post a Comment