1

I'm currently trying to get the id of my currently iserted item. I'm making a timesheet which user is able to save and retrieve his own timesheet. The thing is that i put the ID of an item in the html code so i know if the item is coming from my list or if it was currently inserted by the user . So i want to know the currently inserted item and put it in my html code so i know that it has been inserted in the list and i have to make an update of that item and not insert it a second time.

Here is how i'm saving/updating current user timesheet

if ($(this).find("input").attr("data-ID")) {
                            updateTimesheet($(this).find("input").attr("data-ID"), jobCode, prestation, parseFloat($(this).find("input").val()), dateMonth);
                        }
                        else {
                            entry = webList.addItem(entryCreationInformation);
                            entry.set_item("JobCode", jobCode);
                            entry.set_item("ClientName", prestation);
                            entry.set_item("DateMonth", dateMonth);
                            entry.set_item("Heure",heure);
                            entry.update();
                            context.load(entry);
                            context.executeQueryAsync(success, fail);
                        }

function success() {
    //do nothing
}

function fail(sender, args) {
    alert("Call failed. Error: " +
        args.get_message());
}
function updateTimesheet(id,jobCode,clientName,heure,date) {

    var clientContext = context;
    var oList = webList;

    this.oListItem = oList.getItemById(parseInt(id));

    oListItem.set_item("JobCode", jobCode);
    oListItem.set_item("ClientName", clientName);
    oListItem.set_item("DateMonth", date);
    oListItem.set_item("Heure", heure);

    oListItem.update();

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

function onQuerySucceeded() {

    //alert('Item updated!');
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

I've tried like getting the entry.ID on query success but it doesn't work. I've also tried to get the id just after the context.exectureQueryAsync in case the entry was updated but doesn't work either.

I tried this solution here: http://social.technet.microsoft.com/Forums/sharepoint/en-US/d9c68caa-ddfd-4a68-a32f-d6154546f42e/getting-id-of-newly-inserted-item-with-silverlight?forum=sharepointdevelopmentprevious but won't work too.

I'm learning Sharepoint on the job so maybe it isn't best solution to do this.

Thank you in advance.

3
  • Why it deleted my "Hello there!" At the begining? Can't we say hello here? x)
    – Timon
    Apr 16, 2014 at 14:47
  • "Hello" and other greetings are automatically stripped out by the network. We want the posts to be concice and hence all unnecessary information should be left out of your posts :) Apr 22, 2014 at 7:17
  • I've tried all solutions from here and nothing worked. Can't we do this with javascript?
    – Timon
    Apr 23, 2014 at 7:13

3 Answers 3

3

Quick fix

Reference entry object by this keyword:

this.entry = webList.addItem(entryCreationInformation);

to make it accessible in handler, then you could grab list item id:

function success() {
    console.log(entry.get_id());
}

How to create List Item via CSOM

function addListItem(listTitle,itemProps,OnItemAdded,OnItemError)
{
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();

    var list = web.get_lists().getByTitle(listTitle);
    var itemCreateInfo = new SP.ListItemCreationInformation();
    var listItem = list.addItem(itemCreateInfo);

    for(var propName in itemProps) {
       listItem.set_item(propName, itemProps[propName]) 
    }

    listItem.update();
    context.load(listItem);
    context.executeQueryAsync(
        function() {
          OnItemAdded(listItem);           
        },
        OnItemError
    );
}

Then you could replace your code for creating list item with this one:

var itemProps = {'JobCode': jobCode, 'ClientName': prestation, 'DateMonth': dateMonth, 'Heure':heure};
addListItem('YourListTitle',itemProps,
function(item){
            console.log(item.get_id());
         },
         function(sender,args){
            console.log('Error occurred while creating an item:' + args.get_message());
         }); 
10
  • I've tried this and it doesn't work
    – Timon
    Apr 18, 2014 at 7:00
  • Tested and it works for me Apr 23, 2014 at 15:32
  • I got "entry is undefined" when i do this, there's no typo. I've made entry a global variables. This is how it looks like after changing to what you suggested:
    – Timon
    Apr 24, 2014 at 10:00
  • this.entry = webList.addItem(entryCreationInformation); this.entry.set_item("JobCode", jobCode); this.entry.set_item("ClientName", prestation); this.entry.set_item("DateMonth", dateMonth); this.entry.set_item("Heure", parseFloat($(this).find("input").val())); this.entry.update(); context.load(this.entry); context.executeQueryAsync(success, fail);
    – Timon
    Apr 24, 2014 at 10:00
  • And success methode function success() { console.log(entry.get_id()); }
    – Timon
    Apr 24, 2014 at 10:01
0

What i understand from your question is you want to display recently added item in the same page. One simple way of doing this is:-

  • Create a view, sort the view by Created and have Item Limit in the view to 5( or however number of recent records you want to see.

View Sort Descending by Created

Limit Items returned from View

  • Add a List View web part on your page, make sure you select the view you have created.
  • As soon as you submit item, You can refresh the page and newest record will show up.
0

You need to give entry the proper scope to be valid inside the success handler. The quick and dirty way to do this would be to make it a global variable. Then you can access it in the handler:

function success() {
    var id = entry.get_item("ID");
}
1
  • It doesn't work either
    – Timon
    Apr 18, 2014 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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