SharePoint useful JavaScript objects, functions

There are several SharePoint inbuilt JavaScript objects / functions helps you to write script professional way and saves your time. The following are few you can explore more in SP.Init.debug.js and page sources.

1. _spPageContextInfo: the object exists in sharePoint pages
_spPageContextInfo.siteAbsoluteUrl: returns the site collection url of the current web site.
var siteUrl = _spPageContextInfo.siteAbsoluteUrl

_spPageContextInfo.webAbsoluteUrl: returns the current web site url.
var webUrl = _spPageContextInfo.webAbsoluteUrl

_spPageContextInfo.webServerRelativeUrl: returns the current web relative url
var webRelativeUrl = _spPageContextInfo.webServerRelativeUrl

2. SP.ScriptHelpers: The function implemented in SP.Init.js and used across all scripts. 
SP.ScriptHelpers.isNullOrEmptyString: this method ensure the parameter is null or empty.
var a; if(SP.ScriptHelpers.isNullOrEmptyString(a)) console.log(" empty "); else console.log(" not empty ");

SP.ScriptHelpers.isUndefined: this method ensure the parameter is undefined or defined.
var a; SP.ScriptHelpers.isUndefined(a)? console.log("is undefined"):console.log(a);

SP.ScriptHelpers.urlCombine(a,b): this method usefull to club two url’s. It will take care of ‘/’ forward slash between two urls.
var url = SP.ScriptHelpers.urlCombine(_spPageContextInfo.webAbsoluteUrl,"/_api/web/lists/getbytitle('test')/items")

SP.ScriptHelpers.newGuid(): Generates a new Guid.
Var guid =  SP.ScriptHelpers.newGuid()


Comments

Popular posts from this blog

PAGE NOT FOUND ERROR PAGE IN SHAREPOINT 2013

Hide the Upload Button of the Doucment Library

Get Host site list items in SharePoint Hosted App.