Programatically create a SharePoint site based on a site definition
Posted on
5/19/2007 @ 4:08 PM
in
#Sharepoint
|
12 comments
|
8998 views
Here is a peice of code that I know you will find useful one day. Basically, you supply it with an existing site definition, say "STS#1", and this will then create a site for you, based on the supplied site definition, at the URL you asked for.
Here goes - public static bool CreateSite(
string parentSiteURL, string siteURLRequested,
string siteTitle, string siteTemplateName)
{
bool returnCondition = false; // Assume failure.
const Int32 LOCALE_ID_ENGLISH = 1033;
using (SPSite siteCollection = new SPSite(parentSiteURL))
{
SPWeb parentWeb = siteCollection.OpenWeb();
SPWebTemplateCollection Templates =
siteCollection.GetWebTemplates(Convert.ToUInt32(LOCALE_ID_ENGLISH));
SPWebTemplate siteTemplate = Templates[siteTemplateName];
if (parentWeb.Webs[siteURLRequested].Exists)
{
parentWeb.Webs.Delete(siteURLRequested);
}
parentWeb.Webs.Add(
siteURLRequested,
siteTitle,
"",
Convert.ToUInt32(LOCALE_ID_ENGLISH),
siteTemplate,
false, false);
// All is good?
returnCondition = true;
}
return returnCondition;
}
Enjoy!
|
On
5/21/2007 3:19:01 PM
Dez
said ..
Maybe you can answer this. I've been sucessfull so far in creating sites programmatically...examples like yours have made that rather simple. The problem I'm running into now is how to access the different templates through the site creation code.
I've used the standard list of:
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 Site
BLOG#0 Blog Site
But I cant seem to find any documentation on the "40" templates ID #'s. I've scoured the web trying to find anyone who's made reference to this list of ID's, bu no one seems to have posted or listed this info anywhere.
Would you happen to know what the Id's are for the 40 additional templates or how I would reference them through code in the site creation process?
|
On
5/21/2007 8:59:59 PM
Sahil Malik
said ..
Dez -
The best thing to do would be to install a template, and see what ID it gets. The ID depends on how the site definition's XML files are written.
SM
|
On
5/22/2007 10:54:53 AM
Dez
said ..
SM,
I was actually able to reference the template through its name in code. What's odd is how Microsoft designed this whole thing. The standard templates (listed in my previous post) and the new 20 application templates all get referenced through their ID's, but the new 20 custom templates all get referenced through their names instead.
Would've been nice to see some documentation for that fact from them, but it doesn't matter now that I've got it figured out.
Thanks for the help,
Dez
|
On
5/25/2007 4:05:09 AM
Sharepointer
said ..
And how can I "see what ID it gets" - I'm new to this.
Also: is it possible to create a Web based on a .stp file?
|
On
1/2/2008 3:53:16 PM
HyperX
said ..
Sahil,
Should this code be run on the sharepoint server? How to create a site dynamically using my ASP.NET application, which is hosted on a different box?
Thanks,
HyperX.
|
On
1/3/2008 1:14:23 AM
Sahil Malik
said ..
HyperX - you need to WSS Object model to run the above code.
|
On
1/9/2008 12:06:29 PM
Ami
said ..
Sahil,
Is it possible to pass in site template (<filename>.stp)?
Thanks,
Ami
|
On
1/10/2008 2:55:53 PM
Ami
said ..
I am getting following error message while executing the code, can you help?
System.IO.FileNotFoundException occurred
Message="The Web application at http://amrndhw034:10011/sites/TKR2/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application."
Source="Microsoft.SharePoint"
StackTrace:
at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
at SharepointSiteCreationService.SiteCreatton_Form.CreateSite(String parentSiteURL, String siteURLRequested, String siteTitle, String siteTemplateName) in D:\Projects\SharepointSiteCreationService\SharepointSiteCreationService\SiteCreatton Form.aspx.cs:line 36
thanks,
Ami
|
On
1/23/2008 5:16:40 AM
James
said ..
Sahil,
How do I add the wiki template to the site definition? It must go like this:
The Wiki Template is not default in the list of templates in the subsites because it is not included in the site definition.
Once included, we could already stop doing the steps below every time we create a new site:
o Add the Microsite Wiki Site Template (Root > Site Settings > Modify All Site Settings > Page layouts and site templates)
o Ensure that the “Reset all subsites to inherit these preferred subsite template settings” is checked.
|
On
4/15/2008 1:05:03 PM
Anadmin
said ..
Hi,
I tried using your code... but when there are more than one user simultaneously executing the same code to create a site, the code throws a 'Out Of Memory' error when executing the below line
parentWeb.Webs.Add(
siteURLRequested,
siteTitle,
"",
Convert.ToUInt32(LOCALE_ID_ENGLISH),
siteTemplate,
false, false);
We dont know what to do... any help would be highly appreciated...!
|
On
6/16/2008 12:31:53 PM
Kien Tran
said ..
Just a remark,
I've been trying to use this code to create a site based on a custom template (Site Settings > Save this site as template...)
I was getting a error :
File or arguments not valid for site template 'Custom.stp'
I got it to work by replacing this line:
SPWebTemplateCollection Templates = siteCollection.GetWebTemplates(Convert.ToUInt32(LOCALE_ID_ENGLISH));
with this line:
SPWebTemplateCollection Templates = siteCollection.GetCustomWebTemplates(Convert.ToUInt32(LOCALE_ID_ENGLISH));
Regards,
KT
|
On
6/16/2008 3:08:04 PM
Sahil Malik
said ..
Kien - excellent tip! Thanks
|