Monday, March 12, 2012

ECMAScript Object Model SharePoint 2010

You can also view this article to more about Client object model. Also you can check an introduction to SharePoint 2010 object model article here.

This is nothing but a different name to JavaScript object model.

We can use the ECMAScript object model in Application and Site pages, as well as web parts and dialog framework.

Microsoft provides SP.js file to work with, which is presented in the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS folder.

To work with ECMAScript Object Model we have to add SP.js file which will automatically add
SP.Core.js and SP.Runtime.js. There is also a debug version of the js file is present with the name as SP.debug.js.

These files are downloaded to the client browser when a user browsers to a SharePoint page.

To add .js file we have to use the below tag:

<SharePoint:ScriptLink ID="SPScriptLink" runat="server" LoadAfterUI="true" Localizable="false" Name="SP.js" />

LoadAfterUI="true" indicates to loads the script after the code of the UI.
Name="SP.js" indicates the relative path of the .js file to include in the page.

Here is a sample code to work with list.

<script language="javascript" type="text/javascript">

var clientContext;
var web;
var announcementList;

function retrieveContacts() {
this.clientContext = new SP.ClientContext.get_current();
this.web = this.clientContext.get_web();
this.announcementList = this.web.get_lists().getByTitle("My Custom Announcement List");
this.clientContext.load(this.announcementList);
}

</script>

Also you can check some SharePoint 2010 object Model articles here