Get ModerationStatus of document using SharePoint client managed object model (csom)

We can retrieve ModerationStatus of a document using .Net managed object model csom code in SharePoint online. The same code also will work to retrieve _moderationstatus SharePoint 2013 and SharePoint 2016.

Here I have a SharePoint Online document library and I want to retrieve the ModerationStatus for a particular item using client object model. I am filtering record whose ID is 10.

Moderation Status is a 4-byte integer indicating the moderation approval status of a list item. Configurations can require moderation approval to publish a list item or allow automatic approval. A published list item MUST have a Moderation Status of 0.

The following are all possible valid values for Moderation Status.

ValueDescription
0The list item is approved.
1The list item has been denied approval.
2The list item is pending approval.
3The list item is in the draft or checked out state.
4The list item is scheduled for automatic approval at a future date.

GetModerationStatus of document in SharePoint using CSOM

Below is the full code to retrieve ModerationStatus of document in a SharePoint Online document library.

public string GetItemApprovalStatus(string URL)
{
string status = string.Empty;
using (ClientContext clientContext = new ClientContext(URL))
{
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(),GetSPOSecureStringPassword());
List oList = clientContext.Web.Lists.GetByTitle("MyTestList");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='ID'/><Value Type = 'Int' > 10 </Value ></Eq></Where></Query></View>";
ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(collListItem);
clientContext.ExecuteQuery();
foreach (ListItem oListItem in collListItem)
{
string NodeStatus = oListItem["_ModerationStatus"].ToString();
if (NodeStatus == "0")
{
status = "Approved";
}
else if (NodeStatus == "1")
{
status = "Denied";
}
else if (NodeStatus == "2")
{
status = "Pending";
}
else if (NodeStatus == "3")
{
status = "Draft";
}
else if (NodeStatus == "4")
{
status = "Scheduled";
}
}
}
return status;
}
private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings["SPOAccount"];
}
catch
{
throw;
}
}
private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings["SPOPassword"])
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}

Read some SharePoint 2013 csom tutorials:

I hope this will be helpful to retrieve ModerationStatus of a document in a SharePoint Online document library.

>