1

I am only trying to retrieve the value of the Enterprise Keywords column in my SPList. I do this by:

if(item["TaxKeyword"] != null)
   Log(item["TaxKeyword"].ToString())

The only way I can find to retrieve the value of the Keywords column is to item["TaxKeyword"]. Now I have a different way of checking to see if the field exists in the item SPListItem object, which is:

if((TaxonomyField)item.Fields["Enterprise Keywords"] != null))

Which will work and is never null, but when it gets to any instance item["TaxKeyword"] the code fails with the error message:

Value does not fall within the expected range.

Does this type of column have to be treated different since it is Managed Metadata? How is it possible that the field can exist but when I try to get the value, it doesnt?

0

I was able to confirm that the reason the error is being thrown is because item["TaxKeyword"] is not specifically defined in the SPQuery.ViewFields.

I thought this interesting since I do not define ViewFields in my query. Instead I let SharePoint pull back all columns. I tested this by adding every column to the ViewFields and running the query again. I received an error telling me I exceeded the max number of lookups for a view.

Light bulb!

Here is the problem:

  1. I ask SharePoint to give me every column.
  2. The number of lookups in my list is greater than the Lookup threshold set in Central Administration.
  3. SharePoint only retrieves as many lookups as the threshold allows then stops without error.

Because of this, TaxKeyword was not retrieved. The solution is to either increase the lookup threshold or define ViewFields on your query.

0

SPFieldCollection class

In order to verify if List Item contains the specified field you could use one of the following methods from SPFieldCollection class:

For example to determine whether List Item contains Enterprise Keywords field:

if (!listItem.Fields.ContainsField("TaxKeyword")) {
}

Enterprise Keywords field

Enterprise Keywords field is of TaxonomyFieldValueCollection type

Example

//Get Enterprise Keywords value
public static TaxonomyFieldValueCollection GetTaxKeywordValue(SPListItem listItem)
{
    if (!listItem.Fields.ContainsField("TaxKeyword"))
         return null;
    return (TaxonomyFieldValueCollection) listItem["TaxKeyword"];
}

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.