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.
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. |
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:
- Steps to add items from csv file to SharePoint Online List using PowerShell in CSOM
- SharePoint create workflow history list programmatically using client side object model code (csom)
- SharePoint 2016 CSOM Check if Column Exists or Not in SharePoint List
- Copy SharePoint list items to another list programmatically using CSOM in SharePoint Online/2013/2016
- Create a list from list template in SharePoint Online 2013 programmatically
- CAML Query for SharePoint Boolean Field using SharePoint Online Client Object Model (CSOM)
- SharePoint Online: Create an indexed column in a list Programmatically using CSOM .Net managed object model code using visual studio 2017/2015
I hope this will be helpful to retrieve ModerationStatus of a document in a SharePoint Online document library.
Hello Everyone!! I am Bhawana a SharePoint MVP and having about 10+ 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”