Kwizcom Forms

ECMA Client Object Model (SP.js) in SharePoint 2010

This SharePoint tutorial, we will discuss ECMA Client Object Model (SP.js) in SharePoint 2010. SP.js file which is used while working with ECMA Client Object Model.

ECMA Client Object Model (SP.js) in SharePoint 2010

SP.js is the JavaScript file used for the ECMA Client Object Model. This file is necessary to work with SharePoint Client Object Model.

SharePoint deveopment training course

SharePoint 2010 provides a debug version of the same file known as sp.debug.js which can used for debugging but should not be used in production.

We can give reference like below:

<SharePoint:ScriptLink Name=”SP.js” runat=”server” OnDemand=”true” Localizable=”false” />

SP.js provides a very useful method, that helps in telling the custom script to wait until the SP.js file has been fully loaded.

That method name is ExecuteOrDelayUntilScriptLoaded. method takes a function to call as well as the script that it should wait for. Once the script has loaded, then the function will execute.

Example:

$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(getData, “SP.js”);
});

Here the getData method will execute after the SP.js file has been fully loaded.

Retrieve title and login name using ECMA Client Object Model in SharePoint 2010

In SharePoint 2013 also, there are different ways to access list data, as well as there are different API references needed to work with SharePoint 2013 client object model.

Follow the below code to retrieve the site title and login name of the current logged in user using ECMA Client Object Model:

$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(getWebData, “SP.js”);
});

//Here getWebData will wait until SP.js has been fully loaded.

var context = null;
var web = null;
var currentUser = null;

function getWebUserData() {
// Get the current user
context = new SP.ClientContext.get_current();

// Get the current web
web = context.get_web();

// Get the current user
currentUser = web.get_currentUser();

// Load the current user
context.load(currentUser);

// Execute the query
context.executeQueryAsync(success,failure);
}

function success() {
alert(‘User name:’ + currentUser.get_title() + ‘\n Login Name:’ + currentUser.get_loginName());
}

function failure(sender, args) {
alert(‘Error occured while retrieving data !’);
}

You may like following SharePoint 2010 tutorials:

Hope this SharePoint tutorial explains, ECMA Client Object Model (SP.js) in SharePoint 2010 and how to retrieve title and login name using ECMA Client Object Model in SharePoint 2010.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →