2

我正在尝试从 CRM 2013 中检索所有业务部门。

尝试了以下查询

        var query = _serviceContext.BusinessUnitSet
                            .Where(b => b.EntityState == 0)
                            .Select(x => new
                                        {
                                            Name = x.Name,
                                            Id = x.Id
                                        }
                                    )
                            .ToList();

使用此查询我收到一个错误,只是说明:

{System.ServiceModel.FaultCode} {attributeName} {System.Collections.Generic.SynchronizedReadOnlyCollection<System.ServiceModel.FaultReasonText>}

在搜索该主题时,我发现了有关如何检索单个业务单位的信息(这似乎与检索“正常”实体不同),但没有找到如何全部获取它们的信息(链接)。

任何有关如何检索所有业务部门的帮助将不胜感激。

4

2 回答 2

3

尝试使用 QueryExpression

    public class BusinessUnit
    {
        public Guid Id { get; set; }
        public String Name { get; set; }
    }


    public void GetAllBusinessUnits(Action<QueryExpression> queryModifier = null)
    {

        foreach (BusinessUnit m in RetrieveAllBusinessUnit(this.Service, 1000, queryModifier))
        {

            //Console.WriteLine(m.Name);
        }
    }


    public static IEnumerable<BusinessUnit> RetrieveAllBusinessUnit(IOrganizationService service, int count = 1000, Action<QueryExpression> queryModifier = null)
    {

        QueryExpression query = new QueryExpression("businessunit")
        {
            ColumnSet = new ColumnSet("businessunitid", "name"),

            PageInfo = new PagingInfo()
            {
                Count = count,
                PageNumber = 1,
                PagingCookie = null,
            }
        };

        if (queryModifier != null)
        {
            queryModifier(query);
        }

        while (true)
        {
            EntityCollection results = service.RetrieveMultiple(query);

            foreach (Entity e in results.Entities)
            {
                yield return new BusinessUnit()
                {
                    Id = e.GetAttributeValue<Guid>("businessunitid"),
                    Name = e.GetAttributeValue<String>("name")
                };
            }

            if (results.MoreRecords)
            {
                query.PageInfo.PageNumber++;
                query.PageInfo.PagingCookie = results.PagingCookie;
            }
            else
            {
                yield break;
            }
        }
    }
于 2013-12-12T01:26:41.327 回答
2

我假设您想从系统中获取所有活动的业务单位。所以你必须使用IsDisabled属性来获取它们。您使用的EntityState属性用于在上下文中跟踪实体状态,而不是指示 CRM 中实体的状态。有关 BU 实体的更多信息,请参阅BusinessUnit实体。

于 2013-12-13T09:51:52.820 回答