Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am attempting to write an extension method for the SPListItem type to ease the pain of getting SPUserValues as strings. I am getting an "Object reference not set to an instance of an object" exception when I hit the line: msg = "field.AllowMultipleValues == " + field.AllowMultipleValues;

because field is null. I know that at the item level item[fieldName] has a value and that the field does exist as I can see the field definition in item.Fields.

Any suggestions about where I am going wrong?

    public static List<string> GetFieldValueUserLogin(this SPListItem item, string fieldName)
    {     

        try
        {
            if (item != null)
            {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("_PAR_", TraceSeverity.Unexpected, EventSeverity.Information), TraceSeverity.Unexpected, "Getting value of " + fieldName, null);
                List<string> userNames = new List<string>();

                SPFieldUser field = item[fieldName] as SPFieldUser;

                string msg = field == null ? "field is null" : "field is not null";

                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("_PAR_", TraceSeverity.Unexpected, EventSeverity.Information), TraceSeverity.Unexpected, msg, null);

                msg = "field.AllowMultipleValues == " + field.AllowMultipleValues;

                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("_PAR_", TraceSeverity.Unexpected, EventSeverity.Information), TraceSeverity.Unexpected, msg, null);

//Etc ...
   }
}

Edit--- After a bit of caffeine I went back and realized I was kind of being an idiot. Appears to be my modus operandi. Here is my final method code.

public static List<string> GetFieldValueUserLogins(this SPListItem item, string fieldName)
{
    try
    {
        if (item != null)
        {
            List<string> userNames = new List<string>();

            SPFieldUserValueCollection uvc = new SPFieldUserValueCollection(item.Web, item[fieldName].ToString());
            foreach (SPFieldUserValue userValue in uvc)
            {
                userNames.Add(userValue.User.LoginName);
            }


            return userNames;
        }
        else
        {
            return null;
        }
    }
    catch (Exception ex)
    {
        SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("_PAR_", TraceSeverity.Unexpected, EventSeverity.Information), TraceSeverity.Unexpected, ex.Message + " - " + ex.StackTrace.ToString(), null);
        return null;
    }
}
share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

I'll be honest I'm really confused with your code because your method name implies you are getting a value but the code looks like you are reading and possibly updating a field property. So to provide some feedback on the code you have there:

As you stated, item[fieldDisplayName] will get you a field value. (SPFieldUserValue, SPFieldUserValueCollection, string, etc.)

item.Fields[fieldInternalName] is what you need to get you a field. (SPField, SPFieldUser, SPFieldChoice, etc.)

Once you have the field, to get the value I would use the field Id and cast your values accordingly using item[field.Id] - this will eliminate any discrepencies / confusion between "should I now use the internal or display name?"

share|improve this answer
    
item.Fields[fieldInternalName] <= Exactly that. But, no I was not updating any value in my code. The idea was just to return a list of user names. –  Robert Kaucher Sep 11 '12 at 21:35
add comment

You should try :

  1. Always using the InternalName of the field
  2. Try validating the collection of Fields via spList.Fields.ContainsField(fieldName)
share|improve this answer
    
I pass the internal name as the parameter fieldName. –  Robert Kaucher Sep 11 '12 at 16:55
add comment

As C. Marius you need to lookup the internal name as it would have some other long values attached to it..

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookup.aspx

plus try this instead

SPFieldUser field =(SPFieldUser)item[fieldName];

also field.AllowMultipleValues shouldnt it be set i belive to true or false?

Gets or sets a Boolean value that specifies whether multiple values can be used in the lookup field.

true to specify that multiple values can be used in the field; otherwise, false. The default is false.

like so:

field.AllowMultipleValues = true;

or

field.AllowMultipleValues = false;

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookup.allowmultiplevalues(v=office.15).aspx

share|improve this answer
    
You are not understanding the line msg = "field.AllowMultipleValues == " + field.AllowMultipleValues; this will print the string field.AllowMultipleValues == true or field.AllowMultipleValues == false to the ULS. I'm just throwing in some debugging so I can go back after the fact and make sure values are set as expected. –  Robert Kaucher Sep 11 '12 at 16:58
add comment

Have you tried to cast it as an SPFieldUserValue or an SPFieldUserValueCollection?

share|improve this answer
    
That is not possible as I am dealing with the field object. Microsoft.SharePoint.SPFieldUser inherits from Microsoft.SharePoint.SPField and neither SPFieldUserValue nor SPFieldUserValueCollection are field types. –  Robert Kaucher Sep 11 '12 at 17:30
    
item[fieldName] or item[fieldGuid] won't give you a field, it will give you a field value. –  Jaime Sep 11 '12 at 18:21
    
aditionally you could do SPFieldUserValueCollection users = new SPFieldUserValueCollection(item.Web, item[field].ToString()) and iterate trough the SPFieldValue collection –  Jaime Sep 11 '12 at 18:24
    
@Jaime - don't forget you can edit your answer if you want to expand on it, as well as adding comments. –  SPDoctor Sep 12 '12 at 9:02
    
@Jaime - +1'ed your answer because you were right as well. –  Robert Kaucher Sep 19 '12 at 23:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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