I'm trying to build a URL to call the SharePoint Search REST API in TypeScript. The interesting part is building the querytext, which is done by my buildQueryText method. In my case it takes two parameters: A content type to search for and a array of select properties.

buildQueryText(contentType: string, selectProperties: string[]): string {
    var contentTypeQuery = "'" + "SPContentType:" + contentType + "'";

    var selectPropertiesQuery = "";
    if (selectProperties.length !== 0) {
        selectPropertiesQuery = "&selectproperties=" + "'";
        selectProperties.forEach((property, index) => {
            if (index < selectProperties.length - 1) {
                selectPropertiesQuery += property + ",";
            } else {
                selectPropertiesQuery += property + "'";

    return `${contentTypeQuery}${selectPropertiesQuery}&clienttype='Custom'`;

I'm calling the method like this:

buildQueryText("SearchItem", ["prop1", "prop2", "prop3"])

and expecting the following output:


I'm looking for a way to improve the selectPropertyQuery part and make it more readable, as it seems quite messy. And i'm aware that there is no real validation of the inputs.


It looks to me as you want to join the array. I don't know TypeScript, it it should have a join method on the array.

selectPropertyQuery = properties.join(",")  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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