This SharePoint JSOM tutorial we will discuss how to retrieve SharePoint list items and display in the hyperlink using JSOM (JavaScript Object Model).
Retrieve SharePoint list items and display in hyperlink using jsom
Here I have a SharePoint Online list which has below columns and also I have added a few items in the SharePoint list:
- Title
- Modified
Create HTML Input Form
Here, I have created an HTML file which code looks like below. Here also, I have also added .js, .css and jQuery files.
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://onlysharepoint2013.sharepoint.com/sites/Raju/SiteAssets/Preetihtml/NoticeJS.js"></script>
<link rel="stylesheet" href="https://onlysharepoint2013.sharepoint.com/sites/Raju/SiteAssets/Preetihtml/AnnounceStyle.css">
<meta charset="utf-8" />
<title>ANNOUNCEMENT LIST</title>
</head>
<body>
<p id="Ptitle"></p>
</body>
</html>
Create .JS File
Here, I have created a JavaScript (.js) file which contains the JSOM code to retrieve SharePoint list items. The .js code looks like below:
$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
});
var collListItem;
function retrieveListItems() {
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('CompanyAnnouncement');
var camlQuery = new SP.CamlQuery();
collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
var listItemEnumerator = collListItem.getEnumerator();
var announcements ='';
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var url = "https://onlysharepoint2013.sharepoint.com/sites/Raju/Lists/CompanyAnnouncement/DispForm.aspx?ID=" + oListItem.get_item('ID');
announcements += "<a href='" + url + "'>" + oListItem.get_item('Title') + "</a>" + "<br />";
}
$("#Ptitle").html(announcements);
}
function onQueryFailed(sender, args) {
alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
}
Here I am using the ExecuteOrDelayUntilScriptLoaded method which will call our retrieveListItems method after SP.js file loaded successfully.
Here, in the code I have created the URL like below:
announcements += "<a href='" + url + "'>" + oListItem.get_item('Title') + "</a>" + "<br />";
At last I have binded all the Item of “AnnouncementList” in the paragraph<P> tag using its specific ID attribute as below-
$("#Ptitle").html(announcements);
Then you can add all the HTML, JS and CSS files to the Site Assets library in the SharePoint site.
Then create a web part page and Edit the page and then click on Add a web part link. From there we can create a content editor web part to the web part page.
Then edit the web part and provide the HTML file path.
Read some SharePoint jsom tutorials:
- Retrieve all users from all groups using jsom in SharePoint
- Retrieve SharePoint list items using javascript object model
- Update SharePoint List Item using javascript object model
- Get SharePoint List Item by ID using javascript object model
- How to display SharePoint List Items in Div using JSOM (javascript object model)
- How to retrieve Query String Value using JQuery in SharePoint Online?
- Bind SharePoint list items to dropdownlist using javascript object model (jsom), Rest API and Server Object Model
- Display SharePoint list data in html table using jquery and javascript object model
- Insert item to SharePoint Online list using JavaScript Object Model (jsom)
- Uncaught TypeError: Cannot read property ‘get_current’ of undefined error in jsom SharePoint Online
- How to create a list from existing list in SharePoint Online
Conclusion
In this SharePoint tutorial, we discussed how to retrieve SharePoint list items and display in a hyperlink using the JavaScript object model (JSOM) in SharePoint Online or SharePoint 2013/2016.
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 EnjoySharePoint.com