SharePoint Client Object Model (ECMAScript) #2 – Update Web Properties


SharePoint Client Object Model (ECMAScript) provides the capabilities of SharePoint Server Object capabilities at the client side. In this tutorial, let me show you how you can play around with the Client Object Model through IE Developer Toolbar how to update Web properties.

Check the screenshot below, to know what we are trying to achieve here:

Below is the function that i have used:

    function updateTitle() {
        var ctx = new SP.ClientContext.get_current();
        this.web = ctx.get_web();
        web.set_title(‘UpdatedTitle’);
        this.web.update();
        ctx.executeQueryAsync(Function.createDelegate(this, this.onUpdate),
            Function.createDelegate(this, this.onFail));
    }
    function onUpdate(sender, args) {
        alert(‘title updated’);
    }
    function onFail(sender, args) {
        alert(‘failed to update title. Error:’+args.get_message());
    }
 
ExecuteOrDelayUntilScriptLoaded(updateTitle, “sp.js”); 

The above ECMAScript access the SharePoint’s client context object and updates the Title property of it. It is essential to note that “SP.js” is loaded before this script execution occurs. This is the reason for using the “ExecuteOrDelayUntilScriptLoaded” function.

SharePoint Client Object Model (ECMAScript) #1 – Get Context Object


SharePoint Client Object Model (ECMAScript) provides the capabilities of SharePoint Server Object capabilities at the client side. In this tutorial, let me show you how you can play around with the Client Object Model through IE Developer Toolbar.

Check the screenshot below, to know what we are trying to achieve here:

Below is the function that i have used:

function getWebProperties() {
var ctx = new SP.ClientContext.get_current();
this.web = ctx.get_web();
ctx.load(this.web);
ctx.executeQueryAsync(Function.createDelegate(this, this.onSuccess),
Function.createDelegate(this, this.onFail));
}
function onSuccess(sender, args) {
alert(‘web title:’ + this.web.get_title() + ‘\n ID:’ + this.web.get_id() +
‘\n Created Date:’ + this.web.get_created());
}
function onFail(sender, args) {
alert(‘failed to get list. Error:’+args.get_message());
}

ExecuteOrDelayUntilScriptLoaded(getWebProperties, “sp.js”);

The above ECMAScript access the SharePoint’s client context object. It is essential to note that “SP.js” is loaded before this script execution occurs. This is the reason for using the “ExecuteOrDelayUntilScriptLoaded” function.