How to retrieve ModerationStatus of document using .Net managed object model csom code in SharePoint online?

Here we will discuss how 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.

Here I have a document library and I want to retrieve the ModerationStatus for a particular item. I am filtering record whose ID is 10.

SharePoint deveopment training course

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.

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



Below is the full code.

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 online tutorials:

Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

View all posts by Bhawana Rathore →