简体   繁体   中英

SharePoint REST API membership

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!

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.

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);
    });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM