Question

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?

Was it helpful?

Solution

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: https://stackoverflow.com/questions/516073/max-query-using-caml

OTHER TIPS

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?

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

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?

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top