1

I have a very annoying problem. I get the above error every time I execute below code. It happens at the second executeQueryAsync call. The error callback isn't hit nor did I find anything interesting in live ULS log. Does anybody see anything wrong with my code?

enter image description here

private bindFields() 
{
    console.log('Executing TaxonomyHelper.bindFields()');
    var self = this;
    this.list = this.context.get_web().get_lists().getByTitle(self.listName);
    this.context.load(this.list);
    this.context.executeQueryAsync(function () {
        self.competenceField = self.list.get_fields().getByInternalNameOrTitle(self.competenceFieldName);
        self.technologyTopicField = self.list.get_fields().getByInternalNameOrTitle(self.technologyTopicFieldName);
        self.context.load(self.competenceField);
        self.context.load(self.technologyTopicField);
        self.context.executeQueryAsync(function () {
            self.competenceTaxField = self.context.castTo(self.competenceField, SP.Taxonomy.TaxonomyField);
            self.competenceTaxField.set_sspId(self.defaultTermStore.get_id().toString());
            self.competenceTaxField.set_termSetId(self.competenceTermSetGuid);
            self.competenceField.updateAndPushChanges(true);

            self.technologyTopicTaxField = self.context.castTo(self.technologyTopicField, SP.Taxonomy.TaxonomyField);
            self.technologyTopicTaxField.set_sspId(self.defaultTermStore.get_id().toString());
            self.technologyTopicTaxField.set_termSetId(self.technologyTopicTermSetGuid);
            self.technologyTopicTaxField.updateAndPushChanges(true);

            self.context.executeQueryAsync(function () {
                console.log('All finshed. Cleaning up now...');
                self.cleanUp();
            },
            function (sender, args) {
                console.log('Error in TaxonomyHelper.bindFields(): ' + args.get_message());
                self.cleanUp();
            });
        },
        function (sender, args) {
            console.log('Error in TaxonomyHelper.bindFields(): ' + args.get_message());
            self.cleanUp();
        });
    },
    function (sender, args) {
        console.log('Error in TaxonomyHelper.bindFields(): ' + args.get_message());
        self.cleanUp();
    });
}

EDIT: Refactored my code as below. Still yielded the same error.

private bindFields() 
{
    console.log('Executing TaxonomyHelper.bindFields()');
    var self = this;
    this.list = this.context.get_web().get_lists().getByTitle(this.listName);
    this.competenceField = this.list.get_fields().getByInternalNameOrTitle(this.competenceFieldName);
    this.technologyTopicField = this.list.get_fields().getByInternalNameOrTitle(this.technologyTopicFieldName);
    this.context.load(this.competenceField);
    this.context.load(this.technologyTopicField);
    this.context.executeQueryAsync(function () {
        self.competenceTaxField = self.context.castTo(self.competenceField, SP.Taxonomy.TaxonomyField);
        self.competenceTaxField.set_sspId(self.defaultTermStore.get_id().toString());
        self.competenceTaxField.set_termSetId(self.competenceTermSetGuid);
        self.competenceField.updateAndPushChanges(true);

        self.technologyTopicTaxField = self.context.castTo(self.technologyTopicField, SP.Taxonomy.TaxonomyField);
        self.technologyTopicTaxField.set_sspId(self.defaultTermStore.get_id().toString());
        self.technologyTopicTaxField.set_termSetId(self.technologyTopicTermSetGuid);
        self.technologyTopicTaxField.updateAndPushChanges(true);

        self.context.executeQueryAsync(function () {
            console.log('All finshed. Cleaning up now...');
            self.cleanUp();
        },
        function (sender, args) {
            console.log('Error in TaxonomyHelper.bindFields(): ' + args.get_message());
            self.cleanUp();
        });
    },
    function (sender, args) {
        console.log('Error in TaxonomyHelper.bindFields(): ' + args.get_message());
        self.cleanUp();
    });
}
0

You are doing something with the SP.ClientContext that corrupts its internal data. But since your first executeQueryAsync is not necessary, you should have no problem to solve it.

Try this piece:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {
    var listName = 'example';
    var fieldNameA = 'yourfieldA';
    var fieldNameB = 'yourfieldB';

    var ctx = new SP.ClientContext.get_current();
    var web = ctx.get_web();
    var list = web.get_lists().getByTitle(listName);
    var listFields = list.get_fields();

    var fieldA = listFields.getByTitle(fieldNameA);
    fieldA = ctx.castTo(fieldA, SP.Taxonomy.TaxonomyField);
    fieldA.set_termSetId('valueA');
    fieldA.update();

    var fieldB = listFields.getByTitle(fieldNameB);
    fieldB = ctx.castTo(fieldB, SP.Taxonomy.TaxonomyField);
    fieldB.set_termSetId('valueB');
    fieldB.update();

    ctx.executeQueryAsync(
        function() {
            console.log('success');
        },
        function(err, args) {
            console.log('error: ', args.get_message());
        });
});
2
  • I am in a refactoring process right now and my code is too messy to test your answer. but I will get back ASAP if this worked or not. – LeonidasFett Sep 30 '16 at 13:37
  • ok I just tried this and got another error when the first castTo() function was called: "Sys.ArgumentException: The value is outside the the expected range. Parameter name: type" (Sorry, just translated from german) – LeonidasFett Sep 30 '16 at 14:55

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.