title: How to find Customer Entities logical name from Guid in Microsoft CRM
body: <p>In CRM I am trying to automate the process of creating a new email from a previous email in the chain. This email has to go to the customer of the case, who could be either an <code>account</code> or a <code>contact</code>. </p>
  
  <p>I can retrieve the Guid of the contact/account but I dont know how to retrieve the logical name.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>OrganizationServiceProxy service = CRMCentralCRMServiceInstance;
  
  Guid customerId = GetCustomerIdFromCase(caseId);
  
  Entity email = new Entity("email");
  
  Entity activityPartyTo = new Entity("activityparty");
  
  //"account" is a guess, it could be "contact"
  EntityReference customerReferenceTo = new EntityReference("account", customerId); 
  
  activityPartyTo["partyid"] = customerReferenceTo;
  EntityCollection toEntityCollection = new EntityCollection();
  toEntityCollection.Entities.Add(activityPartyTo);
  email["to"] = toEntityCollection;
  .
  .
  .
  newEmailId = service.Create(email);
  
  
  
  public Guid GetCustomerIdFromCase(Guid caseId) {
      Guid customerId = Guid.Empty;
      List&lt;CRMCase&gt; caseList = GetCRMCasesById(caseId);
  
      if (caseList.Count &gt; 0)
      {
          CRMCase cmcCase = caseList.First();
          customerId = cmcCase.CustomerId;
      }
  
      return (customerId);
  }
  
  public List&lt;CRMCase&gt; GetCRMCasesById(Guid caseId)
      {
          List&lt;CRMCase&gt; crmCases = new List&lt;CRMCase&gt;();
  
          try
          {
              OrganizationServiceProxy service = CRMCentralCRMServiceInstance;
  
              ConditionExpression condition1 = new ConditionExpression();
              ConditionExpression condition2 = new ConditionExpression();
  
              condition1.AttributeName = "incidentid";
              condition1.Operator = ConditionOperator.Equal;
              condition1.Values.Add(caseId.ToString("N"));
  
              condition2.AttributeName = "statecode";
              condition2.Operator = ConditionOperator.In;
              condition2.Values.Add("Active");
              condition2.Values.Add("Resolved");
  
              FilterExpression filter = new FilterExpression();
  
              filter.FilterOperator = LogicalOperator.And;
              filter.Conditions.Add(condition1);
              filter.Conditions.Add(condition2);
  
              QueryExpression query = new QueryExpression();
  
              query.EntityName = "incident";
              query.ColumnSet = new ColumnSet(true);
              query.Criteria = filter;
  
  
              RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest();
              retrieveAttributeRequest.EntityLogicalName = "incident";
              retrieveAttributeRequest.LogicalName = "statuscode";
              retrieveAttributeRequest.RetrieveAsIfPublished = true;
  
              RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
              StatusAttributeMetadata statusCodeAttribute = (StatusAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
  
  
              retrieveAttributeRequest = new RetrieveAttributeRequest();
              retrieveAttributeRequest.EntityLogicalName = "incident";
              retrieveAttributeRequest.LogicalName = "prioritycode";
              retrieveAttributeRequest.RetrieveAsIfPublished = true;
  
              retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
              PicklistAttributeMetadata priorityCodeAttribute = (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
  
  
              retrieveAttributeRequest = new RetrieveAttributeRequest();
              retrieveAttributeRequest.EntityLogicalName = "incident";
              retrieveAttributeRequest.LogicalName = "statecode";
              retrieveAttributeRequest.RetrieveAsIfPublished = true;
  
              retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
              StateAttributeMetadata stateCodeAttribute = (StateAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
  
  
              EntityCollection casesColl = service.RetrieveMultiple(query);
  
              foreach (Entity entity in casesColl.Entities)
              {
                  Entity incidentCRMCase = entity;
                  CRMCase cRMCase = GetCRMCaseFromIncidentCase(incidentCRMCase, statusCodeAttribute.OptionSet.Options, stateCodeAttribute.OptionSet.Options, priorityCodeAttribute.OptionSet.Options);
                  crmCases.Add(cRMCase);
              }
          }
          catch (SoapException se)
          {
              string action = MethodBase.GetCurrentMethod().DeclaringType.Name + " :: " + MethodBase.GetCurrentMethod().Name;
              string message = "Unexpected error in action: " + action
                                  + Environment.NewLine + se.Message
                                  + Environment.NewLine + se.Detail.InnerText;
  
              throw new Exception(message);
          }
  
          return (crmCases);
      }
  </code></pre>
  
  <p>I found <a href="http://www.crmanswers.net/2013/04/retrieve-entity-logical-name-from.html" rel="nofollow">this</a> brute force method but I would rather find a cleaner way if there is. </p>