none
How to create childTerm in subTerm in a taxonomy termstore with CSOM code?

    질문

  • HI 

    I have a requirement like under a Taxanomy having 100 + term groups. Under Term group 100's Termsets.Under Termset multible terms having sub terms.

    All Countries (Term group )-->  10 Term sets--> each Term set have many Terms (100 + in each ) --> each term have again sub terms-->each sub term having terms(child terms).

    Now I will give  SubTerm, New child term  in each Textbox and click on submit.

    As per the CSOM/JSOM code, it will create New child term  under SubTerm.

    I am implementd the code but its taking more time(2 min) to create the term under subterm.

     Could you please provide the code, which would not hit the performance issue.

    I am providing my code below:

    under main Method :

                                                                     

     string cn = "MySubTerm"; // already existing under terms as a subterm
                string ctn = "childterm"; // need to create under above subterm

                string str = "";

                    string username = "XXXXXX";
                    string password = "YYYYY";
                    var securestring = new SecureString();
                    foreach (char pass in password)
                    {
                        securestring.AppendChar(pass);
                    }
                    string siteUrl = "https://XXXX.sharepoint.com/";

                    var context = new ClientContext(siteUrl);
                    context.Credentials = new SharePointOnlineCredentials(username, securestring);
                    context.Load(context.Web);
                    context.ExecuteQuery();

                    TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(context);
                   TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();            
                    context.Load(termStore,
                             store => store.Name,
                             store => store.Groups.Include(
                                 group => group.Name,
                                 group => group.TermSets.Include(
                                     termSet => termSet.Name,
                                     termSet => termSet.Terms.Include(
                                         term => term.Name,
                                         term => term.Terms.Include(
                                            subterm => subterm.Name,
                                            subterm => subterm.Terms)
                                            )
                                 )
                             )
                     );
                    context.ExecuteQuery();

                    if (taxonomySession != null)
                    {
                        if (termStore != null)
                        {
                            foreach (TermGroup group in termStore.Groups)
                            {
                                if (group.Name == "MyGroup")
                                {
                                    TermSetCollection tscoll = group.TermSets;
                                    foreach (TermSet termSet in group.TermSets)
                                    {   //First If clause Start
                                        if (termSet.Name == "MY TermSET" && termSet.Terms.Count > 0)
                                        {

                                            foreach (Term term in termSet.Terms)
                                            {
                                                if (term.Name == cn)
                                                {
                                                    if (!term.Terms.Any(t => t.Name == ctn))
                                                    {
                                                        term.CreateTerm(ctn, 1033, Guid.NewGuid());
                                                        str += "Market Name is added";
                                                    }
                                                    else
                                                    {
                                                        // Dublicate
                                                        str += "Market Name is already Exist";
                                                    }
                                                }
                                                else
                                                {
                                                    str += "State Name is required and not blank.Provide the proper existing Sate Name in the Textbox";
                                                }

                                            }   
                                        }  
                                        Console.WriteLine(str);
                                        context.ExecuteQuery();
                                    }

      }
                            }

    Provide the correct output to improve the performance using CSOM code.

    Thanks.

    2018년 5월 17일 목요일 오후 5:23

답변

  • Hi,

    Modify the code as below:

    string cn = "MySubTerm"; // already existing under terms as a subterm
    string ctn = "childterm"; // need to create under above subterm
    string sctn = "subchildterm";
    string str = "";
    
    string targetSiteURL = @"https://xxx.sharepoint.com/sites/dennis";
    
    var login = "xxx@xxx.onmicrosoft.com";
    var password = "xxx";
    
    var securePassword = new SecureString();
    
    foreach (char c in password)
    {
    	securePassword.AppendChar(c);
    }
    SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
    
    ClientContext ctx = new ClientContext(targetSiteURL);
    ctx.Credentials = onlineCredentials;
    
    TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(ctx);
    TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
    TermGroup termGroup = termStore.Groups.GetByName("MyGroup");  
    TermSet termSet = termGroup.TermSets.GetByName("MY TermSET");
    Term term = termSet.Terms.GetByName(cn);     
    Term subTerm = term.Terms.GetByName(ctn);
    ctx.Load(subTerm, t => t.Terms);
    
    ctx.ExecuteQuery();
    if (!subTerm.Terms.Any(t => t.Name == sctn))
    {
    	subTerm.CreateTerm(sctn, 1033, Guid.NewGuid());       
    	ctx.ExecuteQuery();
    	str += "Market Name is added";
    }
    else
    {
    	// Dublicate
    	str += "Market Name is already Exist";
    }
    Console.WriteLine(str);

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • 답변으로 표시됨 sudhir muvva 2018년 5월 18일 금요일 오전 10:54
    2018년 5월 18일 금요일 오전 10:03
    중재자
  • Hi,

    The following code for your reference:

    string cn = "MySubTerm"; // already existing under terms as a subterm string ctn = "childterm"; // need to create under above subterm string str = ""; string targetSiteURL = @"https://xxx.sharepoint.com/sites/dennis"; var login = "xxx@xxx.onmicrosoft.com"; var password = "xxx"; var securePassword = new SecureString(); foreach (char c in password) { securePassword.AppendChar(c); } SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword); ClientContext ctx = new ClientContext(targetSiteURL); ctx.Credentials = onlineCredentials; TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(ctx); TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); TermGroup termGroup = termStore.Groups.GetByName("MyGroup"); TermSet termSet = termGroup.TermSets.GetByName("MY TermSET"); Term term = termSet.Terms.GetByName(cn); ctx.Load(term,t=>t.Terms); ctx.ExecuteQuery(); if (!term.Terms.Any(t => t.Name == ctn)) { term.CreateTerm(ctn, 1033, Guid.NewGuid()); ctx.ExecuteQuery(); str += "Market Name is added"; } else { // Dublicate str += "Market Name is already Exist"; } Console.WriteLine(str);

    Console.ReadKey();

    Or we can use PnP PowerShell to achieve it.

    https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/new-pnpterm?view=sharepoint-ps

    https://github.com/SharePoint/PnP-PowerShell

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.


    2018년 5월 18일 금요일 오전 8:57
    중재자

모든 응답

  • Hi,

    The following code for your reference:

    string cn = "MySubTerm"; // already existing under terms as a subterm string ctn = "childterm"; // need to create under above subterm string str = ""; string targetSiteURL = @"https://xxx.sharepoint.com/sites/dennis"; var login = "xxx@xxx.onmicrosoft.com"; var password = "xxx"; var securePassword = new SecureString(); foreach (char c in password) { securePassword.AppendChar(c); } SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword); ClientContext ctx = new ClientContext(targetSiteURL); ctx.Credentials = onlineCredentials; TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(ctx); TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); TermGroup termGroup = termStore.Groups.GetByName("MyGroup"); TermSet termSet = termGroup.TermSets.GetByName("MY TermSET"); Term term = termSet.Terms.GetByName(cn); ctx.Load(term,t=>t.Terms); ctx.ExecuteQuery(); if (!term.Terms.Any(t => t.Name == ctn)) { term.CreateTerm(ctn, 1033, Guid.NewGuid()); ctx.ExecuteQuery(); str += "Market Name is added"; } else { // Dublicate str += "Market Name is already Exist"; } Console.WriteLine(str);

    Console.ReadKey();

    Or we can use PnP PowerShell to achieve it.

    https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/new-pnpterm?view=sharepoint-ps

    https://github.com/SharePoint/PnP-PowerShell

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.


    2018년 5월 18일 금요일 오전 8:57
    중재자
  • Thanks a lot. This coding is working fine.

    My requirement is to give this as a web service to the client.

    may be power shell code is not useful here.Guide me if i am wrong?

    In the same requirement if i will give a Child term , sub child term.

    click on save need to save the sub child term under child term.

    Is it work? if yes,provide the code if any modifications in the above code?

    • 편집됨 sudhir muvva 2018년 5월 18일 금요일 오전 9:34
    • 답변으로 표시됨 sudhir muvva 2018년 5월 18일 금요일 오전 10:54
    • 답변으로 표시 취소됨 sudhir muvva 2018년 5월 18일 금요일 오전 10:54
    2018년 5월 18일 금요일 오전 9:30
  • Hi,

    Modify the code as below:

    string cn = "MySubTerm"; // already existing under terms as a subterm
    string ctn = "childterm"; // need to create under above subterm
    string sctn = "subchildterm";
    string str = "";
    
    string targetSiteURL = @"https://xxx.sharepoint.com/sites/dennis";
    
    var login = "xxx@xxx.onmicrosoft.com";
    var password = "xxx";
    
    var securePassword = new SecureString();
    
    foreach (char c in password)
    {
    	securePassword.AppendChar(c);
    }
    SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
    
    ClientContext ctx = new ClientContext(targetSiteURL);
    ctx.Credentials = onlineCredentials;
    
    TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(ctx);
    TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
    TermGroup termGroup = termStore.Groups.GetByName("MyGroup");  
    TermSet termSet = termGroup.TermSets.GetByName("MY TermSET");
    Term term = termSet.Terms.GetByName(cn);     
    Term subTerm = term.Terms.GetByName(ctn);
    ctx.Load(subTerm, t => t.Terms);
    
    ctx.ExecuteQuery();
    if (!subTerm.Terms.Any(t => t.Name == sctn))
    {
    	subTerm.CreateTerm(sctn, 1033, Guid.NewGuid());       
    	ctx.ExecuteQuery();
    	str += "Market Name is added";
    }
    else
    {
    	// Dublicate
    	str += "Market Name is already Exist";
    }
    Console.WriteLine(str);

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • 답변으로 표시됨 sudhir muvva 2018년 5월 18일 금요일 오전 10:54
    2018년 5월 18일 금요일 오전 10:03
    중재자