1

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.

4
  • another approach would be to use Audiences. These are groups that are created on Sharepoint Server. Though I'm not 100% sure how that would work with your approach. I just wanted to give a suggestion.
    – Tally
    Jan 26, 2018 at 10:26
  • 1
    I could target the list with Audience settings but it only allows you to do this beforehand with specific groups or users. My problem is that different users will have access to different list items which are determined when the item is created and the Project Team Members column is populated.
    – cod3r_jim
    Jan 26, 2018 at 10:40
  • does $expand='Project Team Members' help
    – J. Doe
    Jan 26, 2018 at 10:49
  • @Jim that's true, Audience Targetting wouldn't help
    – Tally
    Jan 26, 2018 at 10:57

2 Answers 2

2

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");
8
  • Appreciate the response Vladimir. So if I edit my current list view in advanced mode, where would I be applying the script to? Inside the ViewName or code, the web-part itself or somewhere else?
    – cod3r_jim
    Jan 26, 2018 at 17:29
  • That script has to be executed one time. Open your site at /_layouts/settings.aspx then press F12 from your keyboard to open the Developer Console (Internet Explorer, Chrome, Firefox...). After update the script with your parameters and execute it inside the console tab of the developer console. Jan 26, 2018 at 17:55
  • Did you completed the operation? It works for your case ? If yes could you validate the answer ! Thanks in advance. Jan 30, 2018 at 9:34
  • Still having issues with it, most likely the syntax on my end. I'll link you to my current syntax and perhaps you can see where its going wrong Vlad. Link --> ufile.io/xtitp
    – cod3r_jim
    Jan 31, 2018 at 9:29
  • You modified also variable names, that's why it doesn't work. Sorry, I was not very clear. The script is a JavaScript, it's ready to use, you have to modify only strings on the last line between quotes. Jan 31, 2018 at 9:41
0

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

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.