View Sidebar
JavaScript object model (JSOM) in apps for SharePoint

JavaScript object model (JSOM) in apps for SharePoint

December 5, 2013 16:36

Recently I am involved in managing and creating few tools to be used with SharePoint site. This give me an opportunity to explore SharePoint API’s. The most easy to pick for me was JavaScript Object Model (JSOM) as I already know JavaScript the transition was bit smoother if not clean than CSOM or .NET API’s. More about JSOM here.

Here I have listed a basic operation first to connect to the SharePoint site and then get the logged in user’s name & email id.

To use the JSOM, you must follow these steps:

1. Reference the required libraries.

2. Get a client context instance and load SharePoint objects.

3. Execute the query and provide callback functions.

The core JavaScript/jQuery  code goes as follows

$(document).ready(function () {
 $.support.cors = true;
 $.allowCrossDomainPages = true;
 $.getScript(scriptbase + "SP.Runtime.js", 
 function () {
 $.getScript(scriptbase + "SP.js", execOperation);
 }
 ); 
 });

 function execOperation() { 
 var context = new SP.ClientContext(siteUrl);
 var userObj = context.get_web().get_currentUser();
 context.load(userObj);
 context.executeQueryAsync(function (sender, args)
 {
 var email = userObj.get_email();
 $("#uid").append("Name: "+ email.substring(0,email.indexOf('@')));
 $("#uid").append("<br><br>Email: "+ email);
 },function (){}); 
 }

The source contains a single html file with all the functions, replace your server name & library files location and you are good to go.

Source Files

If you like the article please share

Leave a reply

You must be logged in to post a comment.