Monday, August 20, 2018

Struggling REST query "substringof" with special Characters (umlaut and not latin characters)

Did you have struggle when you create SharePoint REST query's with substringof that didn't return special Characters (umlaut, not latin characters)?
If yes, then i understand your stress and here a simple that could help you in your JS development that return the expected text.

Business Case: Have a list of contacts with usernames that have special characters.
Issue: Create custom Search using SharePoint REST to return usernames, but if your have users with characters "ü, ö, ä" doesn't return any user.

var restUrl = "/_api/web/Lists/getbytitle('kontakt')/items?$select=FullName,Email&$filter=substringof( '" + term + "',FullName)";

One way to resolve:

Include in the JS a new String function to deal with special characters: 

String.prototype.toUnicode = function(){
var result = "";
for(var i = 0; i < this.length; i++){
result += "%u" + ("00" + this[i].charCodeAt(0).toString(16)).substr(-4);
return result;

When you make the REST Query, here a simple way to make this search that return special characters

var restUrl = "/_api/web/Lists/getbytitle('kontakt')/items?$select=FullName,Email&$filter=substringof( '" + term.toUnicode() + "',FullName)";

Simple article to help with the little things that take you hours of your life....

Best regards, 
André Lage

No comments: