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()
- {
-
- clientContext = SP.ClientContext.get_current();
- appname = clientContext.get_applicationName();
- console.log("Application Name: "+ appname );
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
OutputReturns 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()
- {
-
- clientContext = SP.ClientContext.get_current();
- clientContext.set_applicationName(“My Application”);
- appname = clientContext.get_applicationName();
- console.log("Application Name: "+ appname );
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
OutputApplication Name: My Application
SP.ClientContext.current
- Returns the current client context of the Client-Side Object Model (CSOM) runtime
- var clientContext = null;
-
- function contextInfo()
- {
-
- clientContext = SP.ClientContext.get_current();
- }
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
SP.ClientContext.formDigestHandlingEnabledGets 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()
- {
-
- clientContext = SP.ClientContext.get_current();
- console.log("Form Digest Handling Enabled: " + clientContext.get_formDigestHandlingEnabled());
-
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
The following example enables the Form Digest Handling to the current client context.
- var clientContext = null;
-
- function contextInfo()
- {
-
- clientContext = SP.ClientContext.get_current();
- clientContext.set_formDigestHandlingEnabled(true);
- console.log("Form Digest Handling Enabled: " + clientContext.get_formDigestHandlingEnabled());
-
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
Output
Form Digest Handling Enabled: true
SP.ClientContext.hasPendingRequestReturns 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()
- {
-
- clientContext = SP.ClientContext.get_current();
-
- oWeb = clientContext.get_web();
- clientContext.load(oWeb);
-
- console.log("Has Pending Request (Before Execution): "+ clientContext.get_hasPendingRequest());
- clientContext.executeQueryAsync(onSucceeded, onFailed);
-
- console.log("Has Pending Request (After Execution): "+ clientContext.get_hasPendingRequest());
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
Output
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()
- {
-
- clientContext = SP.ClientContext.get_current();
-
- console.log("Request Timeout Value: "+ clientContext.get_requestTimeout());
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
Output
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()
- {
-
- clientContext = SP.ClientContext.get_current();
- clientContext.set_requestTimeout(20000);
-
- console.log("Request Timeout Value: "+ clientContext.get_requestTimeout());
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
Output
Request Timeout Value: 20000
SP.ClientContext. get_serverLibraryVersionReturns the build version of Microsoft.SharePoint.Client.ServerRuntime.dll on the server.
- var clientContext = null;
- function contextInfo()
- {
-
- clientContext = SP.ClientContext.get_current();
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
-
- function onSucceeded() {
-
- console.log("Server Library Version: "+ clientContext.get_serverLibraryVersion());
- }
Output
Server Library Version: 15.0.4569.1501
SP.ClientContext.serverSchemaVersionReturns 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()
- {
-
- clientContext = SP.ClientContext.get_current();
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
-
- function onSucceeded() {
-
- console.log("Server Schema Version: "+ clientContext.get_serverSchemaVersion());
- }
Output
Server Schema Version: 15.0.0.0
SP.ClientContext.serverVersionReturns the current SharePoint version. The following example returns the SharePoint version of the current client context.
- var clientContext = null;
- function contextInfo()
- {
-
- clientContext = SP.ClientContext.get_current();
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
-
- function onSucceeded() {
-
- console.log("SharePoint Server Version:"+ clientContext.get_serverVersion());
- }
Output
SharePoint Server Version: 15.0.4569.1501
SP.ClientContext.traceCorrelationIdGets 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()
- {
-
- clientContext = SP.ClientContext.get_current();
-
- console.log("Trace Correlation ID (Before Execution): "+ clientContext.get_traceCorrelationId());
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
-
- function onSucceeded() {
-
- console.log("Trace Correlation ID (After Execution): "+ clientContext.get_traceCorrelationId());
- }
output
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()
- {
-
- clientContext = SP.ClientContext.get_current();
-
- clientContext.set_traceCorrelationId('a0d5fe9c-e1db-f08f-4af5-817583770b01');
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
-
- function onSucceeded() {
-
- console.log("Trace Correlation ID (After Execution): "+ clientContext.get_traceCorrelationId());
- }
Output
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()
- {
-
- clientContext = SP.ClientContext.get_current();
-
- console.log(“Url: “ + clientContext.get_url());
- }
-
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', contextInfo);
OutputUrl: /
SP.ClientContext.siteReturns 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()
- {
-
- clientContext = SP.ClientContext.get_current();
- oSite = clientContext.get_site();
- clientContext.load(oSite);
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
-
- 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());
- }
Output
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()
- {
-
- clientContext = SP.ClientContext.get_current();
- oWeb = clientContext.get_web();
- clientContext.load(oWeb);
- clientContext.executeQueryAsync(onSucceeded, onFailed);
- }
-
- 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());
- }
Output
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.