1

I am able to query and get an array of all my term names and guids within a Term Set, but now I want to be able to set these via REST. When I look at the structure in JSON the section with my Managed Metadata appears as follows. It seems like a sub-array

"TextField": "My Text",
"MMField": {
    "Label": "7",
    "TermGuid": "7d5e44d8-e5a7-4eb7-9735-7b51cea64aa8",
    "WssId": 7
},

Using AngularJS I have tried posting to it via the following:

 var itemEntry = new appItems;

        $scope.createEvent = function () {
             // Taxonomy Value Posting
            itemEntry.TextField = 'Hooray Text';
            itemEntry.MMField.Label = '7';
            itemEntry.MMField.TermGuid = '7d5e44d8-e5a7-4eb7-9735-7b51cea64aa8';
            itemEntry.MMField.WssId = '7';
            itemEntry.$save(function () {
                $location.path('/');
            });
        }

However, it is failing on the first option of MMField.Label as follows:

Unable to set property 'Label' of undefined or null reference

Using REST I should be able to hit the straight values and set this, but I must be missing something obvious. Any clues/what am I missing to hit the sub values within MMField?

2 Answers 2

5

It probably fails because you haven't set itemEntry.MMField = {};

Try using object literal notation (note __metadata):

itemEntry.MMField = { 
  __metadata: { type: "SP.Taxonomy.TaxonomyFieldValue" }, 
  Label: '7',
  TermGuid: "7d5e44d8-e5a7-4eb7-9735-7b51cea64aa8", 
  WssId: -1 
};

Update:

An alternative solution is to set HTTP request header Content-Type to application/json;odata=nometadata (ref blog and ref msdn).

Then the object literal notation would look like this:

temEntry.MMField = { 
  Label: "7", 
  TermGuid: "7d5e44d8-e5a7-4eb7-9735-7b51cea64aa8", 
  WssId: -1 
};
16
  • It's still throwing a "Unable to get property 'value' of undefined or null reference" error. I have tried with both no metadata and verbose options
    – Kode
    Commented Jun 11, 2015 at 13:04
  • That would be a new error. In your question the error message is specifically about Label, which makes sense. Do you try to set a property value in the same manner as you were trying to set Label? (Perhaps pastebin/gist your code, as I suspect it differs from that in your question)
    – eirikb
    Commented Jun 11, 2015 at 13:08
  • My fault. it was for another field. That is now resolved. It is clicking but giving me an 400 error/Bad Request when I try to send the Managed Metadata
    – Kode
    Commented Jun 11, 2015 at 13:13
  • 1
    Did you add { __metadata: { type: 'SP.ListItem' }} to itemEntry? You might have to send the actual type as well, you can get this from /_api/Lists('...')/ListItemEntityTypeFullName
    – eirikb
    Commented Jun 11, 2015 at 13:20
  • 1
    Just do this: itemEntry.__metadata = { type: 'SP.ListItem' }; And no you don't have to use ngresource
    – eirikb
    Commented Jun 11, 2015 at 13:27
0

Thanks to @erikb, I needed the proper format of my code. I was using dot notation, but needed to hit the array as follows. Please note that I am using the nometadata option in my headers, so my JSON does not have the verbose aspects such as __metadata: { type: "SP.Taxonomy.TaxonomyFieldValue" }.

 // Create item
        var itemEntry = new appItems;

        $scope.createEvent = function () {
            console.log("Clicked");
            itemEntry.CategoryId = $scope.vm.selectedCategory.value;
            itemEntry.Title = $scope.vm.eventdescription;
            // Taxonomy Value Posting
            itemEntry.MMField = {
                Label: $scope.term.selected.termName,
                TermGuid: $scope.term.selected.termGUID,
                WssId: -1
            }
            itemEntry.$save(function () {
                $location.path('/');
            });
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.