0

Following is my code (Console Application) which I am using to copy a file from a site collection to anohter. When I run the code again it says a file with name .. already exists in Style library but I cannot see the file when I refresh the page. and I think something is wrong.

using (SPSite site = new SPSite("http://root.contoso.com/"))
{
    using(SPWeb web = site.OpenWeb())
    {
       SPFileCollection collFile= web.Lists["Style Library"].RootFolder.Files;
       using (SPSite s = new SPSite("http://root.contoso.com/sites/test"))
       {
           using (SPWeb w = s.OpenWeb())
           {
               w.AllowUnsafeUpdates = true;
               SPList l = w.Lists["Style Library"];
               foreach (SPFile f in collFile)
               {
                   SPFile u = l.RootFolder.Files.Add(l.RootFolder.Url + "/" + f.Name, f.OpenBinary(), true);
                    l.RootFolder.Update();
                    l.Update();
                    w.Update();
              }
             w.AllowUnsafeUpdates = false;
           }
       }
   }
}
1

You might not have permissiosns to see that file. You should also add a check for file existence like :

SPFile u = l.RootFolder.Files.Add(l.RootFolder.Url + "/" + f.Name, f.OpenBinary(), true);
 if (l.Exists)
 {
    l.RootFolder.Update();
    l.Update();
    w.Update();
}
0

Style library is system library so pretty much sure both site collection having same amount of files with same name. that's why you facing the issue.

Here is, power shell script on MSFT technet gallery which copy a library from one site to other. http://gallery.technet.microsoft.com/Copy-all-SharePoint-Files-0999c53f.

One thing i want to ask, why you want to copy from style library to other site collection library?

  • the reason I want to copy is, The site collection is created and I have to copy Theme files to theme directory and style library to apply theme to newly created site collections. – Medes Jun 22 '14 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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