3

I want to get parent term set id in SharePoint online using term_parent.get_id(); in below code.I am getting all terms but unable to get parent term id.It is giving me error as

Uncaught Error: Object reference not set to an instance of an object on server. The object is associated with property Parent.

Even loading Parent property is not working.

Please help.

var terms = termSet.getAllTerms();
context.load(terms, 'Include(IsRoot, Labels, TermsCount, CustomSortOrder, Id, IsAvailableForTagging, Name, PathOfTerm, Parent, TermSet.Name)');
context.executeQueryAsync(function(){
var termEnumerator = terms.getEnumerator(); 

while(termEnumerator.moveNext()){
    var currentTerm = termEnumerator.get_current();
    var term_name = currentTerm.get_name();
    var term_id = currentTerm.get_id();
    var term_customsortoder = currentTerm.get_customSortOrder();
    var term_parent = currentTerm.get_parent();
    var terms_count = currentTerm.get_termsCount();
    var term_path = currentTerm.getPath();
    var term_allterms = currentTerm.getTerms();
    var parent_id = term_parent.get_id();         
} 
1

This is happening because you haven't explicitly requested for Parent Id. Try including that in the load statement

'Include(IsRoot, Labels, TermsCount, CustomSortOrder, Id, IsAvailableForTagging, Name, PathOfTerm, Parent, Parent.Id, TermSet.Name)'

You should check whether the current term is root or not as follows

if(currentTerm.get_isRoot() == false) {
    alert(currentTerm.get_parent().get_name());
}
  • 1
    No its not working – A_____ Mar 24 '15 at 11:53
  • Yes it is not null returning SP.Taxonomy.Term – A_____ Mar 24 '15 at 11:57
  • Yes I get it alert(currentTerm.get_parent().get_name()); working fine with name alert. Even I get the currentTerm.get_parent() with all properties in console – A_____ Mar 24 '15 at 12:03
  • Is there something to reload term to get id? – A_____ Mar 24 '15 at 12:05
  • It should work with load(Include.. check this link social.msdn.microsoft.com/Forums/sharepoint/en-US/… – Amal Hashim Mar 24 '15 at 12:09
1

Via C# / CSOM:

    var taxonomySession = TaxonomySession.GetTaxonomySession(ctx);
    var termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
    var allTerms = termStore.GetTermSetsByName("Intranet Navigation", 1033);
    var termSet = allTerms.GetByName("Intranet Navigation");

    var terms = termSet.GetAllTerms();
    ctx.Load(terms, includes => includes.Include(i => i.Id, i => i.Name, i => i.Parent, i => i.Parent.Id, i => i.Parent.Name, i => i.IsRoot, i => i.LocalCustomProperties));
    ctx.ExecuteQuery();
    foreach (var term in terms)
    {
...

Via Javascript / JSOM:

    //Current Context
    var context = SP.ClientContext.get_current();

    //Current Taxonomy Session
    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);

    //Term Stores
    var termStores = taxSession.get_termStores();

    //Name of the Term Store from which to get the Terms.
    var termStore = termStores.getByName("Managed Metadata Service");

    //GUID of Term Set from which to get the Terms.
    var tsets = termStore.getTermSetsByName("Intranet Navigation", 1033);
    var termSet = tsets.getByName("Intranet Navigation")

    var terms = termSet.getAllTerms();

    context.load(terms, 'Include(Name, Parent, IsRoot, LocalCustomProperties)');

    context.executeQueryAsync(function () {

        var termEnumerator = terms.getEnumerator();

        var termList = "Terms: \n";

        while (termEnumerator.moveNext()) {

            var currentTerm = termEnumerator.get_current();
            debugger;
            termList += currentTerm.get_name() + "\n";

        }
0

I've had this problem a few times now, and if you are using reusable terms this could be the source of your problem.

If the original source term has been deleted this is when you get this error message. You can check if the term has been orphaned by looking in the Term Store.

enter image description here

I now have javascript similar to the following in my code

var term = termStore.getTerm(termId)
        var parentTerm = term.get_parent();
        ctx.load(parentTerm);
        ctx.executeQueryAsync(function () {
            if (parentTerm.get_serverObjectIsNull())
            {
                //Display error message about orphan term.
            }

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.