1

I would like to auto-generate a number for each item (row) in my SharePoint list. I can't use the default ID column, because i can't use the ID column in a calculated field. I will need to use this auto-generated number in a CONCATENATE formula for another column in this list. (I want to use this number to concatenate a column that outputs "PRJ###" for each item) Thanks for any help!

  • 1
    I would suggest to still use the ID column, and once the item is added, update it again so the Calculated column get's the newly generated ID.. You can do this by simply adding an Event Receiver and making a fake update.. This way you will not have to manage your own auto-generated number.. – Arsalan Adam Khatri Mar 10 '15 at 19:22
  • It May help you, Refer below link > dlairman.wordpress.com/2011/01/10/… – user40400 Mar 11 '15 at 10:22
1

There are at least 3 ways that you could go about doing this and it will depend mostly on what your business users feel comfortable with and what you feel comfortable with as the person creating the solution.

  1. Add a text field to your list that is hidden in the NewForm.aspx and use PowerShell running as a scheduled task (every 5 minutes or so) to fill in the field of new items that do not have a value for this field. Here is some example code. Let's assume that your list is called "Orders" and that the custom field you have added is called OrderNumber

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$gc = start-spassignment
$web = ($gc | Get-SPWeb http://portal.somedomain.com);
$ordersList = $web.Lists["Orders"];

$spQuery = New-Object Microsoft.SharePoint.SPQuery;
$spQuery.Query = "<Where><IsNull><FieldRef Name='OrderNumber' /></IsNull></Where>";
$orders = $ordersList.GetItems($spQuery);

foreach($order in $orders)
{
   #If you want to continue the same number scheme, add the last value used by your
   # previous system to the item's ID. If not, just use the ID in the format method.
   $orderNumericValue = $order.ID + 350;
   $order["OrderNumber"] = [String]::Format("PRJ{0:0000}", $orderNumericValue);
   $order.Update();
}

$gc | stop-spassignment
  1. Use a SharePoint designer workflow to fill in the field. I won't go into much detail here as this solution is well documented on the web.

  2. Use an ItemAdded Event Receiver. This, IMO, is the best option.

public override void ItemAdded(SPItemEventProperties properties)
{
   base.ItemAdded(properties);
   //Get a new handle on the list item.
   SPListItem item = properties.List.GetItemById(properties.ListItemId);
   item["OrderNumber"] = String.Format("PRJ{0:0000}", item.ID);
   item.Update();
}
0

How about using a "time" generated value for this? e.g:

PRJ&[Today]

  • I know what you're getting at, but in order to remain consistent with past records, it needs to be in a ABC### format. – Alex Mar 10 '15 at 20:09
  • 1
    And you can't use [Today] or Today() because the Formula will be re-evaluated on every Item update... generating a new date – Danny '365CSI' Engelman Mar 10 '15 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.