4

我在尝试从 Dynamics CRM 2011 检索系统用户信息时收到错误消息。以下代码有效:

public List<CrmUser> GetAllCrmUsers()
{
    List<CrmUser> CrmUsers = new List<CrmUser>();
    using (CrmSdk.OrganizationServiceClient myCrm = new CrmSdk.OrganizationServiceClient("CustomBinding_IOrganizationService1"))
    {
        try
        {

            // this will need to be changed... the address to a key in the app.config and the credentials will need to be whatever is correct for the
            // end server to hit the CRM WCF service
            myCrm.Endpoint.Address = new System.ServiceModel.EndpointAddress("https://devcrm.removed/XRMServices/2011/Organization.svc");
            myCrm.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;


            CrmSdk.ColumnSet colsPrincipal = new CrmSdk.ColumnSet();

            colsPrincipal.Columns = new string[] { "lastname", "firstname", "domainname", "systemuserid" };

            CrmSdk.QueryExpression queryPrincipal = new CrmSdk.QueryExpression();
            queryPrincipal.EntityName = "systemuser";
            queryPrincipal.ColumnSet = colsPrincipal;

            CrmSdk.EntityCollection myAccounts = myCrm.RetrieveMultiple(queryPrincipal);

            foreach (CrmSdk.Entity myEntity in myAccounts.Entities)
            {
                //create new crm users and add it to the list
                CrmUser thisOne = new CrmUser();

                thisOne.firstName = myEntity.Attributes[0].Value.ToString();
                thisOne.lastName = myEntity.Attributes[1].Value.ToString();
                thisOne.userId = myEntity.Attributes[2].Value.ToString();
                thisOne.userGuid = myEntity.Attributes[3].Value.ToString();

                CrmUsers.Add(thisOne);

            }


        }

        catch (Exception ex)
        {
            CrmUser thisOne = new CrmUser();
            thisOne.firstName = "Crap there was an error";
            thisOne.lastName = ex.ToString();
            CrmUsers.Add(thisOne);
        }
    }
    return CrmUsers;
}

但是,如果我在调用服务时尝试将“businessunitid”添加到 ColumnSet,我会收到一条错误消息:

“第 1 行位置 1879 出错。元素 \2004/07/System.Collections.Generic:value\' 包含来自映射到名称 \'/xrm/2011/Contracts:OptionSetValue\' 的类型的数据。反序列化程序没有知道映射到此名称的任何类型。考虑使用 DataContractResolver 或将与 \'OptionSetValue\' 对应的类型添加到已知类型列表 - 例如,通过使用 KnownTypeAttribute 属性或将其添加到已知类型列表传递给 DataContractSerializer。\'"

这个错误是因为返回的数据根据​​元数据信息是“查找”类型。我尝试[KnownType(typeof(OptionSetValue))][Data Contract]标签下添加无济于事,我已经在谷歌搜索和 Binging(?)这两天了,所以如果它已经得到回答,我很抱歉。

4

2 回答 2

0

只是杠杆Microsoft.Xrm.Client。它为您提供CrmConnection了一种连接到系统的简化方式。

我还建议(但可能更多的是品味/需求问题)始终通读属性以GetAttributeValue<>获得更易读的代码。

完整的工作样本,包括 BU id:

// helper class
public static class CommonCrm
{        
    public static readonly CrmConnection crmConnection = null;
    public static readonly OrganizationService crmService = null;
    public static readonly CrmOrganizationServiceContext crmContext = null;

    static CommonCrm()
    {
        try
        {
            CommonCrm.crmConnection = new CrmConnection("Crm");

// "Crm" is a connection string which goes like this in Web.config:
//<connectionStrings>
//    <add name="Crm" connectionString="Url=http://orgUrl; Domain=X; Username=Y; Password=Z;" />
//</connectionStrings>

            CommonCrm.crmService = new OrganizationService(crmConnection);
            CommonCrm.crmContext = new CrmOrganizationServiceContext(crmService);
        }
        catch (Exception ex)
        {
            //Log exception (code removed)
            throw;
        }
    }
}

public class CrmUser
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string userId { get; set; }
    public Guid userGuid { get; set; }
    public Guid buId { get; set; }

    public static List<CrmUser> GetAllCrmUsers()
    {
        List<CrmUser> CrmUsers = new List<CrmUser>();
        try
        {
            ColumnSet colsPrincipal = new ColumnSet("lastname", "firstname", "domainname", "systemuserid", "businessunitid");
            QueryExpression queryPrincipal = new QueryExpression();
            queryPrincipal.EntityName = "systemuser";
            queryPrincipal.ColumnSet = colsPrincipal;

            var myAccounts = CommonCrm.crmContext.RetrieveMultiple(queryPrincipal);

            foreach (var myEntity in myAccounts.Entities)
            {
                //create new crm users and add it to the list
                CrmUser thisOne = new CrmUser();

                thisOne.firstName = myEntity.GetAttributeValue<string>("firstname");
                thisOne.lastName = myEntity.GetAttributeValue<string>("name");
                thisOne.userId = myEntity.GetAttributeValue<string>("domainname");
                thisOne.userGuid = myEntity.GetAttributeValue<Guid>("systemuserid");
                thisOne.buId = myEntity.GetAttributeValue<EntityReference>("businessunitid").Id;
                // and so on and so forth...         

                CrmUsers.Add(thisOne);
            }
        }
        catch (Exception ex)
        {
            CrmUser thisOne = new CrmUser();
            thisOne.firstName = "Crap there was an error";
            thisOne.lastName = ex.ToString();
            CrmUsers.Add(thisOne);
        }

        return CrmUsers;
    }
}
于 2013-06-17T12:31:17.057 回答
0

我终于找到了。您必须打开自动生成的 Reference.cs 代码。搜索实体类:

public partial class Entity : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

转到它上面的行并添加已知类型的东西,以便实体可以对其进行反序列化。

[System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))]
public partial class Entity : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
于 2013-10-17T20:42:03.243 回答