SharePoint provides the JavaScript API reference files that contains the information used to access the data and build the custom applications. From the number of files, SP.JS is the most important file in the JSOM that provides the types and members the same as the Microsoft.SharePoint namespace to work with top-level sites, sub-sites and their lists and libraries.
The SP.JS file has the ClientContext object used for representing the context of SharePoint Objects and operations.
Here I will list each property of SP.ClientContext with an example.
SP.ClientContext.applicationName
Represent the name of the runtime application, where the current client application is located.
The following example gets the name of the application that runs in the current location.
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- appname = clientContext.get_applicationName();
- console.log("Application Name: "+ appname );
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
Returns the default value
Application Name: JavaScript Library
The following example sets the name for the runtime application, where the current client application is located. The parameter represents the name of the runtime application.
The string length for the application name should be between 1 and 265 characters.
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- clientContext.set_applicationName(“My Application”);
- appname = clientContext.get_applicationName();
- console.log("Application Name: "+ appname );
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
Application Name: My Application
SP.ClientContext.current
- Returns the current client context of the Client-Side Object Model (CSOM) runtime
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- }
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
Gets or sets a Boolean value that indicates whether form digest handling is enabled. The following example retrieves the value for the current client context form digest settings is enabled or not.
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- console.log("Form Digest Handling Enabled: " + clientContext.get_formDigestHandlingEnabled());
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- clientContext.set_formDigestHandlingEnabled(true);
- console.log("Form Digest Handling Enabled: " + clientContext.get_formDigestHandlingEnabled());
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
Form Digest Handling Enabled: true
SP.ClientContext.hasPendingRequest
Returns a Boolean value indicates whether the client runtime context has any pending request. The following example identifies the pending request.
- var clientContext = null;
- var oWeb = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- oWeb = clientContext.get_web();
- clientContext.load(oWeb);
- //Below code returns true, because the code awaiting to send the request
- console.log("Has Pending Request (Before Execution): "+ clientContext.get_hasPendingRequest());
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- //Below code returns false
- console.log("Has Pending Request (After Execution): "+ clientContext.get_hasPendingRequest());
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
Has Pending Request (Before Execution): true
Has Pending Request (After Execution): false
SP.ClientContext.requestTimeout
Gets or sets the timeout value for the current client context runtime. The following example code returns the default timeout value.
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- //Returns the default timeout value 180000 milliseconds
- console.log("Request Timeout Value: "+ clientContext.get_requestTimeout());
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
Request Timeout Value: 180000
The following example code sets the timeout value to “20000” milliseconds for the current client context runtime. The parameter value should be greater than or equal to 0. 0 represents the no timeout for the current application.
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- clientContext.set_requestTimeout(20000);
- //Returns the default timeout value 20000 milliseconds
- console.log("Request Timeout Value: "+ clientContext.get_requestTimeout());
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
Request Timeout Value: 20000
SP.ClientContext. get_serverLibraryVersion
Returns the build version of Microsoft.SharePoint.Client.ServerRuntime.dll on the server.
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
- function onSucceeded() {
- //Returns the version of Microsoft.SharePoint.Client.ServerRuntime.dll installed on the server
- console.log("Server Library Version: "+ clientContext.get_serverLibraryVersion());
- }
Server Library Version: 15.0.4569.1501
SP.ClientContext.serverSchemaVersion
Returns the installed schema version of Microsoft.SharePoint.Client.ServerRuntime.dll on the server. The following example returns the schema version of the client context DLL from the current server.
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
- function onSucceeded() {
- //Returns the schema version of Microsoft.SharePoint.Client.ServerRuntime.dll installed on the server
- console.log("Server Schema Version: "+ clientContext.get_serverSchemaVersion());
- }
Server Schema Version: 15.0.0.0
SP.ClientContext.serverVersion
Returns the current SharePoint version. The following example returns the SharePoint version of the current client context.
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
- function onSucceeded() {
- //Returns the version of SharePoint
- console.log("SharePoint Server Version:"+ clientContext.get_serverVersion());
- }
SharePoint Server Version: 15.0.4569.1501
SP.ClientContext.traceCorrelationId
Gets or sets the trace correlation id of the current context application. The following example gets the correlation id from the current runtime application.
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- //Returns the null, because the request is not executed. It returns the value on the execution of request.
- console.log("Trace Correlation ID (Before Execution): "+ clientContext.get_traceCorrelationId());
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
- function onSucceeded() {
- //Returns the value of correlation id
- console.log("Trace Correlation ID (After Execution): "+ clientContext.get_traceCorrelationId());
- }
Trace Correlation ID (Before Execution): null
Trace Correlation ID (After Execution): 7ad5fe9c-9163-f08f-4af5-87b3ede4b5de
The following example sets the co-relation id to the current context runtime application.
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- //Sets the value in the guid format to the correlation id
- clientContext.set_traceCorrelationId('a0d5fe9c-e1db-f08f-4af5-817583770b01');
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
- function onSucceeded() {
- //Returns the correlation id, which we set before execution
- console.log("Trace Correlation ID (After Execution): "+ clientContext.get_traceCorrelationId());
- }
Trace Correlation ID (After Execution): a0d5fe9c-e1db-f08f-4af5-817583770b01
SP.ClientContext.url
Returns the URL associated with the runtime context. The following example returns the server relative URL of the current web site.
- var clientContext = null;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- //Logs the current website server relative url
- console.log(“Url: “ + clientContext.get_url());
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
Url: /
SP.ClientContext.site
Returns the site collection object associated with the current client context. The following example returns the site collection URL and id based on the current client context.
- var clientContext = null;
- var oSite;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- oSite = clientContext.get_site();
- clientContext.load(oSite);
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
- function onSucceeded() {
- console.log("Site Collection URL: "+ oSite.get_url());
- console.log("Site Collection ID: "+ oSite.get_id());
- }
- function onFailed(sender, args) {
- console.log("Request Failed: "+ args.get_message());
- }
Site Collection URL: https://sharepointsite
Site Collection ID: f99d7e6e-081a-4c58-806f-ef3f979e5724
SP.ClientContext.web
Returns the web site object associated with the current client context. The following example returns the title and URL of the website based on the current client context.
- var clientContext = null;
- var oWeb;
- function contextInfo()
- {
- //Create an instance of the Current Context
- clientContext = SP.ClientContext.get_current();
- oWeb = clientContext.get_web();
- clientContext.load(oWeb);
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
- //Make sure the SharePoint script file 'sp.js' is loaded before the function 'contextInfo' runs
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
- function onSucceeded() {
- console.log("Web Site Title: "+ oWeb.get_title());
- console.log("Web Site URL: "+ oWeb.get_url());
- }
- function onFailed(sender, args) {
- console.log("Request Failed: "+ args.get_message());
- }
Web Site Title: SharePoint Demo Site
Web Site URL: https://sharepointsite
Summary
This article explores the properties of the current Client Context object and this is very helpful for developing an application using the JavaScript Side Object Model.

SriramPosted Jul 6, 2015, 1:26 AM
Exce;;ent............
Karthik Muthu KaruppanPosted Apr 23, 2015, 11:19 AM
good one
Rahul Kumar SaxenaPosted Apr 22, 2015, 2:30 PM
Good Show.
Krishnanand SivarajPosted Apr 22, 2015, 7:12 AM
good one shantha
Shantha Kumar TPosted Apr 22, 2015, 1:11 AM
Thanks Gowtham
Shantha Kumar TPosted Apr 22, 2015, 1:11 AM
Thanks Santhakumar
Gowtham RajamanickamPosted Apr 21, 2015, 11:26 PM
good one shantha
Santhakumar MunuswamyPosted Apr 21, 2015, 10:54 PM
Thanks for nice article