QueryExpression在Dynamics CRM插件中没有结果

时间:2014-01-14 20:51:45

标签: dynamics-crm-2011 dynamics-crm query-expressions

我编写了以下函数来获取有关帐户或联系人的SharePointDocumentLocation记录。但是,即使我提供的id肯定有一个SPDL记录关联,但返回的EntityCollection的计数结果总是为0.为什么我的查询不返回SPDL记录?

internal static EntityCollection GetSPDocumentLocation(IOrganizationService service, Guid id)
{
    SharePointDocumentLocation spd = new SharePointDocumentLocation();
    QueryExpression query = new QueryExpression
    {
        EntityName = "sharepointdocumentlocation",
        ColumnSet = new ColumnSet("sharepointdocumentlocationid"),
        Criteria = new FilterExpression
        {
            Conditions = 
            { 
                new ConditionExpression
                {
                    AttributeName = "regardingobjectid",
                    Operator = ConditionOperator.Equal,
                    Values = { id }
                }
            }
        }
    };
    return service.RetrieveMultiple(query);
}

以下代码可以正常工作

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
using System.Net;
using Microsoft.Xrm.Sdk.Query;

namespace CRMConsoleTests
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientCredentials credentials = new ClientCredentials();
            credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            Uri orgUri = new Uri("http://localhost/CRMDEV2/XRMServices/2011/Organization.svc");
            Uri homeRealmUri = null;
            using (OrganizationServiceProxy service = new OrganizationServiceProxy(orgUri, homeRealmUri, credentials, null))
            {
                //ConditionExpression ce = new ConditionExpression("regardingobjectid", ConditionOperator.Equal, new Guid(""));
                QueryExpression qe = new QueryExpression("sharepointdocumentlocation");
                qe.ColumnSet = new ColumnSet(new String[] { "sharepointdocumentlocationid", "regardingobjectid" });
                //qe.Criteria.AddCondition(ce);

                EntityCollection result = service.RetrieveMultiple(qe);

                foreach (Entity entity in result.Entities)
                {
                    Console.WriteLine("Results for the first record: ");
                    SharePointDocumentLocation spd = entity.ToEntity<SharePointDocumentLocation>();
                    if (spd.RegardingObjectId != null)
                    {
                        Console.WriteLine("Id: " + spd.SharePointDocumentLocationId.ToString() + " with RoId: " + spd.RegardingObjectId.Id.ToString());
                    }
                }
                Console.ReadLine();
            }
        }
    }
}

它检索4条记录,当我调试上面的插件代码时,它会检索3条记录。

1 个答案:

答案 0 :(得分:3)

你的QueryExpression的一切看起来都不错,虽然我写得更简洁一些(如下所示):

var qe = new QueryExpression(SharePointDocumentLocation.EntityLogicalName){
    ColmnSet = new ColumnSet("sharepointdocumentlocationid"),
};
qe.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, id);

因为QueryExpression我没有看到任何错误导致我猜测。

  1. 您在IOrganizationService上使用模拟,模拟用户没有SharePointDocumentLocation的权限。你不会得到错误,你只是不会得到任何记录。

  2. 您传入的ID不正确。

  3. 我会移除Criteria并查看您收到的记录数量。如果你没有收回所有记录,你知道你的问题是猜测#1。

    如果您获得所有记录,请将regardingobjectid添加到ColumnSet并检索Criteria中没有任何QueryExpression的第一条记录,然后调用此方法传入您返回的regardingobject的ID。如果在添加regardingobjectid约束时没有收到任何内容,则其他错误。

    更新

    由于这是在删除插件时执行的,因此必须在插件触发之前执行级联删除。你可以try the Pre-Validation

    现在我想到了,它必须在验证阶段删除级联实体,因为如果其中一个无法删除,则无法删除实体本身。