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?

1

2 Answers 2

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>"
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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