Update: You can download a comprehensive c# class library to automate RPC calls - including moving files (even between web sites or servers). See this blog post for more information.
public static bool Move(string oldUrl, string newUrl)
{
string result = "";
string webUrl = GetWebURL(oldUrl);
oldUrl = oldUrl.Substring(webUrl.Length + 1);
newUrl = newUrl.Substring(webUrl.Length + 1);
return Move(webUrl, oldUrl, newUrl, out result);
}
public static bool Move(string webUrl, string oldUrl, string newUrl, out string result)
{
EnsureFolders(webUrl, newUrl);
string renameOption = "findbacklinks";
string putOption = "overwrite,createdir,migrationsemantics";
bool doCopy = false;
string method = "method=move+document%3a12.0.4518.1016&service_name=%2f&oldUrl={0}&newUrl={1}&url_list=[]&rename_option={2}&put_option={3}&docopy={4}";
method = String.Format(method, oldUrl, newUrl, renameOption, putOption, doCopy.ToString().ToLower());
try
{
using (WebClient webClient = new WebClient())
{
webClient.Credentials = CredentialCache.DefaultCredentials;
webClient.Headers.Add("Content-Type", "application/x-vermeer-urlencoded");
webClient.Headers.Add("X-Vermeer-Content-Type", "application/x-vermeer-urlencoded");
result = Encoding.UTF8.GetString(webClient.UploadData(webUrl + "/_vti_bin/_vti_aut/author.dll", "POST", Encoding.UTF8.GetBytes(method)));
if (result.IndexOf("\n<p>message=successfully") < 0)
throw new Exception(result);
}
}
catch (Exception ex)
{
result = ex.Message;
return false;
}
return true;
}
public static void EnsureFolders(string rootUrl, string folderUrl)
{
StringBuilder sb = new StringBuilder(rootUrl.TrimEnd('/'));
string[] segments = folderUrl.Split('/');
for (int i = 0; i < segments.Length - 1; i++)
{
sb.Append("/");
sb.Append(segments[i]);
CreateFolder(sb.ToString());
}
}
public static bool CreateFolder(string folderURL)
{
try
{
WebRequest request = WebRequest.Create(folderURL);
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "MKCOL";
WebResponse response = request.GetResponse();
response.Close();
return true;
}
catch (WebException)
{
return false;
}
}