I have added a button to trigger the following JS code to display the versioning history for a list item.
<script type="text/javascript"> var listItem; var list; var title; var clientContext; var item1; var value; var source; var weburl; var currentListGUID; function getSetListItem() { source=document.URL; clientContext = SP.ClientContext.get_current(); if (clientContext != undefined && clientContext != null) { weburl=_spPageContextInfo.webServerRelativeUrl; currentListGUID = SP.ListOperation.Selection.getSelectedList(); if(currentListGUID === "undefined" || currentListGUID === null) { alert('Please select one row'); } else{ var webSite = clientContext.get_web(); var ctx = GetCurrentCtx(); title =ctx.ListTitle; list = webSite.get_lists().getByTitle(title); var selectedItems = SP.ListOperation.Selection.getSelectedItems(clientContext); if(Object.keys(selectedItems).length>1) {alert('Please select only one row');} else{ for (var item in selectedItems) { item1=selectedItems[item].id; } listItem = list.getItemById(item1); clientContext.load(this.listItem); clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed)); ShowDialog(); } } } } function OnLoadSuccess(sender, args) { value = this.listItem.get_item("FileLeafRef"); } function ShowDialog() { var options = { autoSize: true, allowMaximize: true, title: 'Version History', showClose: true, url:weburl+'/_layouts/15/Versions.aspx?list=' + currentListGUID + '&ID='+item1+'&Source='+weburl+'/Lists/'+title+'/AllItems.aspx', }; SP.UI.ModalDialog.showModalDialog(options); } </script>
The code is working well on a SharePoint list page, but what I did is to add the list view webpart to a web part page and then added the button/code. It is strange that the code is working fine when I saved even checked in the page, but it is not working after I published the page. The issue is that, even when I selected an item, when i pushed the button I got the message saying that “Please select one row” as if I didn’t select any rows. the following is the message shown in the console.
Please kindly give me a clue.