I have a column which type is SPGroup
in a list. I want to get items via SharePoint REST API
, and I only need the items which SPGroup
contains the current user. So what's the URL? Thank you for your advice!

- 101
- 12
-
Is the user directly added to the SPGroup or through an indirect AD group? If the latter, you can't check if he's a member just by REST calls. Also, you'll require multiple rest calls to make this work, not just a single one. – Daniel B Sep 07 '15 at 13:32
-
@DanielB It's the first one. Can I get these data via one call? – Frank Code Sep 07 '15 at 13:34
2 Answers
You can use the $expand
parameter in your REST query to include related entities of data in your query, but since the SharePoint REST service implements the OData protocol, the $filter
method can only be invoked on the top level item, thus making it impossible to use on an expanded field.
Depending on how many items your list contains, I'd suggest that you either try to filter it on something other than the current user, or fetch everything and filters the result in your code instead.
A REST query would look something like this:
http://sp/_api/web/lists/getbytitle('MyList')/items?$select=Id,Title,Members/Id&$expand=Members
where Members
is the SPGroup
to be expanded.
In each item that is returned, you will get something like
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">60</d:Id>
</m:properties>
</content>
with the Id
value of the member. From this, you should be able to write some custom code to filter out only the items that contain the current logged in user.

- 8,770
- 5
- 43
- 76
-
Then, I need to get each Group by the ID and check whether the Group contains current user. Right? – Frank Code Sep 07 '15 at 14:03
-
Cause there are many groups and items, I guess there are too many calls being needed. – Frank Code Sep 07 '15 at 15:00
-
Take a look at this question: http://sharepoint.stackexchange.com/questions/137303/get-the-group-of-the-current-user-using-rest-api – Daniel B Sep 07 '15 at 15:06
-
-
You're welcome! I'll update my answer later with some more info to better fit the question! – Daniel B Sep 07 '15 at 15:13
-
Thank you! Actually, I had done this via JSOM today, but it was not friendly for UI controller, so I tried to find a way used REST. – Frank Code Sep 07 '15 at 15:17
-
I always try to go for a REST solution but sometimes it's just not possible! Hopefully, Microsoft keeps adding on methods to it to fully support all current CSOM/JSOM methods later in the REST interface! – Daniel B Sep 07 '15 at 15:19
-
You can still do what you're looking to do with good old fashioned SOAP with less of headache. I use this method to get Tasks assigned to a user's account or a group they're a member of.
function getAssignedToMe(){
var viewFields = '<ViewFields><FieldRef Name="ID" /><FieldRef Name="Title" /><FieldRef Name="Created" /><FieldRef Name="FileRef" />'
+'<FieldRef Name="AssignedTo" /><FieldRef Name="Status" /></ViewFields>';
// filter by AssignedTo is current user ID or assigned to the current user's group;
var camlQuery = '' +
'<Query>' +
'<Where>' +
'<And>' +
'<Or>' +
'<Membership Type="CurrentUserGroups"><FieldRef Name="AssignedTo"/></Membership>' +
'<Eq><FieldRef Name="AssignedTo"/><Value Type="Integer"><UserID/></Value></Eq>' +
'</Or>' +
'<And>' +
'<Neq><FieldRef Name="Status"/><Value Type="Text">Completed</Value></Neq>' +
'<Geq><FieldRef Name="Created" /><Value Type="DateTime"><Today OffsetDays="-60" /></Value></Geq>' +
'</And>' +
'</And>' +
'</Where>' +
'<OrderBy><FieldRef Name="Created" Ascending="TRUE"/></OrderBy>' +
'</Query>';
getListItems('/mysite', 'Tasks', viewFields, camlQuery, callback, 50);
// transform the returned XML to JSON
function callback(xmlDoc){
var data = [];
$(xmlDoc).find('*').filter(function () {
return this.nodeName.toLowerCase() == 'z:row';
}).each(function (i, el) {
// do something with the data
var id = parseInt($(el).attr('ows_ID'));
data.push({
Id: id
});
/* ... */
});
};
};
function getListItems(siteUrl, listName, viewFields, query, callback, rowLimit) {
if (rowLimit === void 0) { rowLimit = 25; }
var packet = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soap:Body>' +
'<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">' +
'<listName>' + listName + '</listName>' +
'<query>' + query + '</query>' +
'<viewFields>' + viewFields + '</viewFields>' +
'<rowLimit>' + rowLimit + '</rowLimit>' +
'</GetListItems>' +
'</soap:Body>' +
'</soap:Envelope>';
var $jqXhr = $.ajax({
url: siteUrl + '/_vti_bin/lists.asmx',
type: 'POST',
dataType: 'xml',
data: packet,
headers: {
"SOAPAction": "http://schemas.microsoft.com/sharepoint/soap/GetListItems",
"Content-Type": "text/xml; charset=utf-8"
}
}).done(function (xmlDoc, status, error) {
callback(xmlDoc);
}).fail(function (jqXhr, status, error) {
callback(null, status + ': ' + error);
});
};

- 466
- 3
- 10