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
SharePoint MVP
Published On :   07 Jul 2017
Visit Count
Today :  2    Total :   593
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


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.

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

SharePoint Analytics