SP.ClientContext constructor (sp.js)

Initializes a new instance of the ClientContext object for the specified SharePoint site.

Applies to: apps for SharePoint | SharePoint Foundation 2013 | SharePoint Server 2013

var object = new SP.ClientContext(serverRelativeUrlOrFullUrl)

Parameters

  • serverRelativeUrlOrFullUrl
    The server-relative URL that starts with / or a full URL that starts with http:// or https://.

Return value

SP.ClientContext
A new instance of the client context for the specified SharePoint site.

Example

The following example gets the client context for the specified URL.

Note

Change the placeholder value for the url variable before you run the code.

var url = 'replace with server-relative or absolute URL';
var clientContext;
var website;

// Make sure the SharePoint script file 'sp.js' is loaded before your
// code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

// Create an instance of the client context for the specified URL.
function sharePointReady() {
    clientContext = new SP.ClientContext(url);
    website = clientContext.get_web();

    clientContext.load(website);
    clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}
function onRequestSucceeded() {
    alert('URL of the website: ' + website.get_url());
}
function onRequestFailed(sender, args) {
    alert('Error: ' + args.get_message());
}

See also

Other resources

SP.ClientContext object (sp.js)