14

It is quite common to use "Assigned To = [Me]" in SharePoint views to list the tasks / items which are assigned to the current user. This of course includes both the items which are directly assigned to the current user, as well as those items which are indirectly assigned to the current user (i.e. as a member of a Group).

I can happily do this in Server Side API code using the following CAML Query:

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

Does anyone know how I can do this using the REST API?

2
  • I never have done this but did you try to use the odata $filter field? You should write something like $filter=UserId eq yourUserID Mar 24, 2014 at 16:54
  • That doesn't take care of the <Membership> element Mar 24, 2014 at 17:16

4 Answers 4

12

Very interesting question and good answers. I have tested it on a usual Tasks list and got it working after trial and error. The link provided above, Using CAML in REST requests, is good, although it contains syntax errors and unintroduced variables that makes it harder to try out the examples. Robert Kaucher has a working solution, even though it contained a little misspelling, that's why it caused a 500 error for me. I'd rather recommend to not to put the whole CAML query into a URL query string, it can stop working in an older IE browser due the url length restrictions. It is better to put the CAML query into a request body.

In my out-of-the-box Tasks list, the following CAML Query works:

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

Pay attention to <FieldRef Name="AssignedTo" />, not <FieldRef ID="Assigned To" />.

Here is the working jQuery.ajax-code:

var viewXml = "<View><Query><Where><Or><Eq><FieldRef Name='AssignedTo' /><Value Type='Integer'><UserID/></Value></Eq><Membership Type='CurrentUserGroups'><FieldRef Name='AssignedTo' /> </Membership></Or></Where></Query></View>";
$.ajax({
  url: "/_api/web/lists/getbytitle('Tasks')/getitems",
  method: "POST",
  data: "{ 'query' : {'__metadata': { 'type': 'SP.CamlQuery' }, \"ViewXml\": \"" 
      + viewXml + "\" }}",
  headers: {
    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
    "Accept": "application/json; odata=verbose",
    "content-type": "application/json; odata=verbose"
  }
});

If you prefer to use SP.RequestExecutor and be independent of jQuery, just replace data with body in the request options. Make sure you have the SP.RequestExecutor.js loaded into your page first:

executor.executeAsync({
  url: "/_api/web/lists/getbytitle('Tasks')/getitems",
  method: "POST",
  body: "{ 'query' : {'__metadata': { 'type': 'SP.CamlQuery' }, \"ViewXml\": \"" + viewXml + "\" }}",
  headers: {
    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
    "Accept": "application/json; odata=verbose",
    "content-type": "application/json; odata=verbose"
  }
});

SP.RequestExecutor will make it easier to rewrite the code to an app and use it in a cross-domain scenario.

To try this request I have used PostMan. enter image description here

And Developer Tools in my browser, to see results without writing ajax callbacks. enter image description here

1
  • BTW, I copied the question's CAML directly and assumed it was correct based on the what the OP said. Mar 25, 2014 at 0:07
6

Try the following. Notice that you need to use POST as you are sending data. You also need the <View>and <Query>tags.

<View>
   <Query>
      <Where>
          <Or>
              <Eq>
                  <FieldRef ID="Assigned To" />
                  <Value Type="Integer"><UserID/></Value>
              </Eq>
              <Membership Type="CurrentUserGroups">
                  <FieldRef Name="AssignedTo" />
              </Membership>
          </Or>
      </Where>
   </Query>
</View>

Here I am sending a POST to 'Test List' with the view XML from above.

var viewXml = "<View><Query><Where><Or><Eq><FieldRef ID='Assigned To' /><Value Type='Integer'><UserID/></Value></Eq><Membership Type='CurrentUserGroups'><FieldRef Name='AssignedTo' /> </Membership></Or></Where></Query></View>";

$.ajax({
    type: "POST", 
    headers: { 
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        }, 
    url: 'https://mysharepoint.com/_api/web/Lists/GetByTitle(\'Test List\')/GetItems(query=@v1)?@v1={"ViewXml":"' + viewXml + '"}', 
    success: function(data){console.log(data); },
    failure: function(data){console.log(data); } 
});
7
  • did you actually try this code? I've had to add the content-length and change "failure" to "error" Mar 24, 2014 at 18:17
  • also getting "Bad Request" (invalid JSON object) Mar 24, 2014 at 18:19
  • Need to see your code. Can you add it as an edit? Mar 24, 2014 at 18:26
  • Oh wait! I think I see the issue. Mar 24, 2014 at 18:27
  • The quotes in my CAML wouldn't have worked in the JSON request. Try it with what I edited. Mar 24, 2014 at 18:36
4

The GetItems method in the REST api accepts a CAML query. Have you tried that?

http://msdn.microsoft.com/en-us/library/office/dn531433%28v=office.15%29.aspx#bk_ListGetItems

3
  • Aha .. might be what I needed, I'll give it a go Mar 24, 2014 at 17:16
  • This isn't an app and I'm not using the "Executor" .. do you have an example using $.ajax ? Mar 24, 2014 at 17:33
  • 1
    everytime I try to add the "Membership" element it just stops working Mar 24, 2014 at 18:16
1

This should get you what you need.

$.ajax({
    url: '/Accounting/_api/web/Lists/GetByTitle(\'Tasks\')/Items?$filter=AssignedTo eq ' + _spPageContextInfo.userId,
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" }, 
    success: function (data) {
        console.log(data);
    },
    error: function (data) {
        console.log(data.responseText);
    }
});
2
  • Will this account for instances where AssignedTo is a group the user is in?
    – Jane Panda
    Jul 21, 2017 at 17:32
  • No it doesn't (Tested it)
    – M.P.
    Jul 17, 2018 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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