0

I have to create a SharePoint timer job which will copy documents from a document library on a SharePoint site to a second SharePoint site which is on a completely different network. There is firewall between the 2 networks (the first network where the file exists and second network where the file needs to be copied to) Both sites are on SharePoint 2013 on prem.

can anyone give some ideas on how to do this? Thanks in advance..

I don't necessarily have to use a timer job if there is some other netter alternative to accomplish this. But the task needs to run periodically on a given schedule .

2

We can achieve this by writing a Console Application using CSOM Which will read the data from one SharePoint site and move to other SharePoint site collection.

and we can schedule this to run in a particular interval using Windows Task Scheduler.

as CSOM is enabled in SharePoint 2013 by default it can be run from any machine only authentication to the respective sites is needed.

kindly find the sample application with code below :

 public static void MoveDocuments()
    {
        ClientContext sourceContext = new ClientContext("source site url");
        ClientContext destinationContext = new ClientContext("destination site url");

        try
        {
            List SourceDocuments = sourceContext.Web.Lists.GetByTitle("SrcDocLibName");
            CamlQuery query = CamlQuery.CreateAllItemsQuery();

            List DestinationDocuments = destinationContext.Web.Lists.GetByTitle("destnLibName");

            destinationContext.Load(destinationContext.Web);
            destinationContext.ExecuteQuery();

            ListItemCollection itemcollection = SourceDocuments.GetItems(query);
            sourceContext.Load(itemcollection);
            sourceContext.ExecuteQuery();

            foreach(var Doc in itemcollection)
            {
                File file = Doc.File;
                sourceContext.Load(file);
                sourceContext.ExecuteQuery();                                      

                string fileName = file.Name;


                string nLocation = destinationContext.Web.ServerRelativeUrl.TrimEnd('/') + "/" + "destnLibName" + "/" + file.Name;

                // read the file, copy the content to new file at new location

                FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(sourceContext, file.ServerRelativeUrl);

                Microsoft.SharePoint.Client.File.SaveBinaryDirect(destinationContext, nLocation, fileInfo.Stream, true);

                Console.WriteLine("Copying Document ... " + fileName);                 
            }

        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.Read();
        }

    }

Hope this helps. this will be easy to handle as compared to writing a Timer Job which needs proper administrative tasks.

1

I would use Powershell and windows task scheduler to achieve this.

Just write a PowerShell script which downloads the document to your hard drive first and run another PowerShell script which will upload the document from hard drive to another SharePoint site. you can schedule this PowerShell scripts using windows task scheduler.

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.