0

I am not able to obtain the newly added managed metadata field by internal name using CSOM. However, the field the related hidden text field is available with the value. The following statement throws Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException:

Following is the code to get list items

  List lst= context.Web.Lists.GetByTitle(<ListName>);
  CamlQuery camlQuery = new CamlQuery
                    {
                        ViewXml = @"<View>  
                                         <Query> 
                                               <Where>
                                                      <Or>
                                                          <Eq>
                                                              <FieldRef Name='SCStatus' />
                                                              <Value Type='TaxonomyFieldType'>Approved</Value>
                                                          </Eq>
                                                          <Eq>
                                                               <FieldRef Name='SCStatus' />
                                                               <Value Type='TaxonomyFieldType'>UpdateRequested</Value>
                                                          </Eq>
                                                      </Or>
                                               </Where>
                                         </Query> 
                                   </View>";
                    };

  ListItemCollection listItems = lst.GetItems(camlQuery);

  context.Load(listItems);
  context.ExecuteQuery();

Following statement to fetch field value

(listItem[<InternalName>] as TaxonomyFieldValue).Label

Please note that other taxonomy fields are available just fine.

  • Was this resolved. I am facing the same issue in 2019 environment. Able to get the field from SOM and powershell, but not from CSOM. one more this is happening for one taxonomy field only rest taxonomy field are available. Any pointer would be help – Ravindra Feb 10 '23 at 06:59

1 Answers1

1

The error occurs since Load<T>(T, \[\]) method does not return the specified field, it needs to be requested explicitly.

You could consider the following options:

Using the Include method

Explicitly specify fields to retrieve via Load<T>(T, \[\]) method, for example:

var qry = new CamlQuery();
var items = list.GetItems(qry);
ctx.Load(items,
             icol => icol.Include(
                i => item.Id,
                i => item.DisplayName,
                i => item["TaxFieldName"]));  //specify taxonomy field 
ctx.ExecuteQuery();

Using the ViewFields in CAML query

Use ViewFields element to specify what fields to retrieve, for example:

var qry = new CamlQuery();
qry.ViewXml = "<View>" +
                 "<ViewFields>" + 
                    "<FieldRef Name='ID'/>" + 
                    "<FieldRef Name='Title'/>" + 
                    "<FieldRef Name='TaxFieldName'/>" +
                 "</ViewFields>" + 
              "</View>";
var items = list.GetItems(qry);
ctx.Load(items);
ctx.ExecuteQuery();
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193