1

i'm looking for a way to make the showing SharePoint 2013 list item version history more intuitive, and eventually i have found the JS code below; however, when i added the code to a SP 2013 list, it is not working. Please kindly advise how to implement the code.

<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> 

<input id="btnGetSetListItem" onclick="getSetListItem()" type="button" value="Version History" />
  • Did you add this script to a content editor web part or to a script editor web part? If you look at your browser's console, there must be some JS errors showing up? – moe Jan 8 '18 at 6:12
1

I think the problem is with URL parameter in ShowDialog() it should be url:weburl+'/_layouts/15/Versions.aspx.... you have missed the slash before _layouts/

the weburl is a server relative URL ending without '/' you have to add it while making up dialog URL

  • Hi Akshay, I will appreciate it very much if you can help me on one more issue. I inserted the list viewer webpart to a publishing page, and then added the JS code. The code is working fine when I saved or even checked-in the page, but it is no longer working when I published the page. The issue is that, even when I selected an item, but got the message saying "Please select one row" when I pushed the button, as if I didn't select any items. – T Zhang Jan 19 '18 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.