Sharepoint Forum

 
Home » Forum » Sharepoint       Ask a questionRSS Feeds

Get the id of the last inserted row in Sharepoint list

  Asked By: Derek Davis         Date: Aug 15, 2009      Category: Sharepoint      Views: 2116
 

How do I get the id of the last inserted row in Sharepoint list. Is there any
way to get it?

Tagged:                    

 

3 Answers Found

 
Answer #1       Answered By: Mason Davis          Answered On: Aug 15, 2009       

You could store it in a separate one column, one item list  that is
used for just that purpose, using a SPD workflow that fires on new
records being added in the list that you are worried about keeping
track of. Just replace what the stored ID is in your 2nd little list
with the new one generated when a new list item is created in your
target list. It is cheating like crazy, but keeps your solution free
of custom coding. Then you can do a lookup on that list item to get
the ID for whatever you want it for.

 
Answer #2       Answered By: Savannah Pena          Answered On: Aug 15, 2009       

There are several different methods to do this - basically you just need
to grab the highest valued ID column value. If you have more specifics
about what you are trying to do and what tools you have at your disposal
we can give a more detailed answer...

 
Answer #3       Answered By: Tom Jackson          Answered On: Oct 01, 2011       

Once you have the rows in an SPListItemCollection, you can do a count on the rows and get the ID from the last one, putting it into a variable, like this (I use a string variable, for simplicity with casting):


string myCAMLquery = @"
<View> <Query> <OrderBy> <FieldRef Name='Created' Ascending='False' /> </OrderBy> </Query> </View> ";
SPListItemCollection myListColl = myList.GetItems(myCAMLquery);
int count = myListColl.Count-1; // because it starts at 0
string myID = myListColl[count]["ID"].ToString();

should work.

 
Didn't find what you were looking for? Find more on Get the id of the last inserted row in Sharepoint list Or get search suggestion and latest updates.


Your Answer
  • Answer should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].