I have a Sharepoint deployment with the following hierarchy :

http://myserver.com:23456/ - SiteCollection as well as root site. http://myserver.com:23456/Archive - An archive site within the site collection.

Within the second archive site I have the following hierarchy within a document library DocLib : Folder1/Folder2/

I want to copy a SPFile instance from a document library within http://myserver.com:23456/ to the Folder2 within the archive site using the SPFile.CopyTo() command. This appears to be feasible based on the answer provided at this SO discussion.

If I attempt the copy operation using the following code snippet, I get an SPException that Folder2 cannot be found.

SPFile fileObj;
SPFolder destFolder;  //points to myserver.com:23456/Archive/DocLib/Folder1/Folder2
fileObj.CopyTo(destFolder.ServerRelativeUrl + "/" +  fileObj.Name);

I have verified that Folder2 exists. I am curious to know if I am doing the right thing here.

  • Check the destination folder server relative url value, check whether it already has "/".
    – Unnie
    Commented Oct 13, 2014 at 7:45

1 Answer 1


Since you have the SPFile object and the SPFolder object you can also do SPFolder.Add and pass in the SPFile name and filestream instead if you are simply copying the file over. The discussion link you provided is for an SPListItem whereas you appear to be copying the SPFile object - that may be your issue. But, saying that, my initial advice will work and pretty much the same as the second example in the link you provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.