Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Thursday, April 23, 2015

Some useful JavaScript methods and objects in Sharepoint

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.

unescapeProperly(str)

unescapeProperly function returns decoded string for provided encoded string.

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.


String.format(str, arguments)

You can apply formatting to string using format method.


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.


STSHtmlEncode(str)

STSHtmlEncode function ecodes the provided HTML string.

STSHtmlDecode(str)

STSHtmlDecode function decodes the provided string into HTML.

Tuesday, March 31, 2015

Processing message for long code operations in SharePoint

As a SharePoint Developer you may need to show spinning wheel or Processing sign while lengthy code operations. You have probably seen this while creating new web application or site collection in Central Administration. You may need to show the processing messages for server side or client side long operation code. Showing processing message improves the user experience. SharePoint provides some nice ways to show processing message while long running code operations.

Server side code


On Server side, if you are executing some code that takes longer to process then you can use SPLongOperation Class to show processing request message. SPLongOperation would require redirecting the user to the destination page. Destination page could be same page or confirmation page. Following is the sample code to get the processing message for server side code:

Once long code is executed, you can end the operation by calling End() method. If you are calling SPLongOperation in modal popup then call EndScript() method to close the current dialog box. For example:


Client side code


For client side long operations, SharePoint provides following options to show wait screen dialog:

SP.UI.ModalDialog.showWaitScreenSize(title, message, callbackFunc, height, width) Method

Displays a wait/loading modal dialog with the specified title, message, height and width. Height and width are defined in pixels. Cancel button is shown. If user clicks it, the callbackFunc is called.

SP.UI.ModalDialog.showWaitScreenWithNoClose(title, message, height, width) Method

Displays a wait/loading modal dialog with the specified title, message, height and width. Height and width are defined in pixels. Cancel/close button is not shown.
Following is the code example to show the wait dialog on client side: