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

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)

  • Thank you for reply sir, I tried your way but I am getting error when I call the function. – users1100 Oct 28 '13 at 14:02
  • what kind of error you receive? – ReTech Oct 28 '13 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.

  • Error is thrown before executing the method GetUsers i.e. error comes when casting – users1100 Oct 28 '13 at 13:34
  • (SPFieldUserValueCollection)item[FieldNames.ProjectOwner] statement throws error – users1100 Oct 28 '13 at 13:36
  • Updated answer, can it be because you cast single user into SPFieldUserValueCOLLECTION? – Jussi Palo Oct 28 '13 at 13:37
  • Is there any way to get the single value.? – users1100 Oct 28 '13 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. – Jussi Palo Oct 28 '13 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
}
  • 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; – users1100 Oct 28 '13 at 14:01
  • See my updated answer – Nadeem Yousuf Oct 28 '13 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.