none
各位达人,帮我分析下这代码 RRS feed

  • 问题

  • using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk.Query;
    using Microsoft.Crm.Sdk.Metadata;
    using Microsoft.Crm.SdkTypeProxy.Metadata;

    namespace UserEntity
    {
        public class Main:IPlugin
        {
            #region IPlugin 成员

            public void Execute(IPluginExecutionContext context)
            {
                CrmService service = GetCrmProxyUsingEndpointUrlInChildPipeline("http://de-c179-src/MSCrmServices/2007/CrmService.asmx", context);
                //userID
                WhoAmIRequest userRequest = new WhoAmIRequest();
                WhoAmIResponse user = (WhoAmIResponse)service.Execute(userRequest);
                Guid userid = user.UserId;

                // Create the column set object that indicates the properties to be retrieved.
                ColumnSet cols = new ColumnSet();
                cols.Attributes.Add("New_name");// = new string[] { "fullname"};

                // Create the ConditionExpression.
                ConditionExpression condition = new ConditionExpression();

                // Set the condition for the retrieval to be when the contact's address' city is Sammamish.
                condition.AttributeName = "New_ControlEntity";
                condition.Operator = ConditionOperator.Equal;
                condition.Values = new string[] { userid.ToString() };


                // Create the QueryExpression object.
                QueryExpression query = new QueryExpression();

                // Set the properties of the QueryExpression object.
                query.EntityName = "new_canuseentity";
                query.ColumnSet = cols;       

                // Retrieve the New_ControlEntity.
                BusinessEntityCollection newentity = service.RetrieveMultiple(query);

                foreach (BusinessEntity b in newentity.BusinessEntities)
                {
                  
                }
            }
            public static CrmService GetCrmProxyUsingEndpointUrlInChildPipeline(string endPointUrl, IPluginExecutionContext context)
            {
                CrmService childCrmService = new CrmService();
                childCrmService.Url = endPointUrl;
                childCrmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
                childCrmService.CrmAuthenticationTokenValue = new CrmAuthenticationToken();
                childCrmService.CrmAuthenticationTokenValue.AuthenticationType = AuthenticationType.AD;
                childCrmService.CrmAuthenticationTokenValue.OrganizationName = context.OrganizationName;
                childCrmService.CorrelationTokenValue = new CorrelationToken(context.CorrelationId, context.Depth, context.CorrelationUpdatedTime);
                return childCrmService;
            }
            #endregion
        }
    }
    我现在需要在Plugin中查询一个自定义的信息,因为我注册时是publish事件,所以只能用webservice,请问我查询那一步 foreach里面怎么获得属性的信息,系统实体我会!


    梅小虎
    2009年5月15日 3:43

答案

  • BusinessEntities 是 DynamicEntity, 所以你应该把他改为以下:

    foreach (DynamicEntity b in newentity.BusinessEntities)

           //((字段Property)b["字段"]).Name 或 ((字段Property)b["字段"]).Value;
           // StringProperty 例子:
           ((StringProperty)b["new_name"]).Value;
    }

    希望帮到你。
    Darren Liu | 刘嘉鸿 | MS CRM MVP | English Blog: http://msdynamicscrm-e.blogspot.com | Chinese Blog: http://liudarren.spaces.live.com
    2009年5月18日 21:02
    版主