In the previous post we discussed the basics of JavaScript Object Model (JSOM). In this post, we will implement JSOM inside a Content editor webpart. As per your requirement you can implement JSOM inside a Site page, Application page or a Master page directly.
Create a custom list for retrieving data from client side
Create a custom list named "Products" with three columns Title, Description and Price.
Code for retrieving all items from a SharePoint list
In the previous post we have already discussed about the objects and methods used in JSOM. Following is complete code for implementing JSOM for retrieving all items from a SharePoint list.
Copy the above code in a Content editor web part
Go to the SharePoint site and add a content editor web part.
Choose the Edit Source option and the HTML source window will pop up.
Copy the code mentioned in the previous step and paste it inside the HTML source window as follows. Click OK button and save the page.
Displaying the data
Click on the "Get Products" button and all the list items in the Products list will be displayed as an alert.
Create a custom list for retrieving data from client side
Create a custom list named "Products" with three columns Title, Description and Price.
Code for retrieving all items from a SharePoint list
In the previous post we have already discussed about the objects and methods used in JSOM. Following is complete code for implementing JSOM for retrieving all items from a SharePoint list.
Code Snippet
- <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">
- function GetProducts() {
- var clientContext = new SP.ClientContext.get_current();
- var oList = clientContext.get_web().get_lists().getByTitle('Products');
- this.collListItem = oList.getItems("");
- clientContext.load(collListItem);
- clientContext.executeQueryAsync(
- Function.createDelegate(this, this.onQuerySucceeded),
- Function.createDelegate(this, this.onQueryFailed)
- );
- }
- function onQuerySucceeded(sender, args) {
- var ProductInfo = '';
- var listItemEnum = collListItem.getEnumerator();
- while (listItemEnum.moveNext()) {
- var oListItem = listItemEnum.get_current();
- ProductInfo += '\n\nID: ' + oListItem.get_id() +
- '\nTitle: ' + oListItem.get_item('Title') +
- '\nDesc: ' + oListItem.get_item('Description') +
- '\nPrice: ' + oListItem.get_item('Price');
- }
- alert(ProductInfo.toString());
- }
- function onQueryFailed(sender, args) {
- alert('Request failed. ' + args.get_message() +
- '\n' + args.get_stackTrace());
- }
- </script>
- <input type="button" value="Get Products" onclick="GetProducts()"/>
Copy the above code in a Content editor web part
Go to the SharePoint site and add a content editor web part.
Choose the Edit Source option and the HTML source window will pop up.
Copy the code mentioned in the previous step and paste it inside the HTML source window as follows. Click OK button and save the page.
Displaying the data
Click on the "Get Products" button and all the list items in the Products list will be displayed as an alert.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.