Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How can I retrieve the GUID of specific record using it's name, in C#?

Posted on by Microsoft Employee

Does anyone know a simple way how I can retrieve the GUID of specific record using it's name, in C#?

Entity: msdyn_contractorganizationalunitid

Record Name: Fabrikam Global

*This post is locked for comments

  • Pranesh M Juwale Profile Picture
    Pranesh M Juwale 1,196 on at
    RE: How can I retrieve the GUID of specific record using it's name, in C#?

    Hope this link will help you with simplest c# code to retrieve entity record guid from entity name :

    community.dynamics.com/.../242514

    Regards,

    Pranesh

  • Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: How can I retrieve the GUID of specific record using it's name, in C#?

    Hi Redhead,

    Did you resolved your issue?

    Best Regards,

    Shahbaaz

  • Verified answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: How can I retrieve the GUID of specific record using it's name, in C#?

    You need to use Queryexpression as we did in above code...you need to keep in mind you pass correct entity name in entity, schema name of filed in condition as we did above.

    Thanks,

    Shahbaaz

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How can I retrieve the GUID of specific record using it's name, in C#?

    How can i query a second and third entity in another records as we have just done in the same code?

    Id believe i need to set these two fields which are both lookup's also:

    ProjectDetail["msdyn_workhourtemplate"] to be set too the record with the name "Default Work Template";
    ProjectDetail["msdyn_projecttemplate"] to be set too the record with the name "TEST-Template";

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: How can I retrieve the GUID of specific record using it's name, in C#?

    In the above code you are setting value for "msdyn_projectid","msdyn_subject" and "msdyn_contractorganizationalunitid" is there any other field on Form which is Required field, if yes then you have to set that field value in above code.

    Let me know if i am not clear.

    Best Regards,

    Shahbaaz

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How can I retrieve the GUID of specific record using it's name, in C#?

    Thanks, I now get the following error:

    Entered CrmVSSolution1.ProjectCreation.PostOperationquoteCreate.Execute(), Correlation Id: e44ea241-62d9-4716-a070-5dd1810f787b, Initiating User: 21cfc663-f73f-4b21-a6a4-438c074c5ab1

    Exception: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Value cannot be null.

    Parameter name: input (Fault Detail is equal to Exception details:

    ErrorCode: 0x80040265

    Message: Value cannot be null.

    Parameter name: input;

    [Microsoft.Dynamics.ProjectService.Plugins: Microsoft.Dynamics.ProjectService.Plugins.PreProjectCreate]

    [c9b474ef-86e4-e411-80e4-00155db85902: PreProjectCreate]

    Starting sync workflow 'Due date validation', Id: 6a9638da-c327-e811-a959-002248004894

    Entering ConditionStep1_step:

    Sync workflow 'Due date validation' completed successfully

    Starting sync workflow 'Actual end date validation', Id: 799638da-c327-e811-a959-002248004894

    Entering ConditionStep1_step:

    Sync workflow 'Actual end date validation' completed successfully

    Entered Microsoft.Dynamics.ProjectService.Plugins.PreProjectCreate.Execute(), Correlation Id: e44ea241-62d9-4716-a070-5dd1810f787b, Initiating User: 21cfc663-f73f-4b21-a6a4-438c074c5ab1

    Exiting Microsoft.Dynamics.ProjectService.Plugins.PreProjectCreate.Execute(), Correlation Id: e44ea241-62d9-4716-a070-5dd1810f787b, Initiating User: 21cfc663-f73f-4b21-a6a4-438c074c5ab1

    TimeSta...)., Correlation Id: e44ea241-62d9-4716-a070-5dd1810f787b, Initiating User: 21cfc663-f73f-4b21-a6a4-438c074c5ab1

    Exiting CrmVSSolution1.ProjectCreation.PostOperationquoteCreate.Execute(), Correlation Id: e44ea241-62d9-4716-a070-5dd1810f787b, Initiating User: 21cfc663-f73f-4b21-a6a4-438c074c5ab1

    Error Message:

    Unhandled Exception: Microsoft.Xrm.Sdk.InvalidPluginExecutionException: OrganizationServiceFault

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: How can I retrieve the GUID of specific record using it's name, in C#?

    Hi Redhead,

    I think your above code is correct, please use below code and let me know if you are getting any error.

    Entity Project = context.InputParameters["Target"] as Entity;

              Entity ProjectDetail = new Entity("msdyn_project");

              ProjectDetail["msdyn_projectid"] = new EntityReference() { Id = Project.Id, LogicalName = Project.LogicalName };

              ProjectDetail["msdyn_subject"] = "New Project";

              QueryExpression getguid = new QueryExpression()

              {

                  EntityName = "msdyn_organizationalunit",

                  Criteria ={

                      Conditions =

                      {

                          new ConditionExpression("msdyn_name",ConditionOperator.Equal,"Fabrikam Global"),

                          },

                      }

              };

              EntityCollection retrievegetguid = service.RetrieveMultiple(getguid);

              if (retrievegetguid.Entities.Count > 0)

              {

                  Guid Id = retrievegetguid.Entities[0].Id;

                  ProjectDetail["msdyn_contractorganizationalunitid"] = new EntityReference("msdyn_organizationalunitid", Id);

              }

              service.Create(ProjectDetail);

    Thanks,

    Shahbaaz

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How can I retrieve the GUID of specific record using it's name, in C#?

    Yes i still get an error: A local or parameter named 'ProjectDetail' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter.

               QueryExpression getguid = new QueryExpression()

               {

                   EntityName = "msdyn_organizationalunitid",

                   Criteria ={

                     Conditions =

                     {

                         new ConditionExpression("msdyn_name",ConditionOperator.Equal,"Fabrikam Global"),

                         },

                     }

               };

               EntityCollection retrievegetguid = service.RetrieveMultiple(getguid);

               if (retrievegetguid.Entities.Count > 0)

               {

                   Entity ProjectDetail = new Entity("projectdetail");

                   Guid Id = service.Create(ProjectDetail);

                   ProjectDetail["msdyn_contractorganizationalunitid"] = new EntityReference("msdyn_organizationalunitid", Id);

               }

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: How can I retrieve the GUID of specific record using it's name, in C#?

    nothing, forget that, are you still facing any issue??

    if not that dont forget to close the thread by marking answer Verified.

    Best Regards,

    Shahbaaz

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How can I retrieve the GUID of specific record using it's name, in C#?

    Hi again, when you say " //Pass the project Detail Guid here which record you want to update" i'm not actually updating one, instead i;m creating a new project and including this field.

    Entity Project = context.InputParameters["Target"] as Entity;

               Entity ProjectDetail = new Entity("msdyn_project");

               ProjectDetail["msdyn_projectid"] = new EntityReference() { Id = Project.Id, LogicalName = Project.LogicalName };

               ProjectDetail["msdyn_subject"] = "New Project";

               QueryExpression getguid = new QueryExpression()

               {

                   EntityName = "msdyn_organizationalunit",

                   Criteria ={

                       Conditions =

                       {

                           new ConditionExpression("msdyn_name",ConditionOperator.Equal,"Fabrikam Global"),

                           },

                       }

               };

               EntityCollection retrievegetguid = service.RetrieveMultiple(getguid);

               if (retrievegetguid.Entities.Count > 0)

               {

                   Guid Id = retrievegetguid.Entities[0].Id;

                   ProjectDetail["msdyn_contractorganizationalunitid"] = new EntityReference("msdyn_organizationalunitid", Id);

               }

               service.Create(ProjectDetail);

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans