Question

Situation:
One WebApplication, two site collections. Both site collections have the same "type" and data structure. Both collections use a document library with the same structure.

Problem/Question:
I have to implement the ability to copy one document from collection A to collection B. When I try the following...

SPFile destfile = destPageList.RootFolder.Files.Add(sourcePage.File.ServerRelativeUrl, sourcePage.File.OpenBinary());

...I get an cxception that there exists no file with the given URL in this Web. The URL is correct, so I'm wondering why this is happen? Do you have any hints or other ways to solve this issue?

Other Infos:
I'm using VisualStudio 2010, .NET 3.5, Sharepoint2010 Server.

Edit:
I need to achieve this programmatically! A content deployment is (for some reasons) no usable solution.

Was it helpful?

Solution

You cannot use Files collection in one web to add file to another web application. Instead you should open source file, then open destination site and web and use its Files collection to create new file in destination web. Something like:

byte[] sourceFileContent = sourcePage.File.OpenBinary();
using(SPSite site = new SPSite("http://mysite2"))
{
    using(SPWeb web = site.OpenWeb("/"))
    {
        SPList list = web.Lists["My List"];
        list.RootFolder.Files.Add(destFileUrl, sourceFileContent);
    }
}

BTW, to copy file between sites it is better to use export/import, like described here: http://www.spsamples.com/2011/07/moving-sharepoint-list-items-to-archive.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top