Finding a Task’s Status Using GetListItems and SPServices in SharePoint 2013

I get interesting questions all this time. This one came from a client who was trying to use GetListItems with SPServices in SharePoint .

Hey Marc, I’m trying to do a GetListItems operation on tasks list and I’m having trouble with the “Task Status” column. This is SP2013.

1var thisTaskStatus = $.trim($(this).attr("Status"));
2var thisTaskStatus = $.trim($(this).attr("ows_Status"));
3var thisTaskStatus = $.trim($(this).attr("ows_Task_x0020_Status"));

None of the above work and I went into the edit column settings of the task list and clicked on the status column and it says the field name is “Status”. When I look at the XML returned using Firebug, a lot of other fields show up like Title, Due Date, Created By, etc. but I don’t see the status column showing up at all in the responseXML.

Many of the out of the box columns have rather obscure StaticNames. In a Tasks list, the column which has the “Status” DisplayName has a StaticName of “Status” as well, so that’s pretty simple. Here’s the field definition, which I grabbed by calling GetList on my own Tasks list:

1<Field Type="Choice" ID="{c15b34c3-ce7d-490a-b133-3f4de8801b76}" Name="Status" DisplayName="Task Status" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="Status" ColName="nvarchar4">
2  <CHOICES>
3   <CHOICE>Not Started</CHOICE>
4   <CHOICE>In Progress</CHOICE>
5   <CHOICE>Completed</CHOICE>
6   <CHOICE>Deferred</CHOICE>
7   <CHOICE>Waiting on someone else</CHOICE>
8  </CHOICES>
9  <MAPPINGS>
10    <MAPPING Value="1">Not Started</MAPPING>
11    <MAPPING Value="2">In Progress</MAPPING>
12    <MAPPING Value="3">Completed</MAPPING>
13    <MAPPING Value="4">Deferred</MAPPING>
14    <MAPPING Value="5">Waiting on someone else</MAPPING>
15  </MAPPINGS>
16  <Default>Not Started</Default>
17</Field>

Calling GetList to take a look at the list schema is a good way to keep yourself from going crazy.

Now that said, you don’t always get all of the columns for a list when you call GetListItems. By default, you get the columns which are shown in the default view.

In an out of the box Tasks list that I just created, when I make this call:

1$().SPServices({
2  operation: "GetListItems",
3    listName: "Tasks"
4});

Status isn’t returned because it’s not in the default view.

1<z:row
2  ows_Checkmark="boolean;#0"
3  ows_LinkTitle="boo"
4  ows_AssignedTo=""
5  ows_PercentComplete="0.500000000000000"
6  ows__ModerationStatus="0"
7  ows__Level="1"
8  ows_Title="boo"
9  ows_ID="1"
10  ows_UniqueId="1;#{CC125F6A-F329-46D7-8140-F9353F1AC7EC}"
11  ows_owshiddenversion="1"
12  ows_FSObjType="1;#0"
13  ows_Created_x0020_Date="1;#2014-01-29 13:04:43"
14  ows_Created="2014-01-29 13:04:43"
15  ows_FileLeafRef="1;#1_.000"
16  ows_PermMask="0x7fffffffffffffff"
17  ows_Modified="2014-01-29 13:04:43"
18  ows_FileRef="1;#sites/Demos2013/jquerylib/Lists/Tasks/1_.000">
19</z:row>

Instead, you can add the CAMLViewFields option to request it:

1$().SPServices({
2  operation: "GetListItems",
3    listName: "Tasks",
4    CAMLViewFields: "<ViewFields><FieldRef Name='Status'/></ViewFields>"
5});

Then I get:

1<z:row
2  ows_Status="Deferred"
3  ows_MetaInfo="1;#"
4  ows__ModerationStatus="0"
5  ows__Level="1"
6  ows_Title="boo"
7  ows_ID="1"
8  ows_UniqueId="1;#{CC125F6A-F329-46D7-8140-F9353F1AC7EC}"
9  ows_owshiddenversion="2"
10  ows_FSObjType="1;#0"
11  ows_Created="2014-01-29 13:04:43"
12  ows_PermMask="0x7fffffffffffffff"
13  ows_Modified="2014-01-29 13:11:21"
14  ows_FileRef="1;#sites/Demos2013/jquerylib/Lists/Tasks/1_.000">
15</z:row>

Being explicit about the columns you want to retrieve is always a good practice. People can change the default view easily.

Similar Posts

One Comment

  1. This is great stuff, thanks! I follow-up — which attr data stores the relationship between tasks — i.e. summary vs child tasks? When I bring up tasks in a view in SP2013 I see a distinct hierarchical and physical ordering. But, when I query via SPServices, they come in completely scrambled.

Leave a Reply to John Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.