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.
Leave a comment