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

Sathish Nadarajan
 
Solution Architect
June 18, 2017
 
Rate this article
 
Views
4201

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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment