Home » DVWP » What if [Me] is a Group?

What if [Me] is a Group?

One of the most common views asked for on a SharePoint list is the “Assigned to Me” view. But what if “Assigned to Me” is really “Assigned to Me” or “Assigned to My Group”?

I have struggled with this one for custom lists for a while now. Short of custom DVWPs which I have done, I didn’t have a simple or elegant solution for this. Since I was asked for it very recently on an Office 356 site that we manage, a DVWP was not an option. (I don’t have the code for how to make these memorized.)

I started thinking about the SharePoint tasks list, which has a “By My Groups” view. Opening that up in SPD, I saw this piece of code in the XsltListViewWebPart for the page:

<Query>
     <Where>
          <Membership Type="CurrentUserGroups">
               <FieldRef Name="AssignedTo"/>
          </Membership>
     </Where>
     <GroupBy Collapse="FALSE">
          <FieldRef Name="AssignedTo"/>
     </GroupBy>
     <OrderBy>
          <FieldRef Name="AssignedTo"/>
          <FieldRef Name="Status"/>
          <FieldRef Name="Priority"/>
     </OrderBy>
</Query>

Translating this into rough English:
Give me rows where the user is assigned the task based on membership in a group, group by “Assigned To”, order by “Assigned To” “Status” and “Priority”

The “My Tasks” view in a task list has a query of:

<Query>
     <Where>
          <Eq>
               <FieldRef Name="AssignedTo"/>
               <Value Type="Integer">
                    <UserID/>
               </Value>
          </Eq>
     </Where>
     <OrderBy>
          <FieldRef Name="Status"/>
          <FieldRef Name="Priority"/>
     </OrderBy>
</Query>

Translating this:
Give me rows where the user is assigned the task, order by “Status” and “Priority”

I combined these two queries to get:

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

Or, in English:
Give me rows where the user is assigned the task based on membership in a group OR where the user is assigned the task.

Now, I wasn’t using the task list or the “Assigned To” column, but all I had to do (and all you have to do) is put the internal name of the column you do want to check in the place of “AssignedTo” in the above query.

Pulling it All Together

To create a view on any list that will show a person’s tasks whether individually, or part of a SharePoint Group:

  1. Go to your list.
  2. Create a view that is a by [Me] view.
  3. Open that ByMeView.aspx page in SPD and locate the <Where></Where> clause in the WebPart code.
  4. Replace everything in that Where clause with the above code (remember to change the name of the column to the one you want to filter on).

Some Notes:

  1. Once you create the view and save the page in SPD, you can edit the view in the browser.
  2. Editing the view in the browser, you will NOT see your conditions in the filter section of the edit view page.
  3. You can add and change columns, you can even add additional conditions on the filter like “Status” not equal to “Closed”
  4. For fun, open up the page in SPD again and take a look at how the query has changed.
<Where>
     <And>
          <Or>
               <Membership Type="CurrentUserGroups">
                    <FieldRef Name="AssignedTo"/>
               </Membership>
               <Eq>
                    <FieldRef Name="AssignedTo"/>
                    <Value Type="Integer">
                         <UserID/>
                    </Value>
               </Eq>
          </Or>
          <Neq>
               <FieldRef Name="Status"/>
               <Value Type="Text">Closed</Value>
          </Neq>
     </And>
</Where>

CMNT_CLSD