Search code examples
sharepointsharepoint-onlinesharepoint-list

Filter SharePoint list by "current user member of group"


I have a SharePoint online list, on a site were everybody in organization has read access. The list has 3 columns: Title, Link, Site and is a table of contents to pages from different SharePoint sites, with different members. The list is populated by Flows that triggers when a page is published and it's name follows a pattern.

For example:

Article1 | Link-to-Article1 | Financial

Article2 | Link-to-Article2 | Administration

I would like to filter this list so that every user sees only the links to the sites he is member of.

Looking at the above example, if current user is member of Financial Team but not a member of Administration, he should see only Article1.

Is it possible to do this either by SharePoint view filtering or view formatting json?

Thank you!


Solution

  • You can add a fourth column (Group) with type User/Group to the list and populate it with group or groups, that should view it. After that you can use CAML Membership element for filtering.

    Basically, your SPView.Query should be something like (if ShowForGroups is an internal name for the column, that contains groups):

    <Where>
       <Or>
          <Eq>
             <FieldRef Name="ShowForGroups" />
             <Value Type="Integer">
                <UserID />
             </Value>
          </Eq>
          <Membership Type="CurrentUserGroups">
             <FieldRef Name="ShowForGroups" />
          </Membership>
       </Or>
    </Where>
    

    However, you won't be able to create such view, using SharePoint view creation page, you will need to use powershell for that.