Bhubaneswar, Odisha, India
+91-8328865778
support@softchief.com

Sequencial GUID in C#

Sequencial GUID in C#

Using windows UuidCreateSequential method we can create sequential GUID while creating records in CRM if required. This is a C# code using an in-built method of rpcrt4.dll.

Sequencial GUID in C#

I recommend to allow the system to create GUIDs without using custom code. But if requirement strictly demands then we can use the below c# code to generate sequential GUIDs.

Use Below Namespaces

 using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Runtime.InteropServices;

Class Definition

public class SQLSequentialGuidUtility
 {
 [DllImport("rpcrt4.dll", SetLastError = true)]
 static extern int UuidCreateSequential(out Guid guid);

 public static Guid GetNewSequentialGUID()
 {
 Guid guid;
 UuidCreateSequential(out guid);
 var s = guid.ToByteArray();
 var t = new byte[16];
 t[3] = s[0];
 t[2] = s[1];
 t[1] = s[2];
 t[0] = s[3];
 t[5] = s[4];
 t[4] = s[5];
 t[7] = s[6];
 t[6] = s[7];
 t[8] = s[8];
 t[9] = s[9];
 t[10] = s[10];
 t[11] = s[11];
 t[12] = s[12];
 t[13] = s[13];
 t[14] = s[14];
 t[15] = s[15];
 return new Guid(t);
 }
}

Hope this will help others as it helped me.