Autonumber Attribute Generation: Programmatically or Non-Programmatically


Autonumber fields automatically generate alphanumeric strings whenever they are created. Makers can customize the format of these fields to their liking and then rely on the system to generate matching values that automatically fill them in at runtime.

We have an auto number attribute available that is out of the box but not for all entities.

Why can the OOB auto number not be used all the time?

OOTB Auto Number Feature is supported for limited Entities listed below.

  1. Contracts
  2. Cases
  4. Orders
  5. Invoices
  6. Campaigns
  7. Articles
  8. Categories
  9. Knowledge Articles
  • No OOTB way for Other
  • Implement Auto Number Logic on your own Using Custom Solutions.
  • You cannot create an auto-number attribute that uses any other special format, such as Email, Phone, TextArea, URL, or any other existing format.

Ways of creating auto number attribute

  1. Creation of auto number non-programmatically
    1. Using power apps
  2. Creation of auto number programmatically
    1. Using Console application
    2. Using Web API + POSTMAN

Auto Number Format Supports the following format tokens

  • Static String
  • DATE:[format] e.g. ddmmyyyy
  • SEQNUM: size

Example of Auto Number Format Value

AutoNumberFormat value Example value
{SEQNUM:6}-#-{RANDSTRING:3} 123456-#-R3V
KA-{SEQNUM:4} KA-0001
{SEQNUM:10} 1234567890
CAS-{SEQNUM:6}-{RANDSTRING:6}-{DATETIMEUTC:yyyyMMddhhmmss} CAS-002000-S1P0H0-20170913091544
CAS-{SEQNUM:6}-{DATETIMEUTC:yyyyMMddhh}-{RANDSTRING:6} CAS-002002-2017091309-HTZOUR
CAS-{SEQNUM:6}-{DATETIMEUTC:yyyyMM}-{RANDSTRING:6}-{DATETIMEUTC:hhmmss} CAS-002000-201709-Z8M2Z6-110901

Auto-Number Attribute Creation in Dynamics 365 with Power Apps

Using power apps, you can add an auto-number attribute. While auto number fields are formally just text fields with additional functionality built on top of them, Power Apps simplifies this concept by simply exposing Autonumber as a distinct data type under the Text category.

Create an auto number field

Sign in to the Power Apps portal

Establish a connection with your org. Go to Data and select Connections.

Power app

Enter your organization credentials; the connection is established.

On the left pane, expand Data and select Entities.

Select the entity to which you want to add an auto number field and then select the Fields tab.

On the toolbar, select the Add field.

On the right pane, enter a Display name and select Autonumber for the Data type.

Select Autonumber

Set optional fields as needed.

Select an auto number type or keep the default String prefixed number option.

Customize a seed value or keep the default value of 1000.

Select Done.

Auto-Number Attribute in Dynamics CRM 365 with Organization Service and Console App

With the Dynamics 365 v9, you can add an auto-number attribute for any entity. Currently, you can add the attribute programmatically. There is no user interface to add this type of attribute.

There are two ways of doing this: one is using c# code, which you can use as a console application, and the other is a Web API request.

Create a simple console application.

  1. Open Visual Studio, click on new project, select console application project type, give it a name of "AutoNumber," and click on OK.
  2. From your solution manager, right-click on "References" and click on "Manager nuget package":
  3. In the search box, enter the name "Microsoft.CrmSdk.CoreAssemblies." When the result comes up, select it and click on Install. This will install the Dynamics CRM core assemblies reference needed in the application.
  4. Now from your solution explorer, click on add references and again select "System. Configuration", "System.ServiceModel "and "System.Runtime.Serialization assemblies ".

Use the below namespaces.

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
using System.Configuration;
using System.Net;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

Copy and paste the below code in the application.

IOrganizationService organizationService = null;
    ClientCredentials clientCredentials = new ClientCredentials();
    clientCredentials.UserName.UserName = ConfigurationManager.AppSettings["Username"];
    clientCredentials.UserName.Password = ConfigurationManager.AppSettings["Password"];

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri(ConfigurationManager.AppSettings["CRMUrl"]),
     null, clientCredentials, null);

    if (organizationService != null)
        Guid userid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;

        if (userid != Guid.Empty)
            Console.WriteLine("Connected to dynamics crm");
            CreateAttributeRequest widgetSerialNumberAttributeRequest = new CreateAttributeRequest
                EntityName = "new_autonumber",
                Attribute = new StringAttributeMetadata
                    //Define the format of the attribute
                    AutoNumberFormat = "DPR-{SEQNUM:5}-{RANDSTRING:6}-{DATETIMEUTC:yyyyMMddhhmmss}",
                    LogicalName = "new_serialnumber",
                    SchemaName = "new_SerialNumber",
                    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
                    MaxLength = 100, // The MaxLength defined for the string attribute must be greater than the length of the AutoNumberFormat value, that is, it should be able to fit in the generated value.
                    DisplayName = new Label("Serial Number", 1033),
                    Description = new Label("Serial Number of the widget.", 1033)
            Console.WriteLine("Created the autonumber attribute successfully..");
        Console.WriteLine("Failed to Established Connection!!!");
catch (Exception ex)
    Console.WriteLine("Exception caught - " + ex.Message);

In the code above

  • Change the entity logical to the name of the entity where you want to create an auto number field.
  • Give the required logical name of the attribute(which you would like to create).

Notice the code line.

AutoNumberFormat = "DPR-{SEQNUM:5}-{RANDSTRING:6}-{DATETIMEUTC:yyyyMMddhhmmss}"

In the code above, you can define your auto number's format.

Now, in your app.config put below( Insert the correct organization URL , username , password):

<?xml version="1.0" encoding="utf-8"?>
<add key="CRMUrl" value="" />
<add key="Username" value="Username" />
<add key="Password" value="Password" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>

Build your console application(right-click on the solution and click "Build Solution" and then Run(Start) it.

If it gets executed fine, go to your dynamics CRM and open the entity fields from Settings > customizations > customize the system > entityname> fields. You will notice the field is created.

Customize the form and add this field to the form post, which tries to create a record in Dynamics CRM.

Note. that you can always update this attribute if created incorrectly by using the code given here and the same application we created the Update AutoNumber attribute.


We can utilize this feature very well for creating an auto number field in CRM. Before this, we needed to create a plugin that would trigger every time or workflow with a very complex configuration.

Similar Articles