0

I want to get the user value from the Person or Group column in code behind.

My code is:

List<string> manager = GeneralMethods.GetUsers((SPFieldUserValueCollection)item[FieldNames.Manager]); List<string> owner = GeneralMethods.GetUsers((SPFieldUserValueCollection)item[FieldNames.ProjectOwner]); 

The method code is:

 public static List<string> GetUsers(SPFieldUserValueCollection users) { List<string> listUsers = new List<string>(); if (users.Count != 0) { foreach (SPFieldUserValue field in users) { SPUser user = field.User; listUsers.Add(user.LoginName); } } return listUsers; } 

My problem is that manager assigned successfully but when the control comes to owner then the above mentioned error is thrown. Here the item[FieldNames.ProjectOwner]) is the person or group column which accepts single value only.

What am I missing?

3 Answers 3

3

I wouldn't cast the field directly in a SPFieldUserValueCollection. Try this:

public List<string> GetUser(string fieldName, SPListItem item) { List<string> listUsers = new List<string>(); string strUserColl = item[fieldName].ToString(); if (strUserColl != null) { SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection(web, strUserColl); if (usercollection != null) { foreach (SPFieldUserValue userValue in usercollection) { SPUser user = userValue.User; listUsers.Add(user.LoginName); } } } return listUsers; } 

If you want to initialize a SPFieldUserValueCollection object that is bases on a field value, you should use this constructor. See Msdn: SPFieldUserValueCollection(SPWeb,string)

2
  • Thank you for reply sir, I tried your way but I am getting error when I call the function. Commented Oct 28, 2013 at 14:02
  • what kind of error you receive? Commented Oct 28, 2013 at 14:29
0

Verify that user.LoginName for the project owner returns a string in format ID;#USERACCOUNT. E.g., 34;#domain\user. ID being the Id of the user in this site collection's hidden User Information List.

If it only has a single value, you probably cannot cast it to a collection.

6
  • Error is thrown before executing the method GetUsers i.e. error comes when casting Commented Oct 28, 2013 at 13:34
  • (SPFieldUserValueCollection)item[FieldNames.ProjectOwner] statement throws error Commented Oct 28, 2013 at 13:36
  • Updated answer, can it be because you cast single user into SPFieldUserValueCOLLECTION? Commented Oct 28, 2013 at 13:37
  • Is there any way to get the single value.? Commented Oct 28, 2013 at 13:38
  • It is a single value if the Field is defined to only have single values. You need to cast it to SPFieldUserValue. Commented Oct 28, 2013 at 13:52
0

Can you give this code a try. I haven't tested it though:

UPDATE

Perhaps there is no value in the field. Check for the null condition as shown below:

SPFieldUserValue owner = item[FieldNames.ProjectOwner] as SPFieldUserValue; if (null != owner) { SPUser user = owner.User; string ownerLogin = user.LoginName } 
2
  • I tried your suggested way but I am getting the error Object reference not set to an instance of an object on the line SPUser user = owner.User; Commented Oct 28, 2013 at 14:01
  • See my updated answer Commented Oct 28, 2013 at 14:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.