Question

I just started creating Office 365 SharePoint Online apps using Visual Studio 2013 and I'm wanting to add a term to a term set in the default term store. What I've done is added a JavaScript function in the App.js file to kick off when the document is ready. I followed an example online and used basically the same code, changing out names and such. I can deploy the app to my site but nothing is happening. The code compiles fine and no run time errors. The term is not being added to the term set. I'm brand new to CSOM so I'm wondering if I'm forgetting a load or query call. Here is the code I'm using:

$(document).ready(function () {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";

$.getScript(scriptbase + "SP.Runtime.js",
    function () {
        $.getScript(scriptbase + "SP.js", function () {

            $.getScript(scriptbase + "SP.Taxonomy.js", execOperation);

        });
    }
);

getUserName();

});

function execOperation() {
var context = SP.ClientContext.get_current();

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

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

//Term Store under which to create the group.
var termStore = termStores.getByName("Taxonomy_/ClwSKVzefj+f2V6rXnmig==");

//Term Set under which to create the term.
var termSet = termStore.getTermSet("8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f");

//Name of the term, LCID and a new GUID for the term.
var newTerm = termSet.createTerm("Shaun", 1033, "b49f64b3-4722-4336-9a5c-56c326b344a9");

context.load(newTermSet);

context.executeQueryAsync(function(){
    alert("Term Created: " + newTerm.get_name());
},function(sender,args){
    console.log(args.get_message());
});

}

I'm wondering about the line of code setting the scriptbase variable if that is the problem, but I'm not sure if it is.

Thank you in advance, Shaun

Was it helpful?

Solution

Your code worked perfectly for me, except for one thing:

context.load(newTermSet);

should be

context.load(newTerm);

Looks like you've been done in by a mistyped variable name.

EDIT Also, if you're not on the root site collection of your web app, you need to put a leading slash in front of _layouts in your script loading code.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top