A while back I worked on a small library with the SharePoint Online’s taxonomy, building the tree structure, and filtering the taxonomy data from SharePoint using JavaScript.
While most of the information can be found at SP.Taxonomy.js namespace in MSDN and many resources online. But I’m struggling with one particular method of Term, according to the MSDN, the function to search for a term under a term will be
SP.Taxonomy.Term.getTerms(termLabel, lcid, defaultLabelOnly, stringMatchOption, resultCollectionSize, trimUnavailable)
This is different than searching for a term under the term store or term set level, which need to use the SP.Taxonomy.LabelMatchInformation for the request. So the code for searching for term in a term will look something like this:
var session = SP.Taxonomy.TaxonomySession.getTaxonomySession(spContext);
var termStore = session.getDefaultSiteCollectionTermStore();
var term = termStore.getTerm(new SP.Guid(termIdString));
var terms = term.getTerms("Fresh", 1033, true, SP.Taxonomy.StringMatchOption.startsWith, 10, true);
spContext.load(terms);
spContext.executeQueryAsync(Function.createDelegate(this, function () {
console.log(terms);
}), Function.createDelegate(this, function () {
console.log("Failed to request");
}))
But instead of getting the terms, I’m getting error:
[
{
"SchemaVersion":"15.0.0.0","LibraryVersion":"16.0.6105.1205","ErrorInfo":{
"ErrorMessage":"Specified argument was out of the range of valid values.\r\nParameter name: lcid","ErrorValue":null,"TraceCorrelationId":"d223cb9d-30df-3000-bf8d-d6cee9f749f3","ErrorCode":-2146233086,"ErrorTypeName":"System.ArgumentOutOfRangeException"
},"TraceCorrelationId":"d223cb9d-30df-3000-bf8d-d6cee9f749f3"
}
]
Regardless what I pass in, the error message is the same. I have been googling for the solution for 1 whole day, even venture down in 3rd page of the search results, still, seems like no one is using the Term.GetTerms method. So I decided to test it with the server side CSOM, and to my surprise, it works.
So I inspect the request in Fiddler, and found there is different between the request made by client side and server side. For server side request, we can see that from the request payload, there is a parameters for my search label and LCID:
...
<Method Id="799" Name="GetTerms" ParentId="796">
<Parameters>
<Parameter Type="String">Fresh</Parameter>
<Parameter Type="Int32">1033</Parameter>
<Parameter Type="Boolean">true</Parameter>
<Parameter Type="Enum">0</Parameter>
<Parameter Type="Int32">10</Parameter>
<Parameter Type="Boolean">true</Parameter>
</Parameters>
</Method>
...
While the request made by client side only have one parameter:
<Method Id="173" ParentId="170" Name="GetTerms">
<Parameters>
<Parameter Type="String">Fresh</Parameter>
</Parameters>
</Method>
Obviously the function didn’t send the rest of my parameters to SharePoint, no wonder regardless whatever I do, I keep getting error around LCID. So in order to find out how to send the correct request without figuring out how to construct the request payload myself, I decided to have a look at the SP.Taxonomy.js in SharePoint 2013. Lucky enough, the SharePoint 2013 version of that file have the correct version of the method:
getTerms: function(g, h, e, d, c, f) {
a:;
var a = this.get_context(),
b;b = new SP.Taxonomy.TermCollection(a, new SP.ObjectPathMethod(a, this.get_path(), "GetTerms", [g, h, e, d, c, f]));
return b
}
Instead of what I get from the sp.taxonomy.js from SharePoint Online:
getTerms: function(c) {
a: ;var a = this.get_context(), b;
b = new SP.Taxonomy.TermCollection(a,new SP.ObjectPathMethod(a,this.get_path(),"GetTerms",[c]));
return b
}
So to solve my problem is to implement the following after the sp.taxonomy.js is loaded from SharePoint.
SP.Taxonomy.Term.prototype.getTerms = function (g, h, e, d, c, f) {
a:; var a = this.get_context(), b;
b = new SP.Taxonomy.TermCollection(a, new SP.ObjectPathMethod(a, this.get_path(), "GetTerms", [g, h, e, d, c, f])); return b
};
By no mean this is a good solution, but it get the work done at the moment. I’ve already sent feedback to Microsoft on that MSDN page, hope it will get fix soon.