I am stuck at a place where I have to update a list which has a Lookup column. For example I am using this code to update my normal list

    var clientContext = SP.ClientContext.get_current();
    var webSite = clientContext.get_web();
    var lists = webSite.get_lists();
    var testList = lists.getByTitle("TestList");
    var itemCreationInfo = new SP.ListItemCreationInformation();
    var listItem = testList.addItem(itemCreationInfo);
    listItem.set_item("Title", "ABC");
    listItem.set_item("Name", "XYZ");
    listItem.set_item("Age", 21);

Now, I have to update another list with the ID of the just created item in the TestList, as a lookup column. On Succeeded function again I am trying to update the second List,

function Succeeded(sender,args) 
var carsList = lists.getByTitle("LookupList");
var itemCreationInfo = new SP.ListItemCreationInformation();
var listItem = carsList.addItem(itemCreationInfo);
listItem.set_item("Title", "Plan");
listItem.set_item("Place", "Delhi");
listItem.set_item("PlanId", ***???*** ); // Lookup Column 

But how can I get recently added item id to provide in PlanId column at Succeeded function. Please let me know what needs to be done for the same.

function onQuerySucceeded() {
    alert('Item created: ' + listItem.get_id());
| improve this answer | |
  • this! using a search by title or top 1 is not a viable solution for a transactional system. – brian brinley Jul 10 '12 at 18:33
  • I'm confused. Does listItem.get_id() use title or top 1? – Tom Resing Jul 10 '12 at 19:46

You can easily get the last item id via the REST API:


In plain English: get the list ids ($select=Id) in descending order ($orderby=Id desc), and only retrieve the top item ($top=1).

| improve this answer | |

There is no direct method in the JavaScript Object Model to achieve what you are doing. You will have to make another call to the 'TestList' to fetch the newly created item.

You can achieve this through the following way:

    function Suceeded(){
      //1) Query the 'TestList' to get items by Created Column.
      //2) Set Ascending='FALSE' so that the latest created item will be fetched first.
      //3) Set RowLimit of the Query to 1 so that only one item (the one you just created) will be fetched.


    function FetchCurrentItemIdSucceeded(){
        //Write the code here which was originally in the 'Suceeded' function.
| 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.