how to filter list items by user/group column in sharepoint?
20,256
Solution 1
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;
}
}
}
}
}
Solution 2
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.
Author by
kisin
Updated on May 03, 2020Comments
-
kisin about 4 years
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?