Thursday, July 30, 2009

Filtering by a group in a SharePoint Person or Group column

I recently stumbled across this in a technet site here and modified it for my own use and to help anyone who wants it. I was trying to put multiple people in a SharePoint group, use a person or group column to categorize an item to that group, and then have it automatically filter based on which group you are in when you look at the item. I found the solution but it requires you to create the view that filters the Person or Group column to [Me] but then open the view in SharePoint designer and copy-and-paste the following (I know, it's scary) code. This means that you CANNOT add anything else to that view or attempt to modify it in any way after you change the code in SharePoint Designer because it will break that view and you have to do this all over again. This isn't a great solution because of that limitation but it will do if you absolutely HAVE to do this. Here are the steps:

1. Create the STANDARD view and filter to [Me] on the Person or Group column that contains the sharepoint group in which a person exists.
2. Open this view in SharePoint Designer…if it ever says you do not have permission to do something, simply click ‘Cancel’ and everything will be fine.
3. Click on the CODE or the SPLIT view near the bottom left...you want to be able to see the code itself.
4. If you are using SP 2010, skip to step 5. Look in the code for the phrase ‘ListViewXml=’ tag and then scroll a LONG ways toward the right (after all the ‘FieldRef Name=blahblah_x0020_blahblah’) and look for ‘<Where’ and it ends a little ways after that with ‘/Where>’
5. Look at the ‘FieldRef Name=’ and note what it is for this filter.
6. For 2007 users, replace the <Where thru the /Where> with the following:
<Where><Or><Membership Type="CurrentUserGroups"><FieldRef Name="REPLACEME"/></Membership><Eq><FieldRef Name="REPLACEME"/><Value Type="Integer"><UserID Type="Integer"/></Value></Eq></Or></Where>
(NOTE: DON’T MESS WITH ANY OTHER > and < in this!!)

For 2010 users, the split view in SharePoint designer will show you the encoded versions of these statements inside the <View> tag (it's SOOO pretty compared to all this garbage), so, you will want to fix them by copying and pasting this instead:
<Where><Or><Membership Type="CurrentUserGroups"><FieldRef Name="REPLACEME"/></Membership><Eq><FieldRef Name="REPLACEME"/><Value Type="Integer"><UserID Type="Integer"/></Value></Eq></Or></Where>

7. NOTE: TO USE CALENDAR VIEWS – For 2007 users, look inside the &lt;Where&gt; statement to find /DateRangesOverlap and then it has a couple of &gt’s and &lt’s and then it says EQ followed by the FieldRef Name of the key column you are using. Replace &lt;EQ through /EQ&gt; with the following:
&lt;Or&gt;&lt;Membership Type="CurrentUserGroups"&gt;&lt;FieldRef Name="REPLACEME"/&gt;&lt;/Membership&gt;&lt;Eq&gt;&lt;FieldRef Name="REPLACEME"/&gt;&lt;Value Type="Integer"&gt;&lt;UserID Type="Integer"/&gt;&lt;/Value&gt;&lt;/Eq&gt;&lt;/Or&gt;

Again, for 2010 users, it should look all HTMLified and pretty so you will want to copy and paste this instead:
<Or><Membership Type="CurrentUserGroups"><FieldRef Name="REPLACEME"/></Membership><Eq><FieldRef Name="REPLACEME"/><Value Type="Integer"><UserID Type="Integer"/></Value></Eq></Or>

8. Replace the REPLACEME’s with the fieldref name that it originally said. For instance, on one form with the Ministry teams the field name was Singing_x0020_Team (the _x0020_ is a space in the field name…and the fieldref name is the name the column was given originally…doesn’t matter if you change it after that, the fieldref name remains).

There ya go. Use only in emergencies ^_^.

17 comments:

  1. listviewxml tag not found !!

    ReplyDelete
  2. I have spent days searching for a solution to Filtering by Permission Groups and could only find very complicated js solutions that I could not get to work. This is such a simple solution to implement and works beautifully, even with multiple groups in the FilterBy field.

    Thank you so much!!
    (SharePoint 2010)

    ReplyDelete
  3. Hi!

    Do you have a similar solution for SharePoint 2010? I am facing exactly this problem.

    Thank you in advance.

    Katrin

    ReplyDelete
    Replies
    1. Hey Katrin,
      This should actually work the same way in SharePoint 2010. What it is doing is providing a filter where your field is either equal to your current user ID or your current group memberships and applies it within the CAML. This is all specified within the ListViewXML tag (notice the comment above yours was also SharePoint 2010). Was this not working for you?

      Delete
    2. I cannot find "ListViewXml=" in my view code. Even in advanced mode... :-(

      Delete
    3. I've updated the post, you are correct. They decided to make it a little easier to read in 2010 so you still have the same stuff, just all the > and < get converted to their actual ">" and "<" symbols. Thanks for pointing this out!

      Delete
  4. YES! It actually works! Thank you very much for this post. I already tried out so many instructions on several websites, but always without success. I will recommend your blog to everyone.
    Thanks again!

    Katrin

    ReplyDelete
  5. If you are trying to do this in SharePoint 2010 DataSheet view follow the direction for the 2007 users works like a charm!

    ReplyDelete
  6. Hi Brendan,

    I am confronted with this topic again. :-)

    How does your solution work the other way round?

    My scenario: I have a list showing the tasks of different teams. A task can be either assigned to a single person or to a group (each team has a group). Now I want to create a view for each team, displaying both the team tasks and those tasks assigned to persons within this group.

    How can I achieve this?

    Thanks for your help!
    Katrin

    ReplyDelete
  7. Hi Brendan!

    I already found a solution for my problem. It turned out to be rather easy. Thank you anyway for your help!

    Katrin

    ReplyDelete
  8. Great thank you!!! :-)
    interesting challenge adding in multiple criteria in the were clause - I got around this by make your suggested update, and then going back to the default SharePoint view edit page and adding in the additional filter criteria that I needed...

    ReplyDelete
    Replies
    1. Glad I could help; I usually setup my entire criteria within the view editor and then update the CAML per above for the one piece of Field=[Me] and leave it alone...I'm surprised that the view edit page didn't corrupt the view but, hey, if it works then we bless it and don't touch it, right? :)

      Delete
  9. What if you wanted to check against a specific group (the same one every time), not a column that contains a group?

    ReplyDelete
    Replies
    1. Well, there are a couple of ways of accomplishing that, but they don't really fit into this scenario. Here, we're trying to filter the list of items to only show items that pertain to a particular group...which, to your question, would end up being a Person/Group column and you just set the items you want to show up for a specific group to that group...and THEN this solution works wonders; otherwise, you could use filter web parts or even a web part page that has item-level permissions to give a specific group access to see a particular view of something.

      Delete
  10. This comment has been removed by the author.

    ReplyDelete
  11. i used this query in CAML

















    but i cannot receive filtered view based on group membership. every user receive files that he/she is created.

    where is my mistake?

    ReplyDelete
    Replies
    1. Hi gogi, I cannot see the CAML query for some reason - it did not show in the email and did not appear in this comment. Can you try updating your comment again with the CAML?

      Delete