Friday, 11 January 2013

Copy Document from One Site Collection to other in SharePoint 2010



The following code copies the document to the library of Destination Site Collection from the library of source Site Collection. The document from source is first compared with the document names of the destination & if the document does not match then only the documents are added to the Library of the Destination Site.


/// CopyToDocLib is a method to Copy Document from Source Library to Destination Library of Source SiteURl to Destination Site URL
/// <param name="sourcesiteUrl">Pass Site URL of Source SiteCollection</param>
/// <param name="destinationsiteUrl">Pass Site URL of Destination SiteCollection</param>
/// <param name="Source">Pass Source Library Name</param>
/// <param name="Destination">Pass Destination Library Name</param>
public void CopyToDocLib(string sourcesiteUrl, string destinationsiteUrl, string Source, string Destination)
{
  SPQuery qry = new SPQuery();
  string camlquery = string.Empty;
           
  SPListItemCollection Srccollitem;
  string filename = string.Empty;

  try
  {
    using (SPSite rootSite = new SPSite(sourcesiteUrl))   //Get Source Site
    {

      using (SPWeb spWeb = rootSite.OpenWeb())    //Open Source Site
      {
       
        //Query to get items according to descending order
        camlquery = "<OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy>";
        qry.Query = camlquery;
       
        ///Get the Collection of SourceLibraryDocument by Caml Query
        Srccollitem = spWeb.Lists[Source].GetItems(qry);
                      
        string count = Srccollitem.Count.ToString();    //Get count of items
        using (SPSite destSite = new SPSite(destinationsiteUrl))  //Get Destination Site
          {

            using (SPWeb spWebdest = destSite.OpenWeb())    //Open Destination Site
            {
              string destlibUrl = string.Empty;

              //Get the Destination Library
              SPFolder destlib = spWebdest.Folders[Destination];
             //Get the Absolute URL of the Destination Library
              destlibUrl = destinationsiteUrl + "/" + destlib;

              ///Start of ForEach Loop to Copy Document one by one from Library of Source Site to Library of Destination Site.
             foreach (SPListItem sourceitem in Srccollitem)
             {
               SPQuery chkquery = new SPQuery();
               //Query to get items of destination library according to descending order

               chkquery.Query = "<OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy>";
               SPListItemCollection destinationlib = spWebdest.Lists[Destination].GetItems(chkquery);
               string destcount = destinationlib.Count.ToString();
               int cnt=0;
               string destlistitemname=string.Empty;

               //Get Document Name
               string sourcelistitemname = sourceitem.File.Name;

               //Start the ForEach Loop on Destination Library items to check the document already exists
               foreach (SPListItem destitem in destinationlib)
               {
               //Get Destination Document Name
                 destlistitemname = destitem.File.Name;

               //Compare the Source Document Name with all the Destination Document  names one by one
                 if (destlistitemname == sourcelistitemname)
                 {
                    cnt++;
                   //If Document Name already exists, go to next item
                    break;
                 }

               }

               //If document with the same name does not exists then copy the document from source site library to destination site library
               if (cnt == 0)
               {
                 spWebdest.AllowUnsafeUpdates = true;
               //Add document to destination library
                 destlib.Files.Add(sourceitem.File.Name, sourceitem.File.OpenBinary(), true);
                 destlib.Update();
                
                                       
               }
               else
               {
                 Console.WriteLine("The list item " + destlistitemname + " already exists in the library " + Destination + " in Site " + destSite);
               }
             }
          }
        }
        ///End of ForEach Loop

      }
    }
  }
  catch (Exception ex)
  {
 
  }
}

No comments:

Post a Comment