3

I have a XsltListViewWebPart with a custom link column used for change a value on current listitem using JavaScript.

image link

<a href="javascript:ApproveRejectRDA('{@ID}', true);" title="approve">

below javascript function...

function ApproveRejectRDA(id, approved) {
    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('RDA');
    this.oListItem = oList.getItemById(id);

    if (approved == true)
       oListItem.set_item('NumStato', 16);
    else
       oListItem.set_item('NumStato', 17);

    oListItem.update();
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

When I click on link, everything works properly only when the row is selected. If not, I get the following error:

Uncaught TypeError: Cannot read property 'get_current' of undefined

It seems that clientContext has no value.

Any ideas?

Thanks

  • 3
    I do want to say this is a brilliant use of SharePoint! I love the ability to approve and reject straight from the listview! – Mike Sep 29 '14 at 15:36
  • Can you provide some more info about the SharePoint version and why you are using XSLT at all, instead of JSLink client-side JavaScript rendering templates. Thanks! – Sig Weber Sep 29 '14 at 20:04
  • I'm novice in SharePoint, and I don't know if this is a good solution. However, I'm working on SharePoint 2010. Any suggestion is wellcome! Thanks – Francesco D'Ercole Sep 30 '14 at 8:00
3

It seems it occurs since sp.js library that contains SP.ClientContext class is not yet loaded when your code runs.

Use SP.SOD.executeFunc to ensure that the specified sp.js library is loaded when the code runs.

Try to change ApproveRejectRDA function to enclose it in SP.SOD.executeFunc like this:

function ApproveRejectRDA(id, approved) {
   SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
      var context = SP.ClientContext.get_current();
      //Your remaining code goes here...

   });
}
0

You need to create a new object for clientContext. You have missed the word "new" while creating the context.

Just change the first statement as follows,

var clientContext = **new** SP.ClientContext.get_current();

Thanks.

  • Thanks, I already tried to create a new Object, but without success. It works only if the grid is focused (by clicking on any cell) – Francesco D'Ercole Sep 30 '14 at 8:07

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.