Check whether a document is already a Record or Not (IsRecord) in SharePoint Office 365 programmatically using C# CSOM


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

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


In the earlier article, we saw how to Declare and UnDeclare a document as record. But, the problem is, if already a document is Record, and again, if we are trying to declare it as a record, the API is not intelligent enough to handle that. This throws the exception.

Hence, the situation comes like, before declaring or undeclaring a record, we need to make sure that the document is a Record or not.

Luckily (☹) , PNP give a method for that as well. IsRecord.

 Records.IsRecord(file.Context, file.ListItemAllFields);

But, I am not sure, why it is not working. It always returns false for me. Probably, I may be doing something wrong in implementing this method. But, with a little thought process, there are some work arounds as well.

The Record declaration settings has been stored on the Property Bag Values of the List Item. There are two property Bag Values, by which, we can decide whether a particular item is a RECORD or not.

1. _vti_ItemHoldRecordStatus

2. _vti_ItemDeclaredRecord

Let us see, how to use this.

 using Microsoft.SharePoint.Client;
 using Microsoft.SharePoint.Client.RecordsRepository;
 using System;
 
 namespace Office365.Console
 {
     class Program
     {
         static void Main(string[] args)
         {
             IsRecord();
         }
 
         public static void IsRecord()
         {
             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(6).File;
             clientContext.Load(file);
             clientContext.Load(file.Properties);
             clientContext.Load(file.ListItemAllFields);
             clientContext.Load(file.ListItemAllFields.Properties);
             clientContext.ExecuteQuery();
 
             Records.IsRecord(file.Context, file.ListItemAllFields);
 
             string recordStatus = string.Empty;
             if (file.ListItemAllFields.Properties.FieldValues.ContainsKey("_vti_ItemHoldRecordStatus"))
             {
                 switch (Convert.ToInt32(file.ListItemAllFields.Properties["_vti_ItemHoldRecordStatus"]))
                 {
                     case 0:
                         recordStatus = "Normal Document";
                         break;
                     case 4353:
                         recordStatus = "Hold";
                         break;
                     case 273:
                         recordStatus = "Record";
                         break;
                     default:
                         recordStatus = string.Empty;
                         break;
                 }
             }
 
             if (file.ListItemAllFields.Properties.FieldValues.ContainsKey("_vti_ItemDeclaredRecord"))
             {
                 switch (Convert.ToDateTime(file.ListItemAllFields.Properties["_vti_ItemDeclaredRecord"]))
                 {
                     default:
                         recordStatus = string.Empty;
                         break;
                 }
             }
         }
 
     }
 }
 

Now, let us see a brief about the two properties. There is nothing much or complicated with these two properties.

1. _vti_ItemHoldRecordStatus

· This is an integer having a possibility of three values

o 0 – Normal Document

o 4353 – Hold

o 273 – Record

· Based on this value, we can decide, whether it is a Record or not.

· This property is a Hidden one. Hence, while using this in Search Queries, we need to be a bit careful.

2. _vti_ItemDeclaredRecord

a. This is a datetime.

b. This key holds the value of the date on which the Item is declared as Record.

c. If the item is not declared, then this property itself will not be available.

d. If an Item is declared, then definitely this key contains the value of a date.

Hope this helps to identify a record.

Happy Coding,

Sathish Nadarajan.

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

Migratiin Tools for SharePoint