SharePoint provides some useful methods and objects in JavaScript. Followings are some methods and objects which I use frequently in my client side code:
SP.ScriptUtility
SP.ScriptUtility provides methods and an emptyString field. It contains following methods:- isNullOrEmptyString - Checks if the specified text is null, an empty string or undefined
- isNullOrUndefined - Checks if the specified object is null or undefined.
- isUndefined - Checks if the specified object is undefined.
- truncateToInt - Returns largest integer value that is less than or equal to the specified number if the number is greater than 0 else returns the smallest integer value that is greater than or equal to the number.
SP.Guid
SP.Guid.newGuid().toString() return new guid._spPageContextInfo
_spPageContextInfo provides following properties:- pageListId - Get Current List Guid
- pageItemId - Get id for current item.
- serverRequestPath - Get current server requested relative path.
- siteAbsoluteUrl - Get current site absolute URL
- siteServerRelativeUrl - Get current site relative url
- userId - Get current user ID
- webTitle - Get current web Title
- webAbsoluteUrl - Get current web absolute Url
- webServerRelativeUrl - Get web server relative url. For exmaple "/teams/SITE_NAME"
escapeProperly(str)
escapeProperly function returns the encoded value for provided string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var encodedString = escapeProperly("SharePoint 2013 & JavaScript"); // Returns "SharePoint%202013%20%26%20JavaScript" |
unescapeProperly(str)
unescapeProperly function returns decoded string for provided encoded string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var decodedString = unescapeProperly("SharePoint%202013%20%26%20JavaScript"); // Returns "SharePoint 2013 & JavaScript" |
Cookies
GetCookie(str)
GetCookie function returns the value for the cookie. It returns null in case cookie is not available.
DeleteCookie(str)
DeleteCookie function remove the cookie by setting expiry to minimum DateTime.
SetCookie(str, str)
SetCookie function adds session cookie. But this is not very useful function as it adds value as true or false. Its only useful if you want to set a flag. Second parameter is optional. If you pass second parameter then value of cookie will be "true" else its "false".
JSRequest
You can use JSRequest object to get the FileName, PathName and QueryString. To use the JSRequest object, you must initialize it using EnsureSetup method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialize JSRequest | |
JSRequest.EnsureSetup(); | |
var fileName = JSRequest.FileName; // Returns the webpage file name. For example "default.aspx" | |
var pagePath = JSRequest.PathName; // Returns the relative path. For example "/Sites/SITE_NAME/default.aspx" | |
var querystringValue = JSRequest.QueryString["QUERYSTRING_NAME"]; // Returns the querystring value if exists else returns undefined |
String.format(str, arguments)
You can apply formatting to string using format method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var stringValue = String.format("{0} and {1} is fun", "SharePoint", "JavaScript"); // Returns "SharePoint and JavaScript is fun" |
GetUrlKeyValue
GetUrlKeyValue is very useful function when you want to get the querystring values. It can return the querystring value from current url or provided url. Followings are the ways you can use GetURLKeyValue method:
GetUrlKeyValue(str)
It will return the querystring value for provided parameter name from current url.
GetUrlKeyValue(str, bool)
It will return the encoded or decoded querystring value for provided parameter name from current url on the basis of second parameter.
GetUrlKeyValue(str, bool, str)
It will return the encoded or decoded querystring value for provided parameter name from given url.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For example current Url = http://MYWEBAPPLICATION/sites/MYSITE/default.aspx?Param1=SharePoint%20%26%20JavaScript&Param2=100 | |
var qs1 = GetUrlKeyValue("Param2"); // Returns "100" | |
var qs2 = GetUrlKeyValue("Param1"); // Returns "SharePoint & JavaScript" | |
var qs3 = GetUrlKeyValue("Param1", true); // Returns "SharePoint%20%26%20JavaScript" | |
var qs3 = GetUrlKeyValue("Param1", false); // Returns "SharePoint & JavaScript" | |
var qs4 = GetUrlKeyValue("q", true, "http://www.google.com?q=SharePoint%20%26%20JavaScript"); // Returns "SharePoint%20%26%20JavaScript" | |
var qs4 = GetUrlKeyValue("q", false, "http://www.google.com?q=SharePoint%20%26%20JavaScript"); // Returns "SharePoint & JavaScript" |
STSHtmlEncode(str)
STSHtmlEncode function ecodes the provided HTML string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var encodedHtml = STSHtmlEncode("<div>This is HTML</div>"); // Returns "<div>This is HTML</div>" |
STSHtmlDecode(str)
STSHtmlDecode function decodes the provided string into HTML.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val decodedHtml = STSHtmlDecode("<div>This is HTML</div>"); // Returns "<div>This is HTML</div>" |
No comments:
Post a Comment