如何从实体中检索记录或视图?

时间:2015-09-23 23:12:01

标签: c# .net dynamics-crm-2013

我想从这些属性中获取值:

class Program
{
    private static OrganizationService _orgService;

     private static void Main(string[] args)
     {
         ClientCredentials cre = new ClientCredentials();
         cre.UserName.UserName = "login";
         cre.UserName.Password = "password";

         Uri serviceUri = new Uri("some_adress");

         OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, cre, null);
         proxy.EnableProxyTypes();
         IOrganizationService service = (IOrganizationService) proxy;

         retrieveEntityRequest = new RetrieveEntityRequest
         {
             EntityFilters = EntityFilters.All,
             LogicalName = "new_sms_parameters",
             RetrieveAsIfPublished = true
         };

         retrieveEntityResponse = (RetrieveEntityResponse) service.Execute(retrieveEntityRequest);
         currentEntity = retrieveEntityResponse.EntityMetadata;

         // Attributes


         foreach (AttributeMetadata allattributes in currentEntity.Attributes)
         {
             Console.WriteLine("SMS Parameters: " +   allattributes.LogicalName);
         }
     }
}

这里我连接到CRM并获取属性名称,但我不知道如何获得这些值。我接下来该怎么办?

1 个答案:

答案 0 :(得分:1)

RetrieveEntityRequest用于获取描述实体的元数据,例如;字段数,字段类型,如字符串,int等。

如果您想获取记录信息,则需要使用Retrieve按Id获取单个记录,或RetrieveMultiple根据查询获取多条记录。

检查上面的链接以获取完整示例,但有效:

ColumnSet attributes = new ColumnSet(new string[] { "name", "ownerid", "address1_postalcode" });

account = service.Retrieve(account.LogicalName, accountId, attributes);

String postcode = account["address1_postalcode"];
String alsoPostcode = account.GetAttributeValue<String>("address1_postalcode");