Move file from one document library to another


Badge +1
Is there any walkthrough or code sample for moving a file from one sharepoint document library to another? I just can t make it work right.


// Set Credentials
SourceCode.K2SPUtilities.SPSUtilities SpsUtils = new SourceCode.K2SPUtilities.SPSUtilities();
SpsList.Credentials = SpsUtils.GetCredentials(Server);

// Decode Encoded String Into Byte Array
byte[] oByte = new byte[1];
K2Base64.K2Base64 K2B64 = new K2Base64.K2Base64();
oByte = K2B64.DecodeBase64((string) K2.ProcessInstance.DataFields["SpsFileURL"].Value);

// Call Web Service to Upload Document
if (! (SpsList.UploadDocument(Server, Site, Folder.ToString(), File.ToString(), oByte, true , ref ErrorMessage))) {
// Error Occurred in UploadDocument - Raise Error
throw new System.Exception(ErrorMessage);
}

3 replies

Badge +8
Hi,

The SharePoint 2003 Multiple Documents Event in K2.net Studio has a move operation, maybe you can look at the code for that and customize it to suit your needs?
Badge +1
I can move a file, but I guess there is a problem with encoding. I am trying to move txt file and after moving the txt file contains strange characters. Does anybody know how to work this out?
Badge +8
in your code you are not loading the file into oByte, only the url to the file, you must get the actual file in oByte by calling the SpsList.GetDocument method.
You don't have to encode/decode to move files from one Library to another, only when the file contents is contained in an XML field for example when you use the InfoPath attachment field to store the actual file contents in an XML field.



;
string Server;
Server = oXmlNode.SelectSingleNode("server").InnerText;
if (! (Server.EndsWith("/"))) Server += "/";

// Set Url for Web Service
SpsList.Url = Server + "_vti_bin/K2SpsList.asmx";
// Set Credentials
SpsList.Credentials = SpsUtils.GetCredentials(Server);

// Call Web Service to Retrive the Document
if (! (SpsList.GetDocument(Server, oXmlNode.SelectSingleNode("site").InnerText, oXmlNode.SelectSingleNode("folder").InnerText,
oXmlNode.SelectSingleNode("docname").InnerText, ref oByte, ref ErrorMessage))) {
// Error Occurred in GetDocument - Raise Error
throw new System.Exception(ErrorMessage);
}

// Call Web Service to Upload Document
SpsList.Credentials = SpsUtils.GetCredentials(DestServer.ToString());

SpsList.Url = DestServer.ToString() + "_vti_bin/K2SpsList.asmx";
if (! (SpsList.UploadDocument(DestServer.ToString(), DestSite.ToString(), DestFolder.ToString(),
oXmlNode.SelectSingleNode("docname").InnerText, oByte, false , ref ErrorMessage))) {
// Error Occurred in UploadDocument - Raise Error
throw new System.Exception(ErrorMessage);
}

// Call Web Service to Delete Document
if (! (SpsList.DeleteDocument(oXmlNode.SelectSingleNode("site").InnerText, oXmlNode.SelectSingleNode("folder").InnerText,
oXmlNode.SelectSingleNode("docname").InnerText, ref ErrorMessage) )) {
// Error Occurred in DeleteDocument - Raise Error
throw new System.Exception(ErrorMessage);
}

}

Reply