0

Let's say I have 2 items in a list. If I add a new calculated column with following code then it correctly displays hyperlink against those two items.

="<a href='http://sharepoint2016/sites/somesite/Pages/Default.aspx?k="&ID&"'>Some Link</a>";

This is how the link is displayed (just showing you URL that you see on hover):

http://sharepoint2016/sites/somesite/Pages/Default.aspx?k=1   

http://sharepoint2016/sites/somesite/Pages/Default.aspx?k=2

Problem is if I add new item from now on, after calculated column is created then it doesn't display ID of new item. It shows up like this:

http://sharepoint2016/sites/somesite/Pages/Default.aspx?k=

Unless I edit that list column and simply press OK to save it then it starts displaying ID for the newly added item correctly.

How to fix this issue?

Frank Martin
  • 3,668
  • 18
  • 68
  • 121
  • 2
    You should not use ID in calculated columns: http://sharepoint.stackexchange.com/questions/159962/calculated-column-with-id-removes-id-number-on-update – Robert Lindgren Mar 04 '17 at 19:34

2 Answers2

3

Without a workflow

The ID is available in Table-Row HTML, so get that when you click the link:

="<a onclick=""{"   
&"var TR=this;while(TR.tagName!='TR'){TR=TR.parentNode}"    
&"var ID=TR.id.split(',')[1];"  
&"document.location='http://sharepoint2016/sites/somesite/Pages/Default.aspx?k='+ID;"   
&"}"">Some Link"    
&"</a>"

Shorter

This trick adds script to every item, so you want it as short as possible.

Since all SharePoint libraries have loaded by the time a user clicks,
you can use the SharePoint GetAncestor function:

="<a target=_blank "
&" onclick=""{"
&"  document.location='[your url]?k='+GetAncestor(this,'TR').id.split(',')[1];"
&"}"">Some Link"
&"</a>"
Danny '365CSI' Engelman
  • 21,176
  • 7
  • 35
  • 79
2

The ID doesn't exist when the calculated column is calculated because the ID isn't generated until a new item is first saved. The easiest solution is to use a workflow that runs when new items are created to simply immediately edit the item (just log the reason in a comments field, if nothing else).

Michael Bailey
  • 582
  • 2
  • 12