Retrieving ID of item just created with UpdateListItems / possible issue with SPGetLastItemId?

Sep 13, 2010 at 5:43 PM

So... here's my problem... I'm writing some "clone" functionality to clone a currently existing list A entry and all the children of that entry in another list B.

I created the cloned entry in list "A" just fine, however I need to know the ID of that row I just created in list "A" so that I can set it properly in List B.

How do I get the ID of the row I JUST created.  I tried using "SPGetLastItemId", but it is always returning "0" although every entry in that list was created by me.

I AM however logged in as a "System Account" if that makes a difference?  

Any ideas??

Here's my code:

 

//Get item from list A whose ID == 1

$().SPServices({
   operation: "GetListItems",
   listName: "ListA",
   CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
   CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Integer'>1</Value></Eq></Where></Query>",
   completefunc: function (xData, Status) {
    $(xData.responseXML).find("[nodeName=z:row]").each(function() {
     var clonedListAID = $(this).attr("ows_ID");
     var clonedListATitle = $(this).attr("ows_Title");
//insert cloned entry
     $().SPServices({
      operation: "UpdateListItems",
	  async: false,
      listName: "ListA",
      updates: "<Batch OnError='Continue' PreCalc='TRUE'>" +
                "<Method ID='1' Cmd='New'>" +
                    "<Field Name='Title'>" + clonedListATitle + " - clone " +  "</Field>" +
                "</Method>" +
            "</Batch>",
     completefunc: function (xData, Status) {
		var lastId = $().SPServices.SPGetLastItemId({ 
			listName: "ListA"
		}); 
		// should be ID for entry I just created? but it's always "0"
		alert(lastId);
     }
     });
    });
   }
  });
 }

 

 

Coordinator
Sep 13, 2010 at 6:17 PM

Mark:

This looks like it ought to work. The fact that you're using a system account shouldn't matter. Would you mind debugging SPGetLastItemId in your environment a little to try to see what's going on?

The only other thing I can think of is that since the commit of the item is asynchronous, it's not complete when you call SPGetLastItemId. What happens if you call SPGetLastItemId afterwards, separately? Are there multiple items in the list created by this account? If so, it ought to return *something* other than 0.

M.

Sep 13, 2010 at 6:33 PM

That's the weird thing Marc, if ALL I call is execute the code below I get "0" even though the list has multiple items:

var lastId = $().SPServices.SPGetLastItemId({
     listName: "ListA"
}); 

// should be ID for entry I just created? but it's always "0"
alert(lastId);

I'll debug through SPGetLastItemId and see what I can find.

thanks,
Mark

Sep 13, 2010 at 6:41 PM

userId is "undefined" in SPGetLastItemId.  Looks like it could be "System Account" related... I'll dig some more.

Sep 13, 2010 at 6:51 PM

userId is "undefined" no matter what user I use...  thoughts?

Coordinator
Sep 13, 2010 at 7:16 PM

Hmm. I know this works in my environments. WSS? MOSS? What comes back from GetUserInfo?

M.

Sep 13, 2010 at 7:30 PM

MOSS Enterprise...  Will let you know what I find... 

Also.. it looks like the issue is that the response XML from the UpdateListItems does not have an rs:data entry or a rowcount.  I can get the ID for the item I just created using the following code instead of trying to loop through each row:

  $().SPServices({
      operation: "UpdateListItems",
	  async: false,
      listName: "ListA",
      updates: "<Batch OnError='Continue' PreCalc='TRUE'>" +
                "<Method ID='1' Cmd='New'>" +
                    "<Field Name='Title'>" + clondedListATitle + " - clone " +  "</Field>" +
                "</Method>" +
            "</Batch>",
     completefunc: function (xData, Status) {
	   var newItemID = $(xData.responseXML).find("[nodeName=z:row]").attr("ows_ID");
     }
     });

 

Coordinator
Sep 13, 2010 at 7:33 PM

Ah, OK. That's a better trick. I couldn't remember if the Web Service returned the just-created ID or not. Makes very good sense that it would.

M.