Pages

10/14/2012

SharePoint - Filtering a View by Group Membership

 

I use this blog to both share things I have discovered about SharePoint and as a place to store "stuff" so I can find it later. Kind of like personal notes…  The following is not an original discovery, just a step by step set of notes for a solution from tips I have found elsewhere. The core XML that led to this article where found here:
http://spboom.com/sharepoint-development/how-to-filter-sharepoint-list-by-membership-in-sharepoint-group/
and here:
http://sympmarc.com/2010/06/16/caml-filter-for-current-group-membership/

Filtering a View by Group Membership

There are a number of possible solutions to this problem:

 

Filtering a View by Group Membership

Notes:

  • This is not security! Other views of the list will still display all of the items.
  • The XML query can be much, much more complicated with the addition of ANDs and ORs. For example, you might add an OR section to always show all items for site owners.
  • The following works on a view (XsltListViewWebPart), but will also work with a Data View (DataFormWebPart). For the DataFormWebPart you will have a little more work to do as the WHERE is embedded in the "selectcommand" string and all of the brackets are "escaped". Example:
    image
    You will need to write the WHERE as a single line of text and replace each "<" with "&lt;" and each ">" with "&gt;".

The following is for SharePoint 2010 and Office 365 (2010)…

  1. If you don't have a convenient group for testing, go create a SharePoint group with a few people who can help you test this.
  2. In your list add a new column:
    Name: "VisibleToGroup"  (any name will do as long as you know how to deal with spaces and special characters in the name)
  3. Add a few items to the list with a few "VisibleToGroup" blank, a few with your test group and one or two for say Site Owners or Members.
  4. Create a view for a list:
    1. Give it a name (maybe "Filtered by Group Membership")
    2. Add a Filter. Anything will do. This is just to add the <Where> section to the web part's XML.  Example: Created By equals [Me]
    3. Configure the rest of the view as usual and save it
  5. Open your site in SharePoint Designer 2010
  6. In the Site Object pane click Lists and Libraries
  7. Click your list and then click the view you just created
  8. In the Code pane search for <Where>
  9. Delete everything between <Where> and </Where>  (but leave these tags)
  10. Add the following bolded code inside of the Where tags:
      <Where>
        <Membership Type="CurrentUserGroups">
          <FieldRef Name="VisibleToGroup"/>
        </Membership>
      </Where>

    The above for a DataFormWebPart (Data View):
    &lt;Where&gt;&lt;Membership Type="CurrentUserGroups"&gt;&lt;FieldRef Name="VisibleToGroup"/&gt;&lt;/Membership&gt;&lt;/Where&gt;
  11. Change "VisibleToGroup" to your column name (remember if you used spaces in the name then see: how to deal with spaces and special characters in the name)
  12. Save the view and go test in the browser

 

The results…

Here's a view showing all tasks in a list, including the People Picker column for a selected group displayed:

  image

Here's the filtered view for a user who is in the Training Members and Training Friends groups:

  image

Here's the filtered view for a user who is in the Training Friends and Training Members groups:

  image

The first user only saw tasks 2 and 4. The second user saw only tasks 2 and 3. Neither user saw task 1 as it was not assigned to any group.

 

Got a better solution?

Post a comment to this article and share it!

.

11 comments:

Anonymous said...

In that last example I think you meant to say "Here's the filtered view for a user who is in the Training Friends and Training Owners groups:"

Anonymous said...

Hi Dave,

is that Example works for you ?

Anonymous said...

When I try to save this query I get: Soap:ServerException of type

'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown. Cannot
complete this action. Please try again.0x89994005"

Do you know what that might be?

Mike Smith said...

Anonymous,

Sorry I don't. The most common cause of errors like that is malformed XML. Make sure all of the tags are correct and are properly capitalized.

Mike

Utkarsh Yeolekar said...

i am trying to implement it on the custom list view. but getting nothing. i have added the user to that group. but it is not working for me.And also it is not showing any xml error. So,that i can look into caml query syntax.

Please help on this.

Mike Smith said...

Utkarsh,

Which version of SharePoint are you working with? 2010 or 2013?

Mike

Unknown said...

Hi Mike

I followed your instructions in a SharePoint 2013 site and it is not working, it's not giving me any errors or message so I don't know what to look for. Do you have an idea?
Thanks

Anonymous said...

worked fine! thank you so much

Anonymous said...

Thank you so much..
its working in SharePoint 2013 as well

conservativeInvestor said...

I added following code in my view



But it gives me error,
other than above code do i need to add anything?
I am using sharepoint Online classic?
Not sure whats wrong

Mike Smith said...

conservativeInvestor, nothing from older versions is guaranteed to work in SharePoint Online. Features there come, go and change every week. Going forward, I would not expect any of the old on-prem tricks to work in SharePoint Online.

Post a Comment

Note to spammers...
Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.