private void RemoveDocument(string documentName)
{
string requestUrl = this.ctx.Url + "/_vti_bin/_vti_aut/author.dll";
string method = GetEncodedString("remove documents:15.0.0.4420");
string serviceName = GetEncodedString(ctx.Web.ServerRelativeUrl);
string url_List = GetEncodedString(String.Concat("[", documentName, "]"));
rpcCallString = "method={0}&service_name={1}&url_list={2}\n";
rpcCallString = String.Format(rpcCallString, method, serviceName, url_List).Replace("_", "%5f");
HttpWebRequest wReq = WebRequest.Create(requestUrl) as HttpWebRequest;
wReq.Method = "POST";
wReq.Headers["Content"] = "application/x-vermeer-urlencoded";
wReq.Headers["X-Vermeer-Content-Type"] = "application/x-vermeer-urlencoded";
wReq.UserAgent = "FrontPage";
wReq.UseDefaultCredentials = false;
Uri targetSite = new Uri(this.ctx.Web.Url);
SharePointOnlineCredentials spCredentials = (SharePointOnlineCredentials)this.ctx.Credentials;
string authCookieValue = spCredentials.GetAuthenticationCookie(targetSite);
wReq.CookieContainer = new CookieContainer();
wReq.CookieContainer.Add(
new Cookie("FedAuth",
authCookieValue.TrimStart("SPOIDCRL=".ToCharArray()),
String.Empty,
targetSite.Authority));
using (IO.Stream requestStream = wReq.GetRequestStream())
{
byte[] rpcHeader = Encoding.UTF8.GetBytes(rpcCallString);
requestStream.Write(rpcHeader, 0, rpcHeader.Length);
requestStream.Close();
GetResponse(wReq);
}
}
private string GetResponse(HttpWebRequest webRequest)
{
string responseString = String.Empty;
using (WebResponse webResponse = webRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
{
responseString = reader.ReadToEnd();
byte[] fileBuffer = Encoding.UTF8.GetBytes(responseString);
}
}
if ((responseString.IndexOf("message=successfully") < 0) && (responseString.IndexOf("msg=Save Conflict") < 0))
{
throw new Exception(responseString);
}
return responseString;
}
Now, let’s evaluate this.
- Here we are using method “remove documents” and, “15.0.0.4420” is server extension version.
- Service name is server relative URL of your site.
- documentName is the name of the document to be deleted. For ex. if the doc ToBeDeleted.aspx resides in the root folder of the site then, documentName will be ToBeDeleted.aspx.
- For authentication, we’re using the CookieContainer of HTTPWebRequest.
Here is the example of how will you call this upload method.
RemoveDocument("ToBeDeleted.aspx");
One utility method has been used here for encoding of string. Here it is for your reference.
public string GetEncodedString(string sourceString)
{
if (!String.IsNullOrEmpty(sourceString))
{
return HttpUtility.UrlEncode(sourceString).Replace(".", "%2e").Replace("_", "%5f");
}
else
{
return sourceString;
}
}