Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
I never have done this but did you try to use the odata $filter field? You should write something like $filter=UserId eq yourUserID –  Salvatore Di Fazio Mar 24 at 16:54
    
That doesn't take care of the <Membership> element –  Martin Hatch Mar 24 at 17:16

3 Answers 3

up vote 7 down vote accepted

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

share|improve this answer
1  
Awesome answer. I'm fine with this being assigned as the accepted answer. –  Robert Kaucher Mar 25 at 0:03
    
BTW, I copied the question's CAML directly and assumed it was correct based on the what the OP said. –  Robert Kaucher Mar 25 at 0:07

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

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

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); } 
});
share|improve this answer
    
did you actually try this code? I've had to add the content-length and change "failure" to "error" –  Martin Hatch Mar 24 at 18:17
    
also getting "Bad Request" (invalid JSON object) –  Martin Hatch Mar 24 at 18:19
    
Need to see your code. Can you add it as an edit? –  Robert Kaucher Mar 24 at 18:26
    
Oh wait! I think I see the issue. –  Robert Kaucher Mar 24 at 18:27
    
The quotes in my CAML wouldn't have worked in the JSON request. Try it with what I edited. –  Robert Kaucher Mar 24 at 18:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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