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.
var thisTaskStatus = $.trim($(this).attr("Status")); var thisTaskStatus = $.trim($(this).attr("ows_Status")); var 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:
<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">
<CHOICES>
<CHOICE>Not Started</CHOICE>
<CHOICE>In Progress</CHOICE>
<CHOICE>Completed</CHOICE>
<CHOICE>Deferred</CHOICE>
<CHOICE>Waiting on someone else</CHOICE>
</CHOICES>
<MAPPINGS>
<MAPPING Value="1">Not Started</MAPPING>
<MAPPING Value="2">In Progress</MAPPING>
<MAPPING Value="3">Completed</MAPPING>
<MAPPING Value="4">Deferred</MAPPING>
<MAPPING Value="5">Waiting on someone else</MAPPING>
</MAPPINGS>
<Default>Not Started</Default>
</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:
$().SPServices({
operation: "GetListItems",
listName: "Tasks"
});
Status isn’t returned because it’s not in the default view.
<z:row
ows_Checkmark="boolean;#0"
ows_LinkTitle="boo"
ows_AssignedTo=""
ows_PercentComplete="0.500000000000000"
ows__ModerationStatus="0"
ows__Level="1"
ows_Title="boo"
ows_ID="1"
ows_UniqueId="1;#{CC125F6A-F329-46D7-8140-F9353F1AC7EC}"
ows_owshiddenversion="1"
ows_FSObjType="1;#0"
ows_Created_x0020_Date="1;#2014-01-29 13:04:43"
ows_Created="2014-01-29 13:04:43"
ows_FileLeafRef="1;#1_.000"
ows_PermMask="0x7fffffffffffffff"
ows_Modified="2014-01-29 13:04:43"
ows_FileRef="1;#sites/Demos2013/jquerylib/Lists/Tasks/1_.000">
</z:row>
Instead, you can add the CAMLViewFields option to request it:
$().SPServices({
operation: "GetListItems",
listName: "Tasks",
CAMLViewFields: "<ViewFields><FieldRef Name='Status'/></ViewFields>"
});
Then I get:
<z:row
ows_Status="Deferred"
ows_MetaInfo="1;#"
ows__ModerationStatus="0"
ows__Level="1"
ows_Title="boo"
ows_ID="1"
ows_UniqueId="1;#{CC125F6A-F329-46D7-8140-F9353F1AC7EC}"
ows_owshiddenversion="2"
ows_FSObjType="1;#0"
ows_Created="2014-01-29 13:04:43"
ows_PermMask="0x7fffffffffffffff"
ows_Modified="2014-01-29 13:11:21"
ows_FileRef="1;#sites/Demos2013/jquerylib/Lists/Tasks/1_.000">
</z:row>
Being explicit about the columns you want to retrieve is always a good practice. People can change the default view easily.
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.