1

Is there's a way to Return a SharePoint 2013 termset in a tree structure using CSOM ?

0

Here is a code that produce this XML structure:

<Field DisplayName="Doc Type" Type="Managed Metadata" InternalName="Doc_x0020_Type">
    <Value>-1;#Term A|8e3217b1-f15d-4049-83f0-02b7cb168cb3
      <Value>
        <Value>-1;#Term A1|3f0cc693-4488-41ec-9158-972e9fac140e
            <Value>-1;#Term A11|3f0cc693-4488-41ec-9158-972e9fac140e</Value>
            <Value>-1;#Term A12|50fe67ae-8ced-471d-a1b1-adeb3266fb45</Value>
        </Value>
      </Value>
    </Value>
    <Value>-1;#Term B|6f45adfe-c64a-481e-bf29-a8f96d8c1791
        <Value>-1;#Term B1|a0a89c0f-4c1b-489e-9b24-bf2648fa36d6</Value>
        <Value>-1;#Term B2|196a4b08-31f2-4837-a9d3-5241d372c4e2
            <Value>-1;#Term B21|1e4d2d7b-19ab-4d82-9ab5-62f7f02c355c</Value>
            <Value>-1;#Term B22|2908460c-a16b-42dc-ac93-55f5f7cd83c9</Value>
        </Value>
    </Value>
    <Value>-1;#Term C|9bccd99c-c957-4d1a-a0c9-fef3e7ad0140</Value>
</Field>
<Field DisplayName="...">...

You can modify it to have what you need, in this case the code get the information to connect to the term store from a field, but if you already have it yo can avoid the field part:

public static List<XElement> GetTaxonomyFieldsXElements(ClientContext pContext, List<TaxonomyField> pFields)
{
    List<XElement> lFieldsXElements = new List<XElement>();
    TaxonomySession lTaxonomySession = TaxonomySession.GetTaxonomySession(pContext);

    foreach (TaxonomyField lTaxField in pFields)
    {
        XElement lXField = new XElement("Field");
        lXField.Add(new XAttribute("DisplayName", lTaxField.Title));
        lXField.Add(new XAttribute("Type", lTaxField.TypeDisplayName));
        lXField.Add(new XAttribute("InternalName", lTaxField.InternalName));

        Guid lTermStoreId = lTaxField.SspId;
        Guid lTermSetId = lTaxField.TermSetId;

        TermStore lTermStore = lTaxonomySession.TermStores.GetById(lTermStoreId);
        TermSet lTermSet = lTermStore.GetTermSet(lTermSetId);
        TermCollection lTermColl = lTermSet.Terms;

        pContext.Load(lTermColl, tms => tms.Include(t => t.Name, t => t.Id, t => t.TermsCount, t => t.Terms, t => t.IsAvailableForTagging));
        pContext.ExecuteQuery();

        if (lTermColl.Count > 0)
        {
            foreach (Term lTerm in lTermColl)
            {
                if (lTerm.IsAvailableForTagging)
                {
                    XElement lXValue = GetTermValueXElement(pContext, lTerm);
                    lXField.Add(lXValue);
                }
            }
        }

        lFieldsXElements.Add(lXField);
    }

    return lFieldsXElements;
}

private static XElement GetTermValueXElement(ClientContext pContext, Term pTerm)
{
    XElement lXValue = new XElement("Value");
    lXValue.SetValue(string.Format("-1;#{0}|{1}", pTerm.Name, pTerm.Id.ToString()));

    if (pTerm.TermsCount > 0)
    {
        pContext.Load(pTerm, t => t.Name, t => t.Id, t => t.TermsCount, t => t.Terms, t => t.IsAvailableForTagging);
        pContext.ExecuteQuery();

        foreach (Term lInnerTerm in pTerm.Terms)
        {
            if (lInnerTerm.IsAvailableForTagging) //this is not necesary
            {
                XElement lXInnerValue = GetTermValueXElement(pContext, lInnerTerm);
                lXValue.Add(lXInnerValue);
            }
        }
    }

    return lXValue;
}

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.