Original article from the blog SharePoint with Attitude…
— Karla Ponce —
In SharePoint 2013 JavaScript becomes an essential development skill to build SharePoint solutions, developers must be comfortable using it. We will be using JavaScript to build, customize and extend SharePoint functionality, however it makes no sense to implement custom code if SharePoint already offers a capability. There are many JavaScript functions provided by SharePoint that we can use in our solutions, requiring less custom code from our part, here I will review the ones that I find more useful:
List Views
- SelectAllItems. Marks all items as selected in the list view.
- DeselectAllItems. Marks all items as unselected in the list view.
- CountSelectedItems(ctx). Returns number of items selected in the list view.
- SelectRowByIndex(ctx, idx, addToSelection). Marks the specified row as selected in the list view, the last parameter indicated whether previous selected items should remain selected or cleared.
- GetListItemByID(ctx, id). Returns an item from the list view given the id property, id must be passed as string.
Urls
- GetUrlKeyValue(keyName, bNoDecode, url, bCaseInsensitive). Gets a value from the query string. Returns empty string if keyName doesn’t exist:
- keyName: name of the key for which we need to retrieve the value.
- bNoDecode: boolean indicating if we want the value decoded
- url: the url that contains the query string to parse
- bCaseInsensitive: boolean indicating if keyName is case sensitive
- SetUrlKeyValue(keyName, keyValue, bEncode, url). Sets a value to the query string.
- keyName: name of the key for which we need to set the value.
- keyValue: value you want to set.
- bEncode: boolean indicating if we want the value to be decoded before being set.
- url: the url where we want the key-value pair amended
- IndexOfIllegalCharInUrlPath(string). Receives a string with the url and returns the first illegal character. Returns -1 if no illegal characters found. (http:// and https:// should be stripped from the url string)
- IndexOfIllegalCharInUrlLeafName(string). Similar as above but for file names. Receives a string with the file name and returns the first illegal character.
- ReplaceUrlTokens(urlWithTokens, ctx). Replaces all instances of the following SharePoint specific tokens in a url string with the correspondent value: {ItemId}, {ItemUrl}, {SiteUrl}, {ListId}, {Source}, {ListUrlDir}
- SP.Utilities.UrlBuilder.urlCombine(string1, string2). Combines the two string parameters without redundant slashes between them; however the second parameter should not begin with a slash, otherwise this doesn’t work.
- SP.Utilities.UrlBuilder.removeQueryStringUrl(url, key). Removes the specified key from the query string of the specified url.
- SP.Utilities.UrlBuilder.getLayoutsPageUrl(pageFileName). Return the proper serve relative url for the specified layout page.
- SP.Utilities.UrlBuilder.navigateTo(url). Takes the browser to the specified url.
SP.ScriptUtility
- isNullOrEmptyString. Gets a value that indicates whether the specified text is null or empty string.
- isNullOrUndefined. Gets a value that indicates whether the specified object is null or undefined.
- isUndefined. Gets a value that indicates whether the specified object is undefined.
- truncateToInt. Gets the largest integer values that is less than or equal to the specified number if the number is greater than zero, otherwise it returns the smallest integer value that is greater than or equal to the number.
Other
- STSHtmlEncode. Encodes a html string.
- STSHtmlDecode. Decodes a html string.
- Browseris(). Browser identification via UserAgent string parsing.
- GetUniqueNumer(). Gets a unique incremented number, starting with 0. It is unique only for the current page loaded, it resets after new page loads.
- GoToPage(url, onlyUseExistingSource). Redirect to specified url. onlyUseExistingSource allows you to specify your own source parameter: If onlyUseExistingSource is false, and the specified url doesn’t contain a source parameter, a source parameter is added pointing to the current page.