Tuesday, December 23, 2014

Some important Javascript functions which are available OOTB in Sharepoint

Some OOTB Sharepoint Javascripts (SP 2010 & 2013) which are very handy

IndexOfIllegalCharInUrlPath(string) - Used to check the validity of the URL. This takes the URL in string format and returns the index of the first available illegal character and returns negative when no illegal characters are present, this is particularly useful when a piece of the URL is taken as an input from an user.
Important thing to know about this is you need to strip certain part of the URL before checking.
http://, &, #, ? will be identified as illegal..so replace them before checking.

IndexOfIllegalCharInUrlLeafName(string) - Used to check the validity of the File name or Leaf Name in SharePoint lingo. Identifies the index of first available illegal character or negative in none are present.

Technically you should use both the functions to check the validity of the URL from user input.


ReplaceURLTokens(urlWithTokens, ctx)


SP.Utilities.UrlBuilder.urlCombine(string1, string2) - only works when the second string doesn't begin with a slash.

SP.Utilities.UrlBuilder.removeQueryString(url,key)

SP.Utilities.Utility.getLayoutsPageUrl("Settings.aspx")) - this simple little function help sreturn the server relative URL for the specific layouts page for the  current context. This is really helpful when you environment is a mix of SP2010 and SP2013 where you have upgraded few and not all.

SP.Utilities.HTTPUtility.navigateTo(URL) - this simply navigates the current page to the URL specified and nothing much. The important thing that it does is when the current page is loaded on a SP Modal dialog, the specified URL will also load in the modal dialog.

SP.ScriptUtility: Provides various general purpose functions for examining values, out of which the most useful are as follows

isNullOrEmptyString - Gets a value that indicates the specified text is null or empty or undefined
isNullOrUndefined - Gets a value that indicates the specified text is null or undefined
isUndefined - Gets a value that indicates the specified text is null
truncateToInt - Gets the largest integer value that is less than or equal to the specified number if the number is greater than 0. Otherwise, gets the smallest integer value that is greater than or equal to the number. var value = SP.ScriptUtility.truncateToInt(12.76);

parsing URL:
For Query string values:
GetURLKeyValue(NameOfTheKey, BoolNoDecode, url, BoolCaseInsensitive)

URL: http://sharepointsite/lists/mylist/editForm.aspx??ID=5&Source=%2FLists%2FCountries

GetURLKeyValue('ID', true)

SetURLKeyValue(NameOfTheKey, KeyValue, BoolDecode, url)




No comments:

Post a Comment