How to Convert a document into Record in a SharePoint Office 365 Record Centre Site Programmatically Using C# Client Site Object Model (CSOM)

Sathish Nadarajan
 
Solution Architect
July 7, 2017
 
Rate this article
 
Views
3718

In this article, let us see how to convert a document into record in Office 365 using C# CSOM. In one of the earlier article we saw various options regarding the record declaration settings. I request the readers to refer that once, if they have not done already.

Now, the piece of code to declare and undeclare a document as record and vice versa is as follows.

 using Microsoft.SharePoint.Client;
 using Microsoft.SharePoint.Client.RecordsRepository;
 
 namespace Office365.Console
 {
     class Program
     {
         static void Main(string[] args)
         {
             DeclareUnDeclareAsRecord();
         }
 
         public static void DeclareUnDeclareAsRecord()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string sourceSiteUrl = "https://*******.sharepoint.com/sites/RecordCentre";
 
             string userName = "Sathish@*******.onmicrosoft.com";
             string password = "*******";
 
             var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(sourceSiteUrl, userName, password);
 
             Web web = clientContext.Web;
             clientContext.Load(web);
             clientContext.Load(web.Lists);
             clientContext.ExecuteQuery();
 
             List list = web.Lists.GetByTitle("MyRecordLibrary");
             clientContext.Load(list);
             clientContext.ExecuteQuery();
 
             File file = list.GetItemById(4).File;
             clientContext.Load(file);
             clientContext.ExecuteQuery();
 
 
             Records.UndeclareItemAsRecord(file.Context, file.ListItemAllFields);
             clientContext.Load(file);
             clientContext.ExecuteQuery();
 
 
             Records.DeclareItemAsRecord(file.Context, file.ListItemAllFields);
             clientContext.Load(file);
             clientContext.ExecuteQuery();
 
 
 
             clientContext.ExecuteQuery();
         }
 
     }
 }
 

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