This is a stumbling block that I keep hitting over and over again. Any advice or tips would be very helpful.
I prefer to use the SP REST api to accomplish all kinds of things on our site, but for certain things there are no (or not complete) REST endpoints, for example working with taxonomy or workflow services. In those cases I have to use JSOM. The objects returned by the JSOM calls are sharepoint typed objects and I just can't figure out how to explore them and figure out if they are doing what I need.
In this thread I was working with the WorkflowServices and couldn't figure out all the different properties being returned by the different calls. I find having the getEnumerator
, moveNext
over it, use different methods like get_label()
, get_name()
, get_value()
, etc. to be very clunky and and awkward way of trying to figure out what is going on. Especially given the general lack of documentation on many of these objects and types.
The solution suggested that I accepted in that thread was to map the typed object to javascript object like so:
return definitions.get_data().map(function(item) {
return item.get_objectData().get_properties();
});
Another example this thread where I was trying to work with the taxonomy terms and the synonyms/other labels for all the terms in a termset.
I used the same map technique, but didn't realize that I would need to do a second mapping to get the label collections that were attached to each term. So a double loop like this.
terms.get_data().forEach( (term) => {
term.get_labels().get_data().forEach( (t) => {
if(t.get_isDefaultForLanguage()) {
console.log("value", t.get_value());
} else {
console.log("synonym value", t.get_value());
}
});
});
I'm not even sure what the get_isDefaultForLanguage()
bit is yet.
I thought I was onto something with that first post, but obviously the hours and hours I wasted on the second problem show that I'm still not getting it.
I don't even know how many other SharePoint Collection items are returned in these and where I might need to loop again.
Here are my questions
Is there a way to loop over all the typed items recursively and just change them to a JSON/simple javascript object? Without know the structure in advance?
I can't use JSON.stringify(terms)
because it ends up with circular references.
If there isn't a way to do the above, is there a way to get the ajax response content directly? I see that in my network debugger, but I don't know how to just get it when my promise resolves in .then()
or .done()
.