1

Is there any way to add version history link (bellow code) on displayform.aspx as a content webpart?

<a href="#" onclick="SP.UI.ModalDialog.showModalDialog( {url: '/_layouts/Versions.aspx?list={list guid}&ID={item id}&IsDlg=1'} ); return false;">version history</a>
4

BUILT IN THINGS

Version history is already in ribbon of a displayform.aspx.

enter image description here

It is disabled by default. If you want to enable it, the go to list setting --> Versioning Settings and enable Create a version each time you edit an item in this list.

enter image description here

CUSTOMIZATION

Did not find any API for version history URL yet but you can construct it manually. There is a pattern in version history URL. Let me explain it now.

Decode any existing version history URL using decodeURIComponent().

decodeURIComponent('%7B233AE45E-F581-4420-B069-B7A74D368B13%7D&FileName=%2FLists%2FTender%20List%2F2_%2E000&Id=2&Source=...ender%2520List%2FAllItems%2Easpx')

It will result something like following.

{233AE45E-F581-4420-B069-B7A74D368B13}&FileName=/Lists/Tender List/2_.000&Id=2&Source=https://yourTenat.sharepoint.com/Lists/Tender%20List/AllItems.aspx

So what are the things in above URL, let me explain.

{233AE45E-F581-4420-B069-B7A74D368B13} is the GUID Id of your list. So declare a hard coded variable named listGuidId as this value will not change ever.

var listGuidId = '{233AE45E-F581-4420-B069-B7A74D368B13}';

FileName=/Lists/Tender List this is the FileName. You can construct it by following way.

    var fileName = _spPageContextInfo.serverRequestPath
                     .substr(0,_spPageContextInfo.serverRequestPath.lastIndexOf('/')+1);

To get this 2_.000&Id=2 portion, you need the Id of your item.

var itemId = GetUrlKeyValue('ID');

So the final URL should look like following

var versionHistory = _spPageContextInfo.siteAbsoluteUrl 
                        + '/_layouts/15/Versions.aspx?list=' 
                        + String.format('{0}&FileName={1}{2}_.000&Id={3}',
                        listGuidId,fileName,itemId,itemId);

Now your link should look like

<a href="#" onclick="SP.UI.ModalDialog.showModalDialog( {url: versionHistory + '&IsDlg=1'} ); return false;">version history</a>

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