Question

Currently I have a Person/Group column called Project Team Members that allows you to add multiple SharePoint users to it.

All i'm looking to do is filter the list view based on the current logged in user being part of the Project Team Members column.

Current methods I've tried:

Workflow sets a single line text column to store the Project Team Members:

(This just returns) {"__metadata":{"type":"Collection(Edm.Int32)"},"results":[28]}

Rest Call to connect to the current list item, then try to extract the values from the Project Team Members column:

_api/web/lists/getByTitle('Projects')/items?$filter=Id eq '[%Current Item:ID%]'

This connects and allows me to extract any value from the list except the Project Team Members column. (Person/Group)

I've looked into using the $expand in the query to expand the Project Team Members column as it's a lookup column but the syntax is most likely wrong.

_api/web/lists/getByTitle('Projects')/items?$filter=Id eq '[%Current Item:ID%]'&$expand=Project Team Members

(I've also tried the internal name: Project_x0020_Team_x0020_Members to no avail)

Essentially I just want to extract the values from the Project Team Members person/group field and display them as one text value so I can then filter the view.

Was it helpful?

Solution

If you want only filter a view from your list for the current user based on a user/group field value(s), you can modify the view query in "advanced mode" by using the membership CAML element (https://msdn.microsoft.com/en-us/library/office/aa544234.aspx).

Two ways for updating the CAML Query:

  1. By using SharePoint Designer. AVOID that to keep the ListView Form ghosted.

  2. By Code (Powershell, CSOM, JSOM). In your case, a sample JS Script below to be executed in your web browser console (F12 - Debug Window) from a page which the JSOM is loaded by default, such as a list view or the list of all site contents (http[s]://sharepoint.domain.com/sites/yoursite/_layouts/settings.aspx)

 (function(listTitle, viewTitle, peopleFieldName){
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {        
            var _log = function () {
                Sys.Debug.trace(String.format.apply(this, arguments));
            }
            var _errHandler = function(sender, args){
                _log("Error occured: Request failed {0}.\r\n{1}",args.get_message(), args.get_stackTrace());
            }

            var ctx = SP.ClientContext.get_current();
            var web = ctx.get_web();
            var list = web.get_lists().getByTitle(listTitle);
            var view = list.get_views().getByTitle(viewTitle);

            ctx.load(view);

            ctx.executeQueryAsync(function(){

                _log("[{0}][{1}]Current CAML Query:\r\n{2}",listTitle,viewTitle,view.get_viewQuery());

                var newquery = "<Where><Or><Membership Type=\"CurrentUserGroups\"><FieldRef Name=\"{0}\"/></Membership><Eq><FieldRef Name=\"{0}\"></FieldRef><Value Type=\"Integer\"><UserID/></Value></Eq></Or></Where>"
                newquery = String.format(newquery, peopleFieldName);

                view.set_viewQuery(newquery);
                view.update();

                ctx.executeQueryAsync(function(){

                    _log("[{0}][{1}]New CAML Query Applied:\r\n{2}",listTitle,viewTitle,newquery);

                },_errHandler);

            },_errHandler);

        });
    })("List Name", "All Items", "People field internal name");

OTHER TIPS

You could try use $select instead of filter

/_api/web/lists/getbytitle('Projects')/items?$select=Id eq '[%Current Item:ID%]'&$expand=Project Team Members

If the REST calls are not working you could try using the search rest api (as suggested in this post)

/_api/search/query?querytext='[%groupID%]* + path:https://sitecollectionurl/*'&selectproperties='DisplayName'

How to get SharePoint group ID from group Name or Title

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top