Get approval status in edit form
-
30-09-2020 - |
Frage
Is there an easy way to retrieve item approval status (moderationStatus) inside the edit form? During display of edit form I need to know wheather the currently edited item was approved or not. I see that the information is not reflected in a hidden column or else. I have the following ideas:
- Custom flag column that indicates if the item was approved.
- Get the current item via js rest call and check OData__ModerationStatus.
I just want to know if there is an easier way to get the moderationStatus of the item I am editing.
Thank you!
Lösung
Since Content Approval status is stored in List Item (_ModerationStatus
) I would recommend to leverage any client APIs (JSOM, REST or SOAP) for that purpose.
How to retrieve content approval status using client APIs
JSOM
function getListItemModerationInfo(listId,itemId,success,error)
{
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getById(listId);
var item = list.getItemById(itemId);
ctx.load(item);
ctx.executeQueryAsync(
function() {
var moderationInfo = {};
moderationInfo.Status = item.get_item('_ModerationStatus');
moderationInfo.Comments = item.get_item('_ModerationComments');
success(moderationInfo);
},
error);
}
Usage: how to retrieve content approval status of list item in edit form
var listId = _spPageContextInfo.pageListId; //current List Id
var itemId = parseInt(GetUrlKeyValue('ID')); //current Item Id
getListItemModerationInfo(listId,itemId,
function(info){
console.log(info.Status);
},
function(sendera,args){
console.log(args.get_message());
});
REST
Endpoint: /_api/web/lists/getById('<guid>')/items(<id>)?$expand=FieldValuesAsHtml
function getListItemDetails(webUrl,listId,itemId)
{
var url = webUrl + "/_api/web/lists/getById('" + listId + "')/items(" + itemId + ")?$expand=FieldValuesAsHtml";
return $.ajax({
url: url,
method: "GET",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose"
}
});
}
Usage
var listId = _spPageContextInfo.pageListId;
var itemId = parseInt(GetUrlKeyValue('ID'));
getListItemDetails(_spPageContextInfo.webAbsoluteUrl,listId,itemId)
.done(function(data)
{
var moderationStatusName = data.d.FieldValuesAsHtml['OData__x005f_ModerationStatus'];
console.log(moderationStatusName);
})
.fail(function(error){
console.log(JSON.stringify(error));
});