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 need to generate a record id for a document, with format YYYYMMDD9999 where 9999 is a autoincrement number per day. This id must of course be unique, but i have no idea how to be sure to avoid duplicates if i use an eventreceiver with a SPQuery to get the highest id of the day and add 1... Is there another approach I could try to generate it ?

Thanks.

share|improve this question

1 Answer 1

EDIT

the best and simplest solution is to use DateTime.Now.ToString(); as the id, format accordingly so that you get yyyymmdd hhmmss it will do the date today, plus the hour minutes and seconds. That way you will know it will be unique regardless and you dont need to bulk your code with alot of checks and loops :)

END EDIT

where are you saveing the info? on a sql server or is this just through code? if its through code you could get the list see the numbers with the current date and from that get the numbers and increment accordingly:

so if you have

201301011

201301012

as you can see when i have pulled that from where your saving be it from a list you can split its value to:

20130101 1

20130101 2

say todays date it 2013/01/01 and you have two records as todays date, get the values that you need, in this cas 1 and 2 itterate in a for loop and you would know you need to add +1 to that value so the next value is:

20130101 3 what == 201301013

That ways you know its unique as you only get the date once in that year so you just need to check that the number in that day is unique by going through the list (for loop) have the numbering in accesinding order 012345... ect. Then add a number on top so in your for loop you should have a varible that is constantly increamenting.. just +1 to it ;) and add it to the current date.

if its through sql its even easier ;) just get the list from a stored procedure, query the list get the values and increment, or better yet have the todaysdate + sql needs a unique column for secondry or primary so you could use that ;).

hope it helps !

share|improve this answer
    
thank you for your help. I store the value in a field of the document library. I already do as you described, but i think using this approach could result in duplicates in some conditions. What could happen if two users attempt to create a document at the same time ? They could both get the max id = 2 and add 1 so generate 201301013 for each document because everything runs asynchronously, couldn't they ? –  sle1306 Jan 9 '13 at 11:18
    
in this day and age its a possibility but highly unlikely! if your that worried you can use this codeproject.com/Articles/450288/Document-ID-Feature it will do it for you ;) but the principal is the same, to get that chance of getting the same id is a matter of nano seconds! obviously the more users you have the greater the chance but its highly unlikely! If your that worried than getting the id from sql is the best option as it has to be unique and is generated automatically regardless of two users at the same time :) both have up/down sides to space and performace/uniqness –  ali Sharepoint Jan 9 '13 at 11:27
    
OK thanks. DocumentID doens't provide the format I need, so I'll continue with event receivers. –  sle1306 Jan 9 '13 at 12:22
    
the best and simplest solution is to use DateTime.Now.ToString(); as the id, format accordingly so that you get yyyymmdd hhmmss it will do the date today, plus the hour minutes and seconds. That way you will know it will be unique regardless and you dont need to bulk your code with alot of checks and loops :) –  ali Sharepoint Jan 9 '13 at 12:35

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.