2

I have a query that filters a Workflow Tasks list to return only items where the task is assigned to either the current user or a group the current user is in:

<Or>
  <Membership Type="CurrentUserGroups">
    <FieldRef Name="AssignedTo"/>
  </Membership>
  <Eq>
    <FieldRef Name="AssignedTo"></FieldRef>
    <Value Type="Integer">
      <UserID />
    </Value>
  </Eq>
</Or>

I've been using this as the caml argument to the following method:

function getDataWithCaml(listName, caml) {
    var endpoint = "/_api/web/lists/GetByTitle('"
        + listName + "')/GetItems";
    var requestData = { "query" :
        {"__metadata": 
            { "type": "SP.CamlQuery" }
            , "ViewXml": caml
        }
        };
    return jQuery.ajax({
        url: endpoint,
        method: "POST",
        data: requestData,
        headers: {
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "Accept": "application/json; odata=verbose",
            "Content-Type": "application/json; odata=verbose"
        }
    });
}

This works fine and returns the filtered results, however what I can't figure out is how can I use this same filter but also expand the Editor and AssignedTo columns as is commonly done with the /items end point.

No matter how I specify the fields I get back The $expand query is not valid for the field 'AssignedTo', even with something as simple as:

/_api/Web/GetByTitle('Workflow Tasks')/getitems?$select=AssignedTo/Id&$expand=AssignedTo

How can I expand the fields while maintaining the above filter?

  • is AssignedTo is a people field? – Venkat Konjeti Aug 3 '17 at 20:10
  • Yes; it's the column that comes with an out of the box Workflow Tasks list – Jane Panda Aug 3 '17 at 20:18
1

Can you try the below REST API call? It worked for me.

siteurl/_api/web/lists/getByTitle('Workflow Tasks')/Items?$select=AssignedTo/Id&$expand=AssignedTo

To get the current user tasks

var currentUserId = _spPageContextInfo.userId;
var url = "siteurl/_api/web/lists/getByTitle('Workflow Tasks')/Items?$filter=AssignedToId eq "+ currentUserId + "$select=AssignedTo/Id&$expand=AssignedTo"
  • That returns all the list items though; I only want items where the task is assigned to either the current user or a group the current user is in – Jane Panda Aug 3 '17 at 20:22
  • check my answer above – Venkat Konjeti Aug 3 '17 at 20:29
  • That doesn't catch instances where the user is in a group and AssignedTo is set to that group. – Jane Panda Aug 3 '17 at 20:31
  • It won`t work for the group. The out of the box list view filter has the same issue. – Venkat Konjeti Aug 3 '17 at 20:32
  • 1
    I am chasing the same question and it just occurred to me that instead of just hitting the REST endpoint with a POST and a CAML query, I need to just bite the bullet and make a JSOM call via the SP.ClientContext.get_web().get_lists().getByTitle()... approach and request that the object model go ahead and expand everything out. I'm posting this as a comment b/c I don't have all the code handy (yet) to do that but if you've done any SP dev at all, you'll know what I'm talking about. – Wade Oct 18 '17 at 21:18

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.