Wednesday, September 19, 2012

SharePoint: How to move files from one document library to other using RPC in Silverlight application. Presrve created and modified information.

In my last post I talked about Uploading large files to SharePoint using RPC call. In this post I am going to tell how to move or copy file from one document library to other.
For basics of RPC, please follow links given in my last post.
So, here is my code which uses RPC method to copy file.

private string rpcCallString = string.Empty;

private byte[] newFileData;

public void MoveFile(string CurrentWebUrl, string serviceName, string newFilePath, string oldFilePath)

{
string requestUrl = CurrentWebUrl + "_vti_bin/_vti_aut/author.dll";

 string method = GetEncodedString("move document:14.0.0.5123");

serviceName = GetEncodedString("/" + serviceName);

 string putOption = "edit";

oldFilePath = GetEncodedString(oldFilePath);

newFilePath = GetEncodedString(newFilePath);

rpcCallString =
"method={0}&service_name={1}&oldUrl={2}&newUrl={3}&rename_option=findbacklinks&put_option={4}&docopy=false";

rpcCallString = String.Format(rpcCallString, method, serviceName, oldFilePath, newFilePath, putOption).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.BeginGetRequestStream(new AsyncCallback(gotFileMoveRequestStream), wReq);

}

private void gotFileMoveRequestStream(IAsyncResult asynchronousResult)

{HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;

 Stream requestStream = webRequest.EndGetRequestStream(asynchronousResult);

 byte[] fileData = Encoding.UTF8.GetBytes(rpcCallString);

requestStream.Write(fileData, 0, fileData.Length);

requestStream.Close();

webRequest.BeginGetResponse(
new AsyncCallback(gotFileMoveResponse), webRequest);

}

private void gotFileMoveResponse(IAsyncResult asynchronousResult)

{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;

 HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);

 Stream responseStream = webResponse.GetResponseStream();

 StreamReader reader = new StreamReader(webResponse.GetResponseStream());

 string responseString = string.Empty;

responseString = reader.ReadToEnd();byte[] fileBuffer = Encoding.UTF8.GetBytes(responseString);

responseStream.Close();

reader.Close();

webResponse.Close();
if (responseString.IndexOf("\n<p>message=successfully") < 0)

{throw new Exception(responseString);
 
}           
}

and here is the utility method.

public string GetEncodedString(string SourceString)

{
if (!string.IsNullOrEmpty(SourceString))

{
return HttpUtility.UrlEncode(SourceString).Replace(".", "%2e").Replace("_", "%5f");

}
 
else

{
return SourceString;

}

}


Let's discuss parameters now.
method is move document. 14.0.0.5123 is server extension version for SharePoint 2010.
oldUrl and newUrl are the paths of old and new files respectively.
rename_option is set to findbacklinks, which means all the references to the file will also be updated for new location.
Here is the parameter for which many like me are looking for. Parameter which will allow us to preserve created and modified information even if we copy or move file from one location to other. And the parameter used is put_option which when set to "overwrite", overwrites file with same name, when set to "edit" prompts with message saying file with same name exists and IF SET TO "migrationsemantics" PRESERVES CREATED and MODIFIED INFORMATION.
If we set docopy to true it will only copy file to new location but if it is set to false it will move file from old location to new location. Default value for it is false.
Here is example of call to above method.
 


MoveFile("http://myserver/sites/prasad/teamsite/", "sites/prasad/teamsite", "Shared Documents/document1.docx", "MyDocLib/document1.docx");

Remember the way paths are provided. It is very important.
For more information about "
move document" method visit MSDN.
This method will be very useful when you want to migrate data from one site to another.
So are you ready to use this method for your next migration task???

No comments:

Post a Comment