How to create new sub site in SharePoint 2010 using web services

sharepoint

In SharePoint 2010, there is a new method CreateWeb in the Sites web service with the purpose to create new SharePoint sub sites. There are, however, a lot of issues with it – well, that is my experience at least. Here is the code that should utilize the web service and its method properly:

Uri site = new Uri("http://sp2010site/"); 
string webServicePath = "_vti_bin/Sites.asmx";
string webServiceUrl = Path.Combine(site.AbsoluteUri, webServicePath);

Sites webService = new Sites();
webService.Credentials = CredentialCache.DefaultNetworkCredentials;
webService.Url = webServiceUrl;

//the following line creates the sub site
string result = webService.CreateWeb("newsite", "New Site", "Site desc", "STS#0", 
    1033, true, 1033, true, 1033, true, true, true, true, true, true, true);

The following code returns Soap exception if something wrong happened (e.g. a sub site with the address "newsite" already exists, or the specified template doesn't exist).

If everything is ok, an InvalidOperation exception is fired with the message "There is an error in XML document (1, 310).", but the site is actually created!

If everything is ok, but I use my own solution instead of the non-default SharePoint template (such as the Team site, i.e. STS#0), I get a Soap exception and the site does not get created.

This has been a terrible experience so far. Please post your experiences with the sub site creation in SP 2010, and even better, post your resolutions to these problems if you have any. Cheers all!

Best Answer

Steps in SP2010 beta something to create a [sub] site from a [custom] template:

  • Activate your site template
  • Browse to betasoftwaresucks/_layouts/templatepick.aspx
  • Click on the "Custom" tab. You should be able to see your template
  • Open up a tool that can inspect the DOM (e.g. IE Developer Tools or Firebug). The DOM is modified dynamically so just viewing the HTML just won't work unless you're interested in one of the default templates in the default selected tab.
  • Find the value of the option element representing your template. It will look like "{guid}#templatename". Make sure the "Custom" tab is still open or you might not be able to find this.
  • Use that value as the template parameter to CreateWeb.

[I roll my SP SOAP "by hand" so it's trivial to see the full request and response.]

Related Topic