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