Thursday, January 20, 2011

SPServices & CAML Query Options

This is a strange behaviour when using JQuery's SPServices on a list with multi-valued person/group field.

Operation: "GetListItems"
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='ID' /></ViewFields>"
CAMLQuery: "<Query></Query>"

E.g. If the list has this item: [ID=1, Title=My First Item, PersonsResponsible=Test User A; Test User B, Status=Pending], then the output of the method call would have 2 rows:
- Row 1: [ID=1, Title=My First Item, PersonsResponsible=Test User A]
- Row 2: [ID=1, Title=My First Item, PersonsResponsible=Test User B]
* Status field does not appear because not part of the ViewFields. However, PersonsResponsible appear because it is a mandatory column.

I'm not too sure why 2 rows of result are returned (could it be a bug?) but to solve this problem, you simply have to add
CAMLQueryOptions: "<QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions>"

This way, the method call correctly returns 1 row [ID=1, Title=My First Item]

No comments:

Post a Comment