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:
- If you have the SharePoint Server (i.e. not foundation) then you could use the Audience feature and set the audience to the group. You would then display the data using the Content Query Web Part. The CQWP is available only when the Publishing features are enabled. (So this one does not help with Views.)
- Customize the web part (view) in SharePoint Designer. See the solution below.
- You could call the SharePoint web services. There is a JavaScript solution for 2007, but I've not seen one for 2010 (http://sharepointjavascript.wordpress.com/2010/02/16/filter-list-view-based-on-membership-in-sharepoint-group/) Post a comment to this article if you know of one.
- There may be third party solutions. (http://roxority.com/sharepoint-zen/sharepoint-list-filtering-by-user-group-membership.html) (I have no experience with this tool)
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:
You will need to write the WHERE as a single line of text and replace each "<" with "<" and each ">" with ">".
The following is for SharePoint 2010 and Office 365 (2010)…
- 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.
- 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) - 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.
- Create a view for a list:
- Give it a name (maybe "Filtered by Group Membership")
- 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]
- Configure the rest of the view as usual and save it
- Open your site in SharePoint Designer 2010
- In the Site Object pane click Lists and Libraries
- Click your list and then click the view you just created
- In the Code pane search for <Where>
- Delete everything between <Where> and </Where> (but leave these tags)
- 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):
<Where><Membership Type="CurrentUserGroups"><FieldRef Name="VisibleToGroup"/></Membership></Where> - 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)
- 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:
Here's the filtered view for a user who is in the Training Members and Training Friends groups:
Here's the filtered view for a user who is in the Training Friends and Training Members groups:
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:
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:"
Hi Dave,
is that Example works for you ?
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?
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
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.
Utkarsh,
Which version of SharePoint are you working with? 2010 or 2013?
Mike
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
worked fine! thank you so much
Thank you so much..
its working in SharePoint 2013 as well
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
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