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");
{
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);
}
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);
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);
}