I have a requirement to allow/deny a button action from script editor web part based on the workflow status of an item. I tried the below code to get the status of a workflow
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var myID = 'item ID';
var list = web.get_lists().getByTitle("list name");
var listItem = list.getItemById(myID);
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Text'>"+myID+"</Value></Eq></Where></Query><ViewFields><FieldRef Name='Workflow_x0020_Column_x0020_Name' /></ViewFields></View>");
window.collListItem = list.getItems(camlQuery);
context.load(collListItem);
context.executeQueryAsync(
function()
{
var listItemEnumerator = collListItem.getEnumerator();
var status= '';
while (listItemEnumerator.moveNext())
{
var oListItem = listItemEnumerator.get_current();
status = oListItem.get_item('Workflow_x0020_Column_x0020_Name');
}
},
function(){alert("something went wrong");
}
);
And the error I would get is
The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
Why this error? Is there a specific way to fetch these columns?