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 working on a solution where I have a list with a column named LogIDNumber. For each new entry in the list, I would like to increment the LogIDNumber by one. I could use the ID generated by SharePoint, but for this solution it needs to be independent. I was thinking of using an event receiver, but the problem I am having trouble with is getting the value from the previous entry.

I can use something like this example to gain access to the list items, but I'm not sure how to proceed after that.

using (SPSite siteCol = new SPSite("http://server/sites/Contoso"))
{
    using (SPWeb web = siteCol.RootWeb)
    {
        SPList list = web.GetList("/sites/Contoso/Lists/Books");
        SPListItemCollection items = list.GetItems("Title", "LogIDNumber",);
    }
}

Is there a way to grab the last value?

share|improve this question
add comment

6 Answers 6

up vote 1 down vote accepted

You could use a CAML query and sort by the particular field, grab the highest value and add one. Should probably have a distinct check as well.

The query can be something like this :

"<OrderBy><FieldRef Name='ColumnName' Ascending='False' /></OrderBy><RowLimit>1</RowLimit>"

For an examle see here: http://stackoverflow.com/questions/516073/max-query-using-caml

share|improve this answer
    
+1, this works well in situations where items are added infrequently to the list. –  Kit Menke Aug 4 '11 at 18:02
add comment

I don't think your approach or the idea of performing a CAML query would work (...at least not consistently), primarily due to your inability to natively manage concurrency. It would be challenging enough to handle it in a single WFE environment, but if you have multiple WFEs you would most assuredly have multiple instances of an event receiver or query executing at the same time, returning the same "last ID" value and then all setting their LogIDNumber property to the same last+1 value.

I suppose you could create an ID list with one column (LogIDNumber), make it a Number type, and make sure Enforce unique values = Yes. You can then add an item to that list in your event receiver and if it chokes then you know another EV instance got there first and you need to requery the last ID until the insert into the ID list doesn't choke. If it doesn't choke then your EV instance owns the ID and can safely update the LogIDNumber property for that list item.

Can I ask what requirement(s) are causing you to create your own auto-icrement column versus using ID?

share|improve this answer
    
I agree with Rob D'Oria. We took that approach with an event receiver and it worked out well. For one thing, performance is better than querying for the highest number +1 of a list. Other benefits are that it gives you more flexibility if you were to need to start renumbering at higher or lower number down the road. –  Rob Wilson Aug 4 '11 at 16:06
    
That is what I meant when saying "Should probably have a distinct check as well." –  HaavardMeling Aug 5 '11 at 14:25
add comment

I suggest using a reference list / lookup column. In your itemadding event, open a list called docid's, which has 2 columns, listUrl and currentId. Get the correct item based on he list url (current list url == item in doc id lib's listUrl), get the value of the currentId and increment it. Then use the new value as your new doc id.

There are a lot of drawbacks though, which all have to do with 1 thing: sharepoint is not "transactional". So if any 2 users perform this action at the same time, it could happen they both get the same id. To cirumvent this, you'd need an itemupdating event handler on he doc id list too, checking if the new value != current value (increment by different user). This is of course also no guarantee for success...

Another option in SP2010 is the doc id feature, which guarantees unique id's across all content, and allows for easy access through search / redirect url..

share|improve this answer
add comment

I assume that you will need some sort of algorithm to calculate your custom ID so why not simply create your own column and then use either the item's integer ID or Guid UniqueID as the 'seed' for your ID algorithm and then populate that column in the event receiver?

share|improve this answer
add comment

You could use document ID service for this one. This unique ID is (I believe) not generated realtime, but on a timerjob base. And it only works for content types that are derived from a document content type. No programming required for this one ;)

http://msdn.microsoft.com/en-us/library/ee559302.aspx http://blogs.technet.com/b/blairb/archive/2009/10/20/new-document-id-feature-in-sharepoint-2010.aspx http://www.sharepointanalysthq.com/2010/04/document-id-feature/

share|improve this answer
    
That's a globally unique identifier, not something that can be incremented per list. I assume whatever requirement that is preventing the use of the ID property would also preclude this solution as well. –  Rob D'Oria Aug 4 '11 at 16:29
    
true that. Really interested into the underlying requirements for the unique identifier. –  Bas Lijten Aug 4 '11 at 22:07
add comment

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.