我对Dynamics CRM 4.0 Web服务有疑问。我一直用它来从CRM获取记录到ASP.NET。在请求和转换之后,可以通过;

访问列的值(例如,对于联系人)
BusinessEntity be = getBusinessEntity(service, crmGuid, type, colnames);
contact tmp = (contact)be;

Response.Write("firstname: " + tmp.firstname + "<BR>");
Response.Write("lastname: " + tmp.lastname+ "<BR>");

我有一个字符串数组,用于标识应从CRM( colnames )检索哪些列,例如在这种情况下 {&quot; firstname&quot;,&quot; lastname&quot;}

但是 colnames 会变得很大(并且可能没有硬编码),所以我不想一个接一个地浏览它们。有没有办法使用像

这样的东西
for(int i = 0; i < colnames.length; i++)
{
    Response.write(colnames[i] + ": " + tmp.colnames[i] + "<BR>");
}

如果我现在这样做,我会收到一个错误,即colnames不是tmp的字段。 有什么想法吗?

有帮助吗?

解决方案

不使用BusinessEntity(除非您使用反射)。 DynamicEntity可以通过从Property派生的类型进行枚举。你必须做类似的事情(我是从内存中做到的,所以可能无法编译)......

for(int i = 0; i < colnames.length; i++)
{
  string colName = colnames[i];
  foreach(Property prop in tmp)
  {  
    if (prop.name != colName)
      continue;
    if (prop is StringProperty)
    {
       var strProp = prop as StringProperty;
       Response.Write(String.Format("{0}: {1}<BR />", colName, strProp.Value));
    } 
    else if (prop is LookupProperty)
    {
      ...
     }
    ... for each type deriving from Property

  }
}

回复注1(长度):

你能给我一个你正在使用的例子吗?如果您只查看基类型(Property),那么您将无法看到value属性 - 您需要转换为适当的类型(StringProperty等)。

在我的示例中,tmp是一个DynamicEntity(它定义了GetEnumerator,它返回一个Property数组)。访问DynamicEntity属性的另一种方法是使用字符串索引器。对于tmp:

string firstname = (string)tmp["firstname"];

请注意,如果使用此方法,则会获得值(字符串,CrmNumber,Lookup),而不是整个属性(StringProperty,CrmNumberProperty等)。

这会回答你的问题吗?另外,我建议使用SDK程序集而不是Web引用。它们更容易使用。但是,如果您选择使用Web引用,则SDK下载具有帮助程序类列表。搜索“帮助者”在SDK中。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top