Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);
    listItem.update();
    clientContext.executeQueryAsync(Function.createDelegate(this,this.Succeeded),Function.createDelegate(this,this.Failed));

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 
listItem.update();

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.

share|improve this question

3 Answers 3

up vote 4 down vote accepted
function onQuerySucceeded() {
    alert('Item created: ' + listItem.get_id());
}
share|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:

http://PathToSite/_vti_bin/listdata.svc/ListName?$select=Id&$orderby=Id%20desc&$top=1

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

share|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.

       clientContext.executeQueryAsync(Function.createDelegate(this,this.FetchCurrentItemIdSucceeded),Function.createDelegate(this,this.Failed));
    }

    function FetchCurrentItemIdSucceeded(){
        //Write the code here which was originally in the 'Suceeded' function.
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.