Reputation: 177
I have a list that has a user/group column that I want to filter by (the column name is: USERS). how do I get only the items in the list where the current user exists in the USERS column?
Upvotes: 3
Views: 20713
Reputation: 177
if (item["users"] != null)
{
//get USERS field for item
SPFieldUserValueCollection fieldUserValueCollection = new SPFieldUserValueCollection(web, item["users"].ToString());
//go over the users/groups collection
foreach (SPFieldUserValue fieldUserValue in fieldUserValueCollection)
{
if (fieldUserValue.User == null) //group
{
if (web.SiteGroups.GetByID(fieldUserValue.LookupId).ContainsCurrentUser)
{
bolItemGood = true;
break;
}
}
else //user
{
if (fieldUserValue.User.IsDomainGroup) //domain group
{
if (web.IsCurrentUserMemberOfGroup(fieldUserValue.LookupId))
{
bolItemGood = true;
break;
}
}
else //sp user
{
if (fieldUserValue.User.LoginName == Context.User.Identity.Name)
{
bolItemGood = true;
break;
}
}
}
}
}
Upvotes: 1
Reputation: 7933
If it is simply a customized view, look at a Tasks list and the My Items view for reference.
You should be able to go the the Filter section in the view and have a filter that has "is equal to" "[Me]". However, it sounds like this is a multi-valued field so maybe you can get away with "contains" "[Me]".
Another considerations is looking into Audiences if you have MOSS. The Content Query Web Part is capable of filtering list items based on the audience.
Upvotes: 1