2

The challenge I am facing is to get full path of the metadata column.

A solution suggest to set the IsPathRendered property to true to get the full path. but the code shared uses server side object. I need to set it through client side. Below is the solution shared

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (SPSite site = (SPSite)properties.Feature.Parent)
    {
        Guid fieldId = new Guid("{YOUR-FIELD'S-GUID-GOES-HERE}");
        TaxonomyField field = site.RootWeb.Fields[fieldId] as TaxonomyField;

        // Render full taxonomy path, not just the leaf.
        field.IsPathRendered = true;

        field.Update();
    }
}

below is my code:

Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
ListCollection lists = web.Lists;
List selectedList = lists.GetByTitle("test");
clientContext.Load<ListCollection>(lists); // this lists object is loaded successfully
clientContext.Load<List>(selectedList);  // this list object is loaded successfully
clientContext.ExecuteQuery();


CamlQuery camlQuery = CamlQuery.CreateAllItemsQuery(100);
Microsoft.SharePoint.Client.ListItemCollection listItems = selectedList.GetItems(camlQuery);

clientContext.Load<Microsoft.SharePoint.Client.ListItemCollection>(listItems); // problem here, this list items is return empty 

clientContext.ExecuteQuery();


var item = selectedList.GetItemById(4);
clientContext.Load(item);
// Execute the query to the server

clientContext.ExecuteQuery();

TaxonomyFieldValueCollection taxFieldValueColl = item["Employee_x0020_Level"] as TaxonomyFieldValueCollection;

// Loop through all the taxonomy field values
List<string> pathvalue = new List<string>();
foreach (TaxonomyFieldValue taxFieldValue in taxFieldValueColl)

{

    // Display the taxonomy field value
    pathvalue.Add(taxFieldValue.Label.);

pathvalue.Add(taxFieldValue.Label.); gives me the actual value, not the full path

  • You don't need to load web, lists, and selectedList. – dstarkowski Feb 11 '16 at 10:56
0

Setting IsPathRendered property on the field is one of the solutions. However this will affect not only values returned in CSOM, but also how the field is rendered on forms and list views.

You can set this property on newly created field this way:

using (var context = new ClientContext("http://contoso.com"))
{
     var web = context.Web;
     var list = web.Lists.GetByTitle("Test");
     var fieldXml = String.Format("<Field StaticName='{0}' Name='{0}' DisplayName='{0}' Type='TaxonomyFieldType' />", "taxonomy_field");
     var field = list.Fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.DefaultValue);

     var taxonomyField = context.CastTo<TaxonomyField>(field);
     taxonomyField.SspId = Guid.Parse("00000000-0000-0000-0000-000000000000"); //GUID of term store
     taxonomyField.TermSetId = Guid.Parse("00000000-0000-0000-0000-000000000000"); //GUID of term set
     taxonomyField.IsPathRendered = true;
     taxonomyField.Update();

     context.ExecuteQuery();
}

taxFieldValue.Label property will return term path for this field.

If you don't want to change how the field is rendered on forms, you can use TaxonomySession to get the term by ID

using (var context = new ClientContext("http://contoso.com"))
{
    var web = context.Web;
    var list = web.Lists.GetByTitle("Test");
    var item = list.GetItemById(1);
    context.Load(item);
    context.ExecuteQuery();

    var termValue = item["taxonomy_field"] as TaxonomyFieldValue;
    var guid = Guid.Parse(termValue.TermGuid);

    var taxonomy = TaxonomySession.GetTaxonomySession(context);
    var term = taxonomy.GetTerm(guid);
    context.Load(term);
    context.ExecuteQuery();

    var path = term.PathOfTerm;
}
  • Thank you so much dstarkowski. I could get the full path of matadata column of a list using var taxonomy = TaxonomySession.GetTaxonomySession(context); var term = taxonomy.GetTerm(guid); context.Load(term); context.ExecuteQuery(); var path = term.PathOfTerm; – MJaiswal Feb 12 '16 at 7:43

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.