Move a document within a Site Collection in SharePoint Office 365 using C# Patterns and Practices


Sathish Nadarajan
SharePoint MVP
Published On :   18 Jun 2017
Visit Count
Today :  3    Total :   396
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


Today, I met with a requirement to move the documents from one Document Library to another Document Library. It is a kind of event receiver, which we need to do from a Console application. It was a one line code, which will move from the Source Document Library to Target Document Library along with the Metadata Information as well. It is really a single line of code to do this. Hence, thought of making it handy to the community.

The Scenario is, I have a Site Collection with two Document Libraries.

1. MySourceDocLib

2. MyTargetDocLib

The File from Source Doc Lib needs to be moved to the Target Doc Lib with the Metadata. Both the Libraries are having a Content Type MyContentType.

 
 using Microsoft.SharePoint.Client;
 using OfficeDevPnP.Core.Framework.Provisioning.Connectors;
 using OfficeDevPnP.Core.Framework.Provisioning.Providers.Xml;
 using System.IO;
 using System.Net;
 using System.Text;
 
 namespace Office365.Console
 {
     class Program
     {
         static void Main(string[] args)
         {
             MoveDocumentBetweenLibraries();
         }
 
         public static void MoveDocumentBetweenLibraries()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://*******.sharepoint.com/sites/communitysite";
             string userName = "Sathish@*****.onmicrosoft.com";
             string password = "***********";
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = clientContext.Web;
                 clientContext.Load(web);
                 clientContext.Load(web.Lists);
                 clientContext.ExecuteQuery();
 
                 List sourceList = web.Lists.GetByTitle("MySourceDocLib");
                 clientContext.Load(sourceList);
                 clientContext.ExecuteQuery();
 
 //As an example, I am taking a Document with the ID 3.  This depends on the requirement.
                 ListItem listItem = sourceList.GetItemById(3);
                 clientContext.Load(listItem);
                 clientContext.ExecuteQuery();
 
                 listItem.File.MoveTo("https://*****.sharepoint.com/sites/CommunitySite/MyTargetDocLib/File1.txt", MoveOperations.Overwrite);
                 
                 clientContext.ExecuteQuery();
             }
         }
 
     }
 }
 

And one interesting thing is, even if the Target Library does not have the Content Type, then the document is getting moved without the Metadata Information. That was really amazing!!!!

In the next article, let us see, how to move between Site Collections. That’s also an interesting Scenario. Let me explain the scenario and the solution in the next article.

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Forms App