You can use the SharePoint client object model to retrieve, update and manage data in SharePoint 2013. SharePoint makes the object model available in several forms.

  • .NET Framework redistributable assemblies
  • JavaScript library
  • REST/OData endpoints
  • Windows Phone assemblies
  • Silverlight redistributable assemblies

This article shows how to perform basic operations using the JavaScript object model. You can add a reference to the object model using HTML <script> tags.

The following sections describe tasks that you can complete programmatically and they include JavaScript code examples that show the operations.

Procedure: Open your SP Site in SharePoint 2013 Designer. Then select an Assets icon from the designer ribbon to add a JavaScript file.



After adding a JavaScript file, the following file is available in the SharePoint Site Assets Folder:



Go to the SP site page and add a new page to the SharePoint site.



After adding a page select an Insert button in the Ribbon Menu.



Then add a Content Editor Web part into the page.



Edit the WebPart and add a JavaScript file link to the content link.



Save the web part and save the page in site. Click the button!



List Created Successfully.



Source Code

  1. <div><button onclick=" queryListItems ()">Click here to Create a List</button></div>
  2. <div id="displayDiv"></div>
  3. <script type="text/javascript">
  4. //This example gets all the items in the Announcements list that have a title that begins with 'T'.
  5. //If your site doesn't include a list called Announcements you must make the changes indicated
  6. //This variable will hold a reference to the Announcements list items collection
  7. var returnedItems = null;
  8. //This function loads the list and runs the query asynchronously
  9. function queryListItems() {
  10. //Get the current context
  11. var context = new SP.ClientContext();
  12. //Get the Announcements list. Alter this code to match the name of your list
  13. var list = context.get_web().get_lists().getByTitle('Announcements');
  14. //Create a new CAML query
  15. var caml = new SP.CamlQuery();
  16. //Create the CAML that will return only items with the titles that begin with 'T'
  17. caml.set_viewXml("<View><Query><Where><BeginsWith><FieldRef Name='Title' /><Value Type='Text'>T</Value></BeginsWith> </Where></Query></View>");
  18. //Specify the query and load the list oject
  19. returnedItems = list.getItems(caml);
  20. context.load(returnedItems);
  21. //Run the query asynchronously, passing the functions to call when a response arrives
  22. context.executeQueryAsync(onSucceededCallback, onFailedCallback);
  23. }
  24. //This function fires when the query completes successfully
  25. function onSucceededCallback(sender, args) {
  26. //Get an enumerator for the items in the list
  27. var enumerator = returnedItems.getEnumerator();
  28. //Formulate HTML from the list items
  29. var markup = 'Items in the Announcements list that start with "T": <br><br>';
  30. //Loop through all the items
  31. while (enumerator.moveNext()) {
  32. var listItem = enumerator.get_current();
  33. markup += 'Item Title: ' + listItem.get_item('Title') + '<br>';
  34. markup += 'Item ID: ' + listItem.get_id() + '<br><br>';
  35. }
  36. //Display the formulated HTML in the displayDiv element
  37. displayDiv.innerHTML = markup;
  38. }
  39. //This function fires when the query fails
  40. function onFailedCallback(sender, args) {
  41. //Formulate HTML to display details of the error
  42. var markup = '<p>The request failed: <br>';
  43. markup += 'Message: ' + args.get_message() + '<br>';
  44. //Display the details
  45. displayDiv.innerHTML = markup;
  46. }
  47. </script>

Thanks for reading my article.