How do I manually set the primary key (ID) of a new list item?

Aug 18, 2014 at 3:00 PM
Looking around the web there appears to be, programmatically, ways to insert a new list item and force a specific item ID (that is, override SharePoint's default behavior of auto-incrementing the primary ID).

This doesn't seem to work for me via SPServices and I'm wondering if it should (or can) or if there is something I am missing.

I've tried:
    operation: "UpdateListItems",
    async: false,
    batchCmd: "New",
    listName: "MyList",
    valuepairs: [["Title", 'first test'], ["ID", 1234]],
    completefunc: function(xData, Status) {
As well as using a updates batch like:
<Batch OnError='Continue'>
  <Method ID='1' Cmd='New'>
    <Field Name='Title'>New Title Value</Field>
    <Field Name='ID'>1234</Field>
Both result in successful additions to the list but it seems to be ignoring the ID field.

Aug 18, 2014 at 3:29 PM
Simple answer: you can't.

If you need a specific ID of some sort on items, you should create your own column to store it.

Marked as answer by thornomad on 8/20/2014 at 6:25 AM
Aug 18, 2014 at 3:34 PM
Thanks for the quick reply. Though: I don't like the answer much. Smile.

If I am storing my own primary key in my own column can I pass an update command to that item based on my custom column? Or will I still need to get the actual item ID first before updating?

I am working with merging changes from a different set of data; so I know the primary key from the other set and I know that I need to update SharePoint ... I was just trying to save the step of first looking up the item based on my column, getting the ID, and then applying the changes.

Aug 18, 2014 at 3:41 PM
You'll have to do a GetListItems to find out what the ID is. SharePoint's key is always the ID.

I'm just the messenger!

Aug 18, 2014 at 3:45 PM
Thanks Mark - I appreciate it.

I'll start down that route ... going to experiment if it's faster to find each ID one item at a time (I have about 500 per batch to update) versus getting them all in a large OR query.
Aug 18, 2014 at 4:10 PM
If there's a filter you can apply to get the set you want to work with, then one request will be much more efficient than 500. It may even make more sense to just retrieve the entire list, depending on how many items you have.

Aug 18, 2014 at 6:01 PM
Unfortunately, I won't know which set of items I am working with until the user is uploading the changes file (CSV); there will be 10,000-15,000 items on the list at any given time to query against ... I think that might be too big to just keep grabbing the whole list. Not sure what the upwards limit is but it seems like a lot.

I experimented with pinging the server with each query to retrieve an ID (say for like 500 items) and the process was very slow ... seconds after passing seconds.

My second attempt was to write a nested <Or> CAML query and (after I figured out there was a nesting limit of about 150 items) am sending them in batch queries. So is about four queries total and responses come back almost immediately. I think I'll go with that route. Took a little more to get it working but only hits the server four times (instead of 500) and seems to work fine.

Thanks again for your advice. Always appreciated.