privatestaticstring 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 = newStreamWriter(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() asHttpWebResponse;
StreamReader sr = newStreamReader(objResponse.GetResponseStream());
sr.Close();
objResponse.Close();
returnstring.Empty;
}
After the site has been created, we need to apply the template to the site:
privatestaticstring 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 = newStreamWriter(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() asHttpWebResponse;
StreamReader sr = newStreamReader(objResponse.GetResponseStream());
sr.Close();
objResponse.Close();
returnstring.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:
privatestaticvoid 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 = newStreamWriter(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() asHttpWebResponse;
StreamReader sr = newStreamReader(objResponse.GetResponseStream());
sr.Close();
objResponse.Close();
}
Finally, here is how you delete a site:
publicstaticbool 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 = newStreamWriter(objRequest.GetRequestStream());
sw.Write(string.Format("method=remove service:{0}&service_name={1}&flags=default", spVersion, relativeURLToDelete));
sw.Close();
HttpWebResponse objResponse = objRequest.GetResponse() asHttpWebResponse;
StreamReader sr = newStreamReader(objResponse.GetResponseStream());
sr.Close();
objResponse.Close();
returntrue;
}