0

YYYYMMDD-XX

Suppose List Item is created today Its Unique ID should be

20150604-01
20150604-02
.....
20150604-99

Suppose Item is created Tomorrow Item Unique ID should be

20150605-01
20150605-02
.....
20150605-99

We are able format date in required way. How do we set set the counters to start again from 01-99 each day. Can someone help on this

1 Answer 1

0

You'll need the following things to achieve what you're trying to:

  • 1 ItemAdding event receiver which is attached to every list (only those lists apply which use your custom unique ids) link
  • a central place where you store your current id (e.g. SPWeb PropertyBag) so you can increment it whenever a value has been used

public static class PropertyBagExtension {

public static void AddProperty(this SPWeb web, string key, string value)
{
    web.AllowUnsafeUpdates = true;
    web.Properties[key] = value;
    web.AllProperties[key] = value;
    web.Update();
    web.Properties.Update();
}


public static void RemoveProperty(this SPWeb web, string key)
{
    web.AllowUnsafeUpdates = true;
    web.AllProperties.Remove(key);
    web.Properties[key] = null;
    web.Update();
    web.Properties.Update();
}


public static string GetPropertyValue(this SPWeb web, string key)
{
    if (web.AllProperties.ContainsKey(key))
        return web.AllProperties[key].ToString();

    return null;
}

}

  • a mechanism which ensures every day, that the id is set to 01 again in the beginning (could be done in the event receiver too)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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