Tuesday, 7 April 2009

Fun with SPFieldUserValueCollection

I have been searching around to see the various ways in which people are retrieving Users and Groups objects or the string value from relevant SPField  object when retrieving ListItems from a SharePoint List,  and I have found that there are quite a few blog posts on this subject, all with differing code and views. 

Having now tested nearly all of them, i have used 2 approaches that have helped me and the are

  1. Retreiving an SPUser or SPGroup object – perhaps for validation purposes or to gain other information about the user of group
  2. Retrieving a User friendly string representation of the User or Group – perhaps for simple display purposes.

The code below show the retrieval of the SPUser/SPGroup objects

Code Snippet
  1. using (SPSite site = new SPSite("http://mysite.com/sitecollection"))
  2. {
  3.     using (SPWeb web = site.OpenWeb("mysitename"))
  4.     {
  5.         SPList oList = web.Lists["ListName"];
  6.         SPListItemCollection oListItems = oList.Items;
  7.         foreach (SPListItem oListItem in oListItems)
  8.         {
  9.             //Gets a collection of all the User and Group objects from the UserGroup Field
  10.             SPFieldUserValueCollection oFieldUserValueCollection =
  11.                 new SPFieldUserValueCollection(web, oListItem["FieldName"].ToString());
  12.  
  13.             //Next, loop through the Values in the Collection
  14.             foreach (SPFieldUserValue oFieldUserValue in oFieldUserValueCollection)
  15.             {
  16.                 //now we need to test if the SPFieldUserValue is a
  17.                 //User or a  Group, because if we simply assign a user
  18.                 //we will get a Null Reference Exception if it is a Group
  19.  
  20.                 //It is a Group
  21.                 if (oFieldUserValue.User == null)
  22.                 {
  23.                     //Create and assign a new SPGroup object by using the
  24.                     //Lookup value to return a Group from the current Site(SPWeb)
  25.                     SPGroup oGroup = web.Groups.GetByID(oFieldUserValue.LookupId);
  26.                 }
  27.                 //It is a User
  28.                 else
  29.                 {
  30.                     //Create and assign a new SPUSer object by using the User
  31.                     //property of the SPFieldUserValue object
  32.                     SPUser oUser = oFieldUserValue.User;
  33.                 }
  34.             }
  35.         }
  36.     }
  37.     
  38. }

and the code below here shows the retrieval if the simple string representation.

Code Snippet
  1. SPList oList = web.Lists["ListName"];
  2. SPListItemCollection oListItems = oList.Items;
  3. foreach(SPListItem oListItem in oListItems)
  4. {
  5.     SPFieldUserValueCollection oFieldUserValueCollection
  6.         = SPFieldUserValueCollection(web, oListItem["FieldName"].ToString());
  7.  
  8.      foreach(SPFieldUserValue oFieldUserValue in oFieldUserValueCollection)
  9.     {
  10.         //simply retrieve the string value from the SPFieldUserValue object
  11.          string sFieldValue = oFieldUserValue.LookupValue;
  12.     }
  13. }

If anyone else has any more thoughts on this please let us know.

2 comments: