0

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?

2

Check the internal name of the workflow column. In my case the internal name was restricted to first 8 characters of the workflow column name. Otherwise your code looks good to me. Also the result was a numeric instead of "In Progress" or "Completed" or "Cancelled". Following are the numbers used for the statuses

0 - Starting

1 - Failed on Start

2 - In Progress

3 - Error Occurred

4 - Cancelled

5 - Completed

6 - Failed on Start (Retrying)

7 - Error Occurred (Retrying)

Hope it helps

0

what happens if you change

context.load(collListItem);

into

context.load(collListItem, 'Include(Workflow_x0020_Column_x0020_Name)');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.