19719

Sitecore - Custom field, add unique value on create [closed]

Question:

I want to create a field that generates a running number. This number will be automatically generated everytime I create a new item and has to be unique.

Any examples of how this could be implemented? I don't want to use the Sitecore item ID for this.

Answer1:

You can implement a custom token and just use your field. I think this is going to be the cleanest solution for your problem. You can add your custom algorithm to guarantee that the IDs are unique or you can just use Guid.NewGuid(). You can check how to create custom tokens in this <a href="http://nsgocev.wordpress.com/2014/08/03/sitecore-custom-tokens/" rel="nofollow">blog post</a>.

Answer2:

Alright. I came up with a solution inspired by the nsgocev blogpost. Our IDs needed to be stored somewhere, so I created an item inside the /sitecore/content/ that stores the last ID as a string. Set the start as "AA000000". Our Ids has a prefix of "AA" and 6 digits.

This is the logic that counts:

Namespace Tokens Public Class GeneratedArticleId Inherits ExpandInitialFieldValueProcessor Public Overrides Sub Process(ByVal args As ExpandInitialFieldValueArgs) If args.SourceField.Value.Contains("$articleid") Then Dim database = Sitecore.Client.ContentDatabase Dim counter = database.GetItem(New ID("Our Item")) If counter Is Nothing Then args.Result = "" Exit Sub End If Dim idfield = AppendToIdValue(counter("ID")) Using New SecurityDisabler() counter.Editing.BeginEdit() counter.Fields("ID").Value = idfield counter.Editing.EndEdit() End Using If args.TargetItem IsNot Nothing Then args.Result = args.Result.Replace("$articleid", idfield) End If End If End Sub 'Extracts the digits and adds one Private Shared Function AppendToIdValue(ByVal id As String) Dim letterprefix = Left(id, 2) Dim integervalue = CInt(id.Replace(letterprefix, "")) integervalue += 1 Return letterprefix & integervalue.ToString("000000") End Function End Class End Namespace

We also need to add our class to the web config file. Patch given class in:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <pipelines> <expandInitialFieldValue help="Processors should derive from Sitecore.Pipelines.ExpandInitialFieldValue.ExpandInitialFieldValueProcessor"> <processor patch:after="*[@type='Sitecore.Pipelines.ExpandInitialFieldValue.ReplaceVariables, Sitecore.Kernel']" type="OurLibrary.Tokens.GeneratedArticleId, OurLibrary"/> </expandInitialFieldValue> </pipelines> </sitecore> </configuration>

Now when we create a new item with token "$articleid", the ID is AA000001. The next one will be AA000002 and so on.

Thank you @nsgocev for resource and answer.

Recommend

  • How to make shape's child TextView white when state_pressed=“true”
  • Modify a Google App Engine entity id?
  • Regexp, capture between parentheses, javascript
  • Log4j cannot find properties file in a simple application
  • Express displaying mongodb documents in Jade
  • Search files(key) in s3 bucket takes longer time
  • How can I see a list of all files that are different between two Hg repositories?
  • What does Main.1 output file do in XCode?
  • Ruby and class variables in inherit class
  • Receiver has no segue with identifier“***”
  • Python cosine function precision [duplicate]
  • Updating Dojo provide
  • Calculate time difference in hh:mm:ss with simple javascript/jquery
  • Autofac with Web API 2 - Parameter-less constructor error
  • Android changing fragment order inside FragmentPagerAdapter
  • Validate child input components on submit with Vee-Validate and vue js 2
  • how does System.Web.HttpRequest::PathInfo work?
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • RxJava debounce by arbitrary value
  • Not able to aggregate on nested fields in elasticsearch
  • Keep this build forever option - Jenkins
  • onBackPressed() not being executed
  • Converting a WriteableBitmap image ToArray in UWP
  • Is there any way to access browser form field suggestions from JavaScript?
  • Incrementing object id automatically JS constructor (static method and variable)
  • ActionScript 2 vs ActionScript 3 performance
  • Hazelcast - OperationTimeoutException
  • Transpose CSV data with awk (pivot transformation)
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Buffer size for converting unsigned long to string
  • python regex in pyparsing
  • Free memory of cv::Mat loaded using FileStorage API
  • How to set the response of a form post action to a iframe source?
  • Why joiner is not used after Sequence generator or Update statergy
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • Qt: Run a script BEFORE make
  • unknown Exception android
  • reshape alternating columns in less time and using less memory
  • How can I use threading to 'tick' a timer to be accessed by other threads?