Skip to main content

Cloud

Use RPC to create a SharePoint Sub Site

I am working on an internal project that requires me to create and delete sites from a remote SharePoint site. My first thoughts were "this will be easy I will use web services to do it". Well I was wrong, the admin service only deals with site collections and not sub sites, so I had to do it the old fashioned way…RPC. I read a blog when I was looking for an example that said SharePoint designer uses RPC. I then started fiddler and SPD and created a site and deleted a site. This gave me everything I needed to make my RPC call. Since there are few examples out there on how to do this, I thought I would provide some actual working ones. Below are code examples for creating and deleting SP sites:
First is the method for creating the site:

private static string CreateSiteRequest(string parentSiteURL, string newSiteRelativeURL, NetworkCredential credential, string spVersion)

{

HttpWebRequest objRequest = (HttpWebRequest)HttpWebRequest.Create(parentSiteURL + "/_vti_bin/_vti_adm/admin.dll");

objRequest.Method = WebRequestMethods.Http.Post;

objRequest.Credentials = credential;

objRequest.Headers.Add("X-Vermeer-Content-Type", "application/x-www-form-urlencoded");

objRequest.ContentType = "application/x-www-form-urlencoded";

StreamWriter sw = new StreamWriter(objRequest.GetRequestStream());

sw.Write(string.Format("method=create service:{0}&service_name={1}&flags=0&lcid=0", spVersion, newSiteRelativeURL));

sw.Close();

HttpWebResponse objResponse = objRequest.GetResponse() as HttpWebResponse;

StreamReader sr = new StreamReader(objResponse.GetResponseStream());

sr.Close();

objResponse.Close();

return string.Empty;

}

After the site has been created, we need to apply the template to the site:

private static string ApplyTemplate(string newSiteAbsoluteURL, NetworkCredential credential, string templateName)

{

HttpWebRequest objRequest = (HttpWebRequest)HttpWebRequest.Create(newSiteAbsoluteURL + "/_vti_bin/owssvr.dll?Cmd=DisplayPost");

objRequest.Method = WebRequestMethods.Http.Post;

objRequest.Credentials = credential;

objRequest.Headers.Add("X-Vermeer-Content-Type", "application/xml");

objRequest.ContentType = "application/xml";

StreamWriter sw = new StreamWriter(objRequest.GetRequestStream());

sw.Write(string.Format("<?xml version="1.0" encoding="UTF-8"?><ows:Batch OnError="Return" Version="12.0.0.000"><Method ID="0,SiteProvision"><SetVar Name="Cmd">SiteProvision</SetVar><SetVar Name="CreateLists">True</SetVar><SetVar Name="SiteTemplate">{0}</SetVar></Method></ows:Batch>", templateName));

sw.Close();

HttpWebResponse objResponse = objRequest.GetResponse() as HttpWebResponse;

StreamReader sr = new StreamReader(objResponse.GetResponseStream());

sr.Close();

objResponse.Close();

return string.Empty;

}

There is some other meta data that can get set, but the purpose of this blog is to provide a good example on creating sites via RPC.

Below illustrated the code on setting the title for a site:

private static void SetSiteTitle(string newSiteAbsoluteURL, NetworkCredential credential, string siteTitle)

{

HttpWebRequest objRequest = (HttpWebRequest)HttpWebRequest.Create(newSiteAbsoluteURL + "/_vti_bin/owssvr.dll?Cmd=DisplayPost");

objRequest.Method = WebRequestMethods.Http.Post;

objRequest.Credentials = credential;

objRequest.Headers.Add("X-Vermeer-Content-Type", "application/xml");

objRequest.ContentType = "application/xml";

StreamWriter sw = new StreamWriter(objRequest.GetRequestStream());

sw.Write(string.Format("<?xml version="1.0" encoding="UTF-8"?><ows:Batch OnError="Return" Version="12.0.0.000"><Method ID="0,UpdateProject"><SetVar Name="Cmd">UpdateProject</SetVar><SetVar Name="NewListTitle">{0}</SetVar><SetVar Name="owshiddenversion">-1</SetVar></Method></ows:Batch>", siteTitle));

sw.Close();

HttpWebResponse objResponse = objRequest.GetResponse() as HttpWebResponse;

StreamReader sr = new StreamReader(objResponse.GetResponseStream());

sr.Close();

objResponse.Close();

}

Finally, here is how you delete a site:

public static bool DeleteSite(string parentSiteURL, string relativeURLToDelete, NetworkCredential credentials, string spVersion)

{

HttpWebRequest objRequest = (HttpWebRequest)HttpWebRequest.Create(parentSiteURL + "/_vti_bin/_vti_adm/admin.dll");

objRequest.Method = WebRequestMethods.Http.Post;

objRequest.Credentials = credentials;

objRequest.Headers.Add("X-Vermeer-Content-Type", "application/x-www-form-urlencoded");

objRequest.ContentType = "application/x-www-form-urlencoded";

StreamWriter sw = new StreamWriter(objRequest.GetRequestStream());

sw.Write(string.Format("method=remove service:{0}&service_name={1}&flags=default", spVersion, relativeURLToDelete));

sw.Close();

HttpWebResponse objResponse = objRequest.GetResponse() as HttpWebResponse;

StreamReader sr = new StreamReader(objResponse.GetResponseStream());

sr.Close();

objResponse.Close();

return true;

}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.