JavaScript Client Object Model JSOM in SharePoint 2013

InfoPath alternatives for form designing SharePoint

Here we will discuss how to use javascript client object model in sharepoint 2013. In some other posts we will see some jsom sharepoint 2013 examples. This also known as ECMAScript Client Object Model (jsom) or JavaScript Client Object Model (JSOM) in SharePoint 2013. JSOM is used for accessing and manipulating SharePoint objects by using JavaScript (JavaScript) in an asynchronous manner.

If you are working with SharePoint hosted add-in, then we have to use Jsom in SharePoint hosted Add-in. We can use jsom code inside a script editor web pat, content editor web part, inside an html page or we can use in SharePoint Add-in.

As most of cases you will not get access to deploy your server side code, this will help you much in that scenarios. From SharePoint 2010 towards next versions there is lot of improvements towards the client side object model. Lots of classes available to work with client side object model.

We can access and manipulate SharePoint objects like Site, Web, List, ContentTypes, User Permission using ECMAScript client object model which works in asynchronous mode. To work with JSOM we need to refer to certain javascript files and if you web part page or the page is using the default master pages then we do not need to give any reference because most of the cases the reference is already presented inside the master pages. And we should also make sure that there should be one and only reference exists for a particular file.

We can write the JSOM code inside a script editor or also inside a content editor web part inside a SharePoint Site. But Cross-ste scripting is also not allowed in JSOM like you can not call SharePoint objects from one sites to other site.

Below are the browsers supported like:
– IE 7.0 onwards
– Firefox 3.5 onwards
– Safari 4.0 onwards.
– Google Chrome

When client maked an Http request to the server, the server tries to load the page and the master page. If the page using the default master pages then the page already has references to the required JSOM scripts. If it is a custom master page, then we have to load the JSOM references before the pages and controls can use the JSOM. The SharePoint downloads the files from the C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS directory to the client browser.

The js files which we required are:
SP.js
SP.Core.js
SP.Runtime.js

Unlike SharePoint 2007, SharePoint 2010 or 2013 versions SharePoint only download the required files instead of downloading the whole file. SharePoint provides Script On Demand (SOD) framework which ensures that only the required files are downloaded to the client and provides options to load them in the background after the page has completed loading. SOD is a class defined in the init.js file and contains methods for loading scripts, registering script dependencies, executing methods within a loaded page, and event notification.

You can enable on-demand loading is to set the OnDemand=”true” attribute in the SharePoint:ScriptLink server tag.

<script type=”text/javascript”>
SP.SOD.RegisterSod(“SP.js”, “\_layouts\SP.js”);
</script>

If you want to give reference in a page which is using any custom master page, then you can write like below:

<SharePoint:ScriptLink ID=”ScriptLink21″ Name=”sp.js” runat=”server” OnDemand=”false” LoadAfterUI=”true” Localizable=”false” />

Example:
Now to work with the SharePoint objects from JSOM, first we need an instance of the client context. You can get an instance of SP.ClientContext by using the get_current() method. Like

var clientContext = SP.ClientContext.get_current();

Below is the code to create a List in the site in SharePoint 2013 or SharePoint online:

<input type=’button’ id=’id1′ value=’Create List’ onclick=”CreateList();”/>

<script type=”text/javascript”>
function CreateList()
{
var clientContext;
var listCreationInfo;
var web;
var list;

clientContext = SP.ClientContext.get_current();
web = clientContext.get_web();
listCreationInfo = new SP.ListCreationInformation();
listCreationInfo.set_title(“MyTestList”);
listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
list = web.get_lists().add(listCreationInfo);

clientContext.load(list);

clientContext.executeQueryAsync(
function () { alert(“Site Created Successfully!!!”) },
function () { alert(“Request failed”) }
);
}
</script>

Go to the site content and you will be able to see the list created.

Example-1:
Here we will try to create one list item from JSOM SharePoint 2013 or SharePoint online.

<input type=’button’ id=’id1′ value=’Create List Item’ onclick=”CreateListItem();”/>

<script type=”text/javascript”>

var siteUrl = ‘http://win-pfcp2dgt8di/sites/EnjoySharePoint/’;

function CreateListItem() {

var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle(‘MyTestList’);

var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);

oListItem.set_item(‘Title’, ‘My test item’);

oListItem.update();

clientContext.load(oListItem);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

alert(‘Item created: ‘ + oListItem.get_id());
}

function onQueryFailed(sender, args) {

alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}
</script>

Also you can check some Jsom SharePoint 2013 articles:
– Retrieve site workflow id using JavaScript Object model in SharePoint Online

– Delete all items from SharePoint online list using JavaScript object model

– Remove items from Site Actions menu using JavaScript object model in SharePoint Online Office 365

Hope this will be helpful.

Similar SharePoint 2013 Tutorials

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 →

Leave a Reply