Pages

Thursday, September 29, 2011

Create Site using Sharepoint webservices

Sharepoint is gaining traction every passing day and becoming more and more the defacto standard for collaboration and content management in Enterprise. Nowadays more and more business applications are moving into sharepoint along with Line of Business Applications. This is leading to sharepoint interfacing with variety of different applications, including SAP and other ERP systems and some times multiple Sharepoint farm. Most of the time people tend to create custom application or web services to perform basic operations of creating site or updating list item. There are times when we have to create custom web service to perform some operations and it is always case by case basis. In my experience so far i tend to not to create custom web services for some basic operations like provisioning a site or updating the list. Here I am going to show how simple is to use sharepoint web service to create site and also point out an error which causes panic with new developers consuming sharepoint webservice.

Creating site using shrepoint web service is a simple and straight forward task.

1. Add web reference to the Sharepoint Site.asmx web service.
2. use CreateWeb method on the service to create a web. If you want to create a sub site pass relative URL with parent and child site.
3.That's it the site is created.

But here come a problem. When ever you use Sharepoint web service to create site there is a bug in sharepoint site.asmx web service which will throw an SOAP exception. There is no need to panic as Microsoft did not fix this is Sharepoint 2010 as well. Just suppress the error and move on. when you go to the remote site collection you will see the site created though you got the exception. I wanted to write this down because every time a new resource who is new to sharepoint joins our organization and if he has to use site.asmx to create site in their project they get this error and often times they spend a lot of time trying to figure out why the error is happening either by playing around with parameters or trying to write a new web service to create a site.

In the code snippet I am creating a sub site at 3 levels deep hence I am passing relative url of the web that needs to be created. Replace the web name field with your name and remember web name can be different than the web tile. web title is the one that is displayed and web name is the one that forms the URL.  in the code I am writing to the event log so that I can copy the exception here. I forgot to copy it when I am writing this but I will add the exception ASAP. Code snippet also shows how to get tempalte using site.asmx and set it in. You can also refer to my blog about Web template here.

Code


SharepointSiteService.Sites siteWS = new SharepointSiteService.Sites();
                                siteWS.Credentials = new System.Net.NetworkCredential("user1", "password", "domain");
try 
{
                                       
                                            SharepointSiteService.Template[] templates;
                                            siteWS.GetSiteTemplates(1033, out templates);
                                            SharepointSiteService.Template template = (from SharepointSiteService.Template t
                                            in templates
                                                                                       where t.Title == "Test Template"
                                                                                       select t).FirstOrDefault();

siteWS.CreateWeb("<parent web name>" + "/" + <sub web name>" + "/" + <sub web name>", "Test web", "Test Web", template.Name, 1033, true, 1033, true, 1033, true, false, false, false, false, false, false); 
}
catch (Microsoft.SharePoint.SoapServer.SoapServerException e)
{
                                        System.Diagnostics.EventLog.WriteEntry("SharePoint Foundation", "soap exception" + e.Detail + e.Message + e.Source + e.Node);
}
catch (Exception ex)
{
                                        System.Diagnostics.EventLog.WriteEntry("SharePoint Foundation", "Site Created");
                                        System.Diagnostics.EventLog.WriteEntry("SharePoint Foundation", ex.Message + ex.Source + ex.StackTrace);
 }

2 comments:

  1. This code does help in createing a sub-site. However, the newly created sub-site never appears in the navigation. I have to manually type the URL in the browser to access the sub-site
    Unfortunately, since my sub-sites are programmatically created, users never know what would the name be.

    Do you know how to make the newly created sub-site appear in the navigation?

    ReplyDelete
    Replies
    1. @Ahmad:
      Unfortunately Sharepoint webservices are limited and exposes only certain operation as methods. But you can still achieve this in one the many ways described here

      1. if you are in 2010 you can use web provisioned event to add a top nav. To see how you can use Web provisioned event please refer to my blog here
      2. If you are in 2007 then you can either run a timer that checks the top nav for the newly created site if it does not exists then you can add it to the list.
      3. you can manually set the navigation option
      Make sure you enable display sub site in your root site to show top nav.
      you can use the following code to add TOP Nav

      SPNavigationNode node = new SPNavigationNode("New Nav", web.Url);

      SPNavigationNodeCollection topNav = web.Navigation.TopNavigationBar;
      web.AllowUnsafeUpdates = true;
      site.AllowUnsafeUpdates = true;
      SPNavigationNode topNavNode = web.Navigation.GlobalNodes.AddAsLast(node);
      topNavNode.IsVisible = true;
      topNav[0].Children.AddAsLast(node);
      web.Update();
      web.AllowUnsafeUpdates = false;
      site.AllowUnsafeUpdates = false;

      you can have this code run in web provisioned event to add top nav in 2010 which i would suggest is the better and cleaner way of doing.

      Delete