2
\$\begingroup\$

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:

"'SPContentType:SearchItem'&'selectproperties=prop1,prop2,prop3'&clienttype='Custom'"

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.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 5, 2016 at 11:02
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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(",") 
janos
113k15 gold badges154 silver badges396 bronze badges
answered Feb 6, 2016 at 9:32
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.