通过变量检索 MS CRM 字段的值
我对 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
),例如本例中的 {"firstname", "lastname"}
。
但是 colnames
可能会变得相当大(并且可能不会被硬编码),所以我不想一一介绍它们。有没有办法使用类似的方法
for(int i = 0; i < colnames.length; i++)
{
Response.write(colnames[i] + ": " + tmp.colnames[i] + "<BR>");
}
如果我现在这样做,我会得到一个错误,即 colnames 不是 tmp 的字段。 有什么想法吗?
I have a question about the Dynamics CRM 4.0 Webservice. I've been using it to get records from CRM into ASP.NET. After the request and the casting, the values of the columns (for instance for a contact) can be accessed through;
BusinessEntity be = getBusinessEntity(service, crmGuid, type, colnames);
contact tmp = (contact)be;
Response.Write("firstname: " + tmp.firstname + "<BR>");
Response.Write("lastname: " + tmp.lastname+ "<BR>");
I have an array of strings which identify which columns should be retrieved from CRM (colnames
), for instance in this case {"firstname", "lastname"}
.
But colnames
can become quite big (and may not be hardcoded), so I don't want to go through them one by one. Is there a way to use something like
for(int i = 0; i < colnames.length; i++)
{
Response.write(colnames[i] + ": " + tmp.colnames[i] + "<BR>");
}
If I do this now I get an error that colnames is not a field of tmp.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不使用 BusinessEntity(除非您使用反射)。 DynamicEntity 可通过从 Property 派生的类型进行枚举。你必须做类似的事情(我是凭记忆做的,所以可能无法编译)...
回复注释1(长度):
你能给我一个你正在使用的例子吗?如果您只查看基本类型(属性),那么您将无法看到值属性 - 您需要转换为适当的类型(StringProperty 等)。
在我的示例中,tmp 是一个 DynamicEntity(它定义了 GetEnumerator,它返回一个 Property 数组)。访问 DynamicEntity 属性的另一种方法是使用字符串索引器。对于 tmp:
请注意,如果使用此方法,您将获得值(字符串、CrmNumber、Lookup),而不是整个属性(StringProperty、CrmNumberProperty 等)。
这能回答你的问题吗?另外,我建议使用 SDK 程序集而不是 Web 参考。它们更容易使用。不过,如果您选择使用 Web 参考,则 SDK 下载包含帮助程序类列表。在SDK中搜索“Helper”。
Not using BusinessEntity (unless you use reflection). DynamicEntity is enumerable by types deriving from Property. You'll have to do something like (I did this from memory, so might not compile)...
Reply to Note 1 (length):
Could you give me an example of what you're using. If you are only looking at the base types (Property) then you won't be able to see the value property - you'll need to convert to the appropriate type (StringProperty, etc).
In my example tmp is a DynamicEntity (it defines GetEnumerator which returns an array of Property). The other way to access the properties of a DynamicEntity is using the string indexer. For tmp:
Note that if you use this method, you get the Values (string, CrmNumber, Lookup) and not the whole property (StringProperty, CrmNumberProperty, etc).
Does that answer your question? Also, I recommend using the SDK assemblies and not the web references. They're much easier to use. The SDK download has a list of helper classes if you choose to use the web references, however. Search "Helper" in the SDK.