0

I am using sp 2010. This is my custom code:

var taxField = item["Application"] as TaxonomyField;

                    if (taxField.AllowMultipleValues)
                    {
                        var fieldValue = item["Application"] as TaxonomyFieldValueCollection;
                        ApplicationValue = "";
                        foreach (var valueitem in fieldValue)
                        {
                            ApplicationValue = ApplicationValue + valueitem.Label + ",";
                        }
                    }
                    else
                    {
                        var fieldValue = item["Application"] as TaxonomyFieldValue;
                        if (fieldValue != null)
                        {
                            ApplicationValue = fieldValue.Label;
                        }
                        else
                        {
                            ApplicationValue = "";
                        }
                    }

What I am doing wrong? I checked the internal name and it is "Application"

1

When you get the "Object reference not set to an instance of an object" that means that it's expecting not null value and you give it null value. The cast that you are making is returning null.

var taxField = item["Application"] as TaxonomyField;

Since you are geting the lable of only one field "Application" you can check if it is a Multiple Value Field and you don't need to make a check.

You can cast it like this:

    string ApplicationValue="";
    TaxonomyFieldValueCollection taxFieldCollection = (TaxonomyFieldValueCollection)item["Application"];
    foreach(var value in taxFieldCollection)
    {ApplicationValue += value.Label + ","
    }
    ApplicationValue.TrimEnd(',');

If its not a taxonomy value collection then cast it as TaxonomyFieldValue and just get the label no loops.

  • TrimEnd returns a new string. Last line should beApplicationValue = ApplicationValue.TrimEnd(','); – rlv-dan Aug 14 '18 at 8:41

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.