2009-06-30 80 views
4

我试图从CRM安装中获取信息,到目前为止,这对于使用默认字段很合适。但是,我无法检索自定义字段,例如,联系人有一个名为web_username的自定义字段。使用Webservice查询检索Dynamics CRM自定义字段

我目前的代码是

 QueryExpression query = new QueryExpression(); 
     query.EntityName = "contact"; 
     ColumnSet cols = new ColumnSet(); 
     cols.Attributes = new string[] { "firstname", "lastname" }; 
     query.ColumnSet = cols; 

     BusinessEntityCollection beReturned = tomService.RetrieveMultiple(query); 
     foreach (contact _contact in beReturned.BusinessEntities) 
     { 
      DataRow dr = dt.NewRow(); 
      dr["firstname"] = _contact.firstname; 
      dr["lastname"] = _contact.lastname; 
      dt.Rows.Add(dr); 
     } 

怎样包括我的查询自定义字段?我试过四处寻找,但没有运气,但我可能会搜索不正确,因为我不习惯使用CRM术语。

提前欢呼!

+0

你可以参考你的解决了这个链接。 [https://stackoverflow.com/questions/43984397/retrieve-fetch-records-from-dynamic-365-crm-using-c-sharp-code/43985297#43985297][1] – Piyush 2017-06-12 14:36:40

回答

6

我已经能够解决这个问题。如果它对其他人有用,这就是我所做的。除了我已将自定义字段添加到ColumnSet之外,该查询的设置与以前一样。

cols.Attributes = new string[] { "firstname", "lastname", "new_web_username" }; 

,然后用来RetrieveMultipleResponse并请求设置为true

 RetrieveMultipleResponse retrived = new RetrieveMultipleResponse(); 

     RetrieveMultipleRequest retrive = new RetrieveMultipleRequest(); 
     retrive.Query = query; 
     retrive.ReturnDynamicEntities = true; 

     retrived = (RetrieveMultipleResponse)tomService.Execute(retrive); 

请评论还是如果有,我做这更好的方式ReturnDynamicEntities。

编辑 使用例如在我原来的问题,如果你投的接触

contact myContact = (contact)myService.Retrieve(EntityName.contact.ToString(), userID, cols); 

然后,您可以将对象

   phone = myContact.telephone1; 
      password = myContact.new_password; 

的访问属性如果您更新CRM Web引用自定义您在CRM中添加的字段可用。

0
public List<Entity> GetEntitiesCollection(IOrganizationService service, string entityName, ColumnSet col) 
    { 
         try 
         { 
          QueryExpression query = new QueryExpression 
          { 
           EntityName = entityName, 
           ColumnSet = col 
          }; 
          var testResult = service.RetrieveMultiple(query); 
          var testResultSorted = testResult.Entities.OrderBy(x => x.LogicalName).ToList(); 

      foreach (Entity res in testResultSorted) 
         { 
          var keySorted = res.Attributes.OrderBy(x => x.Key).ToList(); 
          DataRow dr = null; 
          dr = dt.NewRow(); 
          foreach (var attribute in keySorted) 
          { 
           try 
           { 
            if (attribute.Value.ToString() == "Microsoft.Xrm.Sdk.OptionSetValue") 
            { 
             var valueofattribute = GetoptionsetText(entityName, attribute.Key, ((Microsoft.Xrm.Sdk.OptionSetValue)attribute.Value).Value, _service); 
dr[attribute.Key] = valueofattribute; 


             } 
             else if (attribute.Value.ToString() == "Microsoft.Xrm.Sdk.EntityReference") 
             { 
              dr[attribute.Key] = ((Microsoft.Xrm.Sdk.EntityReference)attribute.Value).Name; 
             } 
             else 
             { 
              dr[attribute.Key] = attribute.Value; 
             } 
            } 
            catch (Exception ex) 
            { 
             Response.Write("<br/>optionset Error is :" + ex.Message); 
            } 
           } 
           dt.Rows.Add(dr); 
          } 
       return testResultSorted; 
       } 
         catch (Exception ex) 
         { 
          Response.Write("<br/> Error Message : " + ex.Message); 
          return null; 
         } 
      } 

//这里我提到彼此的功能:此功能的

var valueofattribute = GetoptionsetText(entityName, attribute.Key, ((Microsoft.Xrm.Sdk.OptionSetValue)attribute.Value).Value, _service); 

//定义如下:

public static string GetoptionsetText(string entityName, string attributeName, int optionSetValue, IOrganizationService service) 
    { 
       string AttributeName = attributeName; 
     string EntityLogicalName = entityName; 
     RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest 
     { 
      EntityFilters = EntityFilters.All, 
      LogicalName = entityName 
     }; 
     RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails); 
     Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata; 
     Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata; 
     Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet; 

     IList<OptionMetadata> OptionsList = (from o in options.Options 
              where o.Value.Value == optionSetValue 
              select o).ToList(); 
     string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label; 
     return optionsetLabel; 
    } 
相关问题