Search

Rss Posts

Rss Comments

Login

 

Posts from January, 2010

How to copy files across SharePoint libraries.

Jan 22

Yesterday, I was working on a SharePoint Workflow. I needed to copy the final InfoPath (xml) to a different library. My first thought was to use the spfile.copyto but that gave me the “Value does not fall within the expected range.” or COM errors.
Instead of wasting my time trying to figure out how to make the copyto  method copy across site collections, I did the following.
   

SPFile file = workflowProperties.Item.File;
string xmlFileContent = System.Text.Encoding.Default.GetString(file.OpenBinary());
int index = xmlFileContent.IndexOf(”<?xml”);
if (index != 0)
    xmlFileContent = xmlFileContent.Remove(0, index);
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlFileContent);
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace(”my”, @”http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-10-05T17:30:02”

 I needed to set the status before I move to the new location.

XmlNode nodeStatus = doc.DocumentElement.SelectSingleNode(“/my:myFields/my:FormStatus”, mgr);
nodeStatus.InnerText = status;
char[] newFile = doc.InnerXml.ToCharArray();
file.SaveBinary(System.Text.Encoding.Default.GetBytes(newFile));
Stream s = file2.OpenBinaryStream();
SPFolder target = curweb.GetFolder(“Contract”);
SPFile ofile = curweb.GetFile(“Contract/” + _requestName + “.xml”);
if (ofile.Exists)
    ofile.Delete();
target.Files.Add(_requestName + “.xml”, s);
fileURLWorkSpace = thedestinationlocation-URL + _requestName + “.xml”;
target.Update(

 Hope this helps someone.