I am editing a item display template in SharePoint 2013. The following problem occurs:

ctx.CurrentItem becomes undefined in a custom function.

How can I solve this?

Here is a little code snippet:

 var p = getFollowers();
 p.done(function(result) {
  • well it is a standard display template and then you use . var uname = ctx.CurrentItem.PreferredName; but on the begin of the display template there is a check if currentitem is not null. and currentitem becomes empty when entering the p.done function – Julien Kulker Jan 24 '16 at 19:12
  • Can you put below line and then check ctx. var ctx= new SP.ClientContext(siteUrl); Thanks Bhism – BHISM NARAYAN Jan 25 '16 at 6:03
  • i can try this but i don't think it will work. The problem isn't the ctx but the currenitem is empty – Julien Kulker Jan 25 '16 at 7:18
  • Every example uses ctx as the variable inside a function, but ctx is a global object and JavaScript passes Objects by reference. So when you hit your done function ctx is something completely different in the global scope. The way to fix this depends on your code, this snippet alone is not enough. – Danny '365CSI' Engelman Jan 25 '16 at 9:17
  • I am not sure if I understand well your problem, but I think that you can try this: AddPostRenderCallback(ctx, function() { //your code here }); – Tomasz Szulczewski Jan 25 '16 at 12:26

We fought this for more than 5 hours and it turns out all we had to do was reboot all the sharepoint servers and the ctx.currentitem started returning all of the managed property mappings.

| improve this answer | |

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.