Update List Item Value using UpdateListItems

Dec 27, 2011 at 7:40 AM

Hi All,

I have one problem here. I'm have a Sharepoint list that have status Pending, Approve and Rejected. I have create a reject button for each sharepoint list items and when I click the button, if the list item status equal to Pending, it will change to Rejected.

But, I unable to update the items unless I hardcoded the items ID. Do you have any ideas on how to get current items ID and update the value.

Here the code that I have try :

 

function UpdateStatus() {
    $().SPServices({
        operation: 'GetListItems',
        listName: "Campaign Request",
        CAMLViewFields: '<ViewFields><FieldRef Name="ID"/></ViewFields>',
        completefunc: function (xData, Status) {
            $(xData.responseXML).find("[nodeName=z:row]").each(function () {
                var ID2 = $(this).attr("ows_ID");
                $().SPServices
                ({
                    operation: "UpdateListItems",    
                    listName: "Campaign Request", 
                    ID:ID2,   
                        valuepairs: [["Status", "Rejected"]],    
                        completefunc: function (xData, Status){

                        }
      });
      });
    }
  });
}

function UpdateStatus(){

$().SPServices({

operation: "UpdateListItems",    
listName: "Campaign Request", 
ID:152,   
valuepairs: [["Status", "Rejected"]],    
completefunc: function (xData, Status)
});


}</script>


Coordinator
Dec 27, 2011 at 12:13 PM
It all depends where you are. If you're on a list form page, then the ID is on the Query String. If you're on some other page, then you'll either need to find the ID in the page content or add it into the page content (e.g., in a view) so that you can access it.

M.
Dec 27, 2011 at 1:57 PM

Hi

it is in a list where I created a view and added the link button.

I have added the column name "ID".

And I have tried ID:ID and ID:RowID

both did not work.

Any idea? thanks.

Dec 27, 2011 at 2:15 PM
Can you post the HTML that is displayed on the page for one of the rows? Including the 'delete' button that you included on each row?
If I can see the scope of your button with the list, I might be able to help.

Also,
What version of jQuery and SPServices are you using?

_____
Paul

Coordinator
Dec 27, 2011 at 3:07 PM

You'll need to select the ID in the DOM using jQuery selectors. As Paul mentioned, if you can post the HTML for one of the rows, we may be able to help.

M.

Dec 27, 2011 at 3:23 PM

hi many thanks for reply.

I am using jquery.SPServices-0.6.0.js, jquery.SPServices-0.6.0.min.js,jquery-1.4.2.min.js

I am creating the view by using SPD 2010

the button is sth like this

<asp:LinkButton runat="server" id="LinkButton1{$ID}{generate-id()}" onclientclick="UpdateStatus()">Reject</asp:LinkButton>

for HTML for one of the row..i am not sure where to start copy from..hmm..any guide on this?

it seem like quite messy if i just copy all, that is in WebPartPages:XsltListViewWebPart. all using ootb features.

Thanks :)

Coordinator
Dec 27, 2011 at 3:27 PM

A few things:

  • You only should use one version of the SPServices library. It looks like you are using both the minified and unminified version.
  • SPServices v0.6.0 is old, and you'd be better off with v0.7.0.
  • jQuery 1.4.2 is a very old version. You'll get better results with jQuery 1.7.
  • What we need to see isn't what you have on the page server side, but the HTML in the DOM on the client side. If you do a View Source and fine one of the rows, that's a good starting point.

M.

 

Dec 27, 2011 at 3:58 PM

@starincloud

Ok.. so you are creating the button on SPD... you already have access to the ID for each row... just use it as input to your button's call to UpdateStatus()... like this:

 

<asp:LinkButton runat="server" id="LinkButton1{$ID}{generate-id()}" onclientclick="UpdateStatus({$ID})">Reject</asp:LinkButton>

 

Then, change your UpdateStatus() function to accept the input parameter... Like this:

 

function UpdateStatus(itemId){
    if (!itemId) {
        alert("UpdateStatus(ERROR): No itemId defined as input param!");
    }

    $().SPServices({
        operation: "UpdateListItems",
        listName: "Campaign Request",
        ID: itemId,   
        valuepairs: [["Status", "Rejected"]],    
        completefunc: function (xData, Status){
        
            alert("Update Done");
        
        }
    });
}

 

In addition to the issues that Marc already pointed, out, I noticed the following in your initial post:

  1. You have two functions of name UpdateStatus in your markup... is this a mistake? are you trying to show us something else?
  2. When using jQuery selectors that match on an attribute value, always quote the value (ex. .find("[nodeName='z:row']")... single quote around z:row)

 

Also,

If this does not work, it would still be good to see the actual HTML that the browser displays.... For one row only, including the table row markup (<tr>...</tr>).

 

Paul.

 

 

 

 

Coordinator
Dec 27, 2011 at 4:02 PM

Good work, Paul (as usual).

One other note: the syntax

.find("[nodeName='z:row']")
no longer works with jQuery 1.7+. See the $().SPFilterNode function.
M.
Dec 27, 2011 at 4:41 PM

it works!

Thanks for helping!

Anyway, does it gonna be big effect on my current jquery if i update it?

cos i am running out of time..

am considering should i update it or not..

Coordinator
Dec 27, 2011 at 4:44 PM

If you've got something running with the versions you have, you're fine. As you move forward, you may need to retool to keep things running.

M.