Summary: Learn to create a unique identifier in Windows SharePoint Services 3.0 for all items contained within a site.
Applies to: Windows SharePoint Services 3.0
Microsoft Corporation
March 2008
If you like me dislike gotos, this code might work better for you:
private static int GetUniqueId(SPWeb web)
{
return GetValue(web, 0);
}
private static int GetValue(SPWeb web, int retryCounter)
{
int uniqueId = -1;
int returnValue = -1;
try
{
// Get the list and list item
SPList list = web.Lists["Counter"];
SPListItem item = list.Items[0]; // Get the value for the unique id
uniqueId = int.Parse(item["Title"].ToString());
// Increment and update the value
item["Title"] = uniqueId + 1;
item.Update();
// Set the value to return
returnValue = uniqueId;
}
catch (Exception ex)
{
// Handle the exception by retrying 5 times
retryCounter += 1;
if (retryCounter <= 5)
{
System.Threading.Thread.Sleep(2500);
return GetValue(web, retryCounter);
}
else
{
// Write the exception to the log
}
}
return returnValue;
}
Basically the same codebase ut we use an external SQL data connection to autoincrement the numbers.
It works OK is you stick to content types and new created docs.
In practice we see a lot of File/Save As actions by office personel to create new documents instead of using templates.
I know this is bad behaviour, but it just works that way in practice.
If a document already has the Sharepoint applied document properties, the property bag analyzer tries to apply them to the colums.
The property bag analyzer can not be directed in one direction in the sense that only documents added will be left alone and let the feature apply a number. You can disable the property handler temporary as far as I discovered, but you cannot empty bogus properties available in documents
We solved it temporary by writing a word macro capture for the FileSaveAs() function and empty the server properties before it arrives in sharepoint.
But it should be handled on the sharepoint server IMHO. Same issues when using the barcode feature. All kinds of dups pop up.
We are running Sharepoint 2007 SP2 here, not sure if this works the same in 2010.
Hopefully you are still reading these posts all this time later. I was trying to add a unique identifier to each document and saw your response. My question is : does each one of these id numbers start with 1? Is there any way to start the first id number with a higher number that actually looks like a reference number.
Thanks
The major problem with this technique is concurrency .
2 or more users with fast machines and network connections ( or many queued docuemnts) can cause this to deadlock.
Pro s
Easy to do , easy to deply and staple the feature to the site if you want and makes your life easy , as it is an event handler for on the onCreate or onNew.
Final Solution
I have been in this situation a few times, and here is the final working solution.
Why reinvent the wheel when SharePoint already has a Sequence generator that is Thread safe it is called (SharePoint List )
Simply on every Item you need a a new ID , create an Item in the new List , get the ID and give it to your Document List , announcent , or whatever Items you want Unique ID for.
Code is simple but not public.
Thanks and good luck
George Gergues
SharePoint Architect.
http://wm.microsoft.com/ms/msdn/office/2007OfficeVisualHowTos/WSS30GenerateUniqueID.wmv
I have to wonder the same as the former poster....how do you ensure the same ID is not generated twice, or more without some locking mechanism?
// Get the value for the unique id
uniqueId = int.Parse(item["Title"].ToString());
// Increment and update the value
item["Title"] = uniqueId + 1;
item.Update();
I can definately see the issue where two users could generate two documents and winde up with the same ID thus causing some problems.
uniqueId = int.Parse(item["Title"].ToString());
and when Update is called?
item.Update();
I would have thought you would need check in/check out enabled on the list to prevent locking.
