通过变量检索 MS CRM 字段的值

发布于 2024-08-12 14:51:22 字数 734 浏览 5 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红焚 2024-08-19 14:51:22

不使用 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(长度):

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

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

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

请注意,如果使用此方法,您将获得值(字符串、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)...

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

  }
}

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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文