Follow us on Twitter
Follow us in Facebook
Office Dev Content
SharePoint Dev Content
Blogs for Office developers > Apps for Office and SharePoint blog
Hi! My name is Ricardo Loo. I am a Programming Writer for SharePoint. In today's article we'll talk about how to use the JavaScript object model (JSOM) in a simple HTML page in your app for SharePoint. By using the JSOM, you can access data and functionality in SharePoint 2013. You can also use Representational State Transfer (REST) endpoints to access data. The choice depends on various factors such as your abilities or development platform. For more information, see Choose the right API set in SharePoint 2013.
To keep things simple and short, I am using an app page hosted in SharePoint—but remember that you can also use the JSOM if your pages are not hosted in SharePoint. To access the objects using the JSOM in pages not hosted in SharePoint, you can use the cross-domain library (we'll post more information about the cross-domain library in a subsequent article.)
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.
Before using the JSOM you must reference the following libraries, in this order:
1. ASP.NET Ajax library
2. sp.runtime.js file
3. sp.js file
When the app pages are hosted in SharePoint, you can use relative paths to the required files. The following script tags show you how to reference the required libraries in a page hosted in SharePoint:
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script><script type="text/javascript" src="_layouts/15/sp.runtime.js"></script><script type="text/javascript" src="_layouts/15/sp.js"></script><script type="text/javascript"> // Continue your program flow here.</script>
To access the objects in the JSOM, you first need an instance of the client context. You can get an instance of SP.ClientContext by using the get_current() method. Now, we have an access point (the client context) to the objects in SharePoint. As you probably imagine, you must issue a query to get the specific objects you need.
What's next? Let's create a list. You can use lists in the app web to provide storage for your app. For example, you can use a list to store user data or some other information. Whether you use lists or another type of storage depends on how you design your app. For more information, see Data storage options in apps for SharePoint.
The following code shows how to get an instance of the client context and load the required objects to create a list:
var clientContext; var listCreationInfo; var web; var list; clientContext = SP.ClientContext.get_current(); web = clientContext.get_web(); listCreationInfo = new SP.ListCreationInformation(); listCreationInfo.set_title("User data"); listCreationInfo.set_templateType(SP.ListTemplateType.genericList); list = web.get_lists().add(listCreationInfo); clientContext.load(list);
Now, you are ready to send the query to SharePoint and get your list created. You can send your query by using the executeQueryAsync method in the client context object. You should provide callback functions to handle the success and failure events. The following code shows how to use the executeQueryAsync method and provide callback functions for the success and failure events:
clientContext.executeQueryAsync( function () { alert("Success!") }, function () { alert("Request failed") });
Great! So… where is your list?
To see your list, just go to the following URL:
app_web/Lists/User%20data
Figure 1. List created using the JSOM
Easy! Well, I think that is enough for one post. I hope you found it useful to get up and started using the JSOM in your apps for SharePoint.
Let us know what other topics you would like us to talk about by using the comments section below. Thanks for your time!