logo

NJP

[External Search Source]

Import · Sep 17, 2022 · article

Background: I was looking for a solution on how to read html tags without using DOM in a perfect way for a long time. By using Regular Expressions, you can do almost everything. We will now see an example where anyone can think of using the Regular expressions when we don't get a proper solution.

Example: I have taken Microsoft Support Search as an example for using regular expressions instead of using a Microsoft Graph Connector/Microsoft Graph API. However, for this kind of scenario, I don't see any solution provided by either Microsoft Graph API / in the community (but please do a comment if it has).

Microsoft Support Website: Click Here

Here I used the term as "Microsoft Teams" and we got the results for the same as below.

image

image

If you observe the Search Results URL, it will be something like this

https://support.microsoft.com/en-US/search/results?query=microsoft+teams&isEnrichedQuery=false

And if you right click on the page and click on "View page source". You will see all the HTML code of the results.

image

Now, we will see how this results can be included as an "External Search Source" to the Portal search in Servicenow.

Open your Instance ==> go to the Portals ==> Click on "SP" Portal ==> Under the "Search Sources" create a new Search source called "Microsoft Support Search"

image

On this Search Source, Copy the below code into "Search Page Template"

image

Search page template

<div>
  <a href="{{item.url}}" class="h4 text-primary m-b-sm block" target="_blank">
    <span ng-bind-html="highlight(item.primary, data.q)"></span>

  </a>
  <span>{{item.short_desc}}</span>

</div>

On the "Data fetch script" use the below script (Make sure you check the option for "Is scripted source" to true as below and copy the code and save this source.

image

Data fetch script

(function(query) {
    var results = [];
    /* Calculate your results here. */
    try {
        var enQuery = GlideStringUtil.urlEncode(query);
        var eURL = "https://support.microsoft.com/en-US/search/results?query=" + enQuery;
        var ws = new sn_ws.RESTMessageV2();
        ws.setHttpMethod("get");
        ws.setEndpoint(eURL);
        var jsonOutput = ws.execute();
        if (jsonOutput) {
            var responseBody = JSON.stringify(jsonOutput.getBody());
            responseBody = responseBody.replaceAll('\\r', ' ');
            responseBody = responseBody.replaceAll('\\t', ' ');
            responseBody = responseBody.replaceAll('\\n', ' ');
            responseBody = responseBody.replaceAll('\\', '');
            responseBody = responseBody.replaceAll('"', '');
            responseBody = responseBody.replaceAll("<a class=header href=", "data-si-area");
            var allurls = responseBody.match(/data-si-area(.*?)data-bi-area/g);
            if (JSUtil.notNil(allurls)) {
                if (allurls.length > 0) {
                    for (var u = 0; u < allurls.length; u++) {
                        var mm = allurls[u].toString().replaceAll("data-bi-area", "");
                        mm = mm.replaceAll("data-si-area", "");
                        var labelurl = mm.split("aria-label=");
                        var sdesc = labelurl[1].toString().trim().replaceAll("<b>", "");
                        try {
                            sdesc = removeTags(sdesc);
                        } catch (ee) {
                            sdesc = labelurl[1].toString().trim().replaceAll("<b>", "");
                        }
                        sdesc = sdesc.replaceAll("</b>", "");
                        var rslt = {
                            "url": labelurl[0].toString().trim(),
                            "target": "_blank",
                            "primary": labelurl[1].toString().trim(),
                            "short_desc": sdesc
                        };
                        results.push(rslt);
                    }
                }
            }
        }
    } catch (e) {

    }

    return results;
})(query);

function removeTags(str) {
    var a = str.replace(/<\/?[^>]+(>|$)/g, ""); 
    var b = a.replace(/&amp;/g, '&'); 
    return b.replace(/&#(\d+);/g, function(match, dec) {
        return String.fromCharCode(dec);
    });
}

Now, go to the SP Portal and type "Microsoft teams" like how you used in Microsoft Support site. You will see the results something like this.

Note: The results may vary based on the priority of that article by Microsoft at that point of time, but the results that you see is entirely from Microsoft Support site by just reading the HTML response

If you observe the above code, its completely reading a html tags by using the Regular expressions & String functions. In future, if the Microsoft support uses a different pattern, you may change your logic based on that..

Results in Servicenow Portal

image

Please do a comment if you have any other solution for reading HTML tags in a better way on Server side.

Thanks,

Narsing

View original source

https://www.servicenow.com/community/now-platform-articles/external-search-source-add-microsoft-support-search-source-sp/ta-p/2322817