Move or Copy SharePoint Document Library Files Programmatically
In this post I published some code that took a look at using FrontPage Server Extentions to upload a file to a document library where the requirement was to be able to save metadata along with the document and not use the API. Here are a few more static methods that can be inserted into that code to provide simple file moving or copying functionality from a remote client. Documentation about the move RPC method can be found here. To copy rather than move the file, change the doCopy flag to true. An alternative method for remotely moving a file from one document library to another location is to use the copy web service which is documented on MSDN here. For local SharePoint calls, the appropriate API method is SPListItem.CopyTo or SPFile.MoveTo.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("\nmessage=successfully"
) <>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 <>{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;}}
0 comments:
Post a Comment