Question

I wrote the following function to get the SharePointDocumentLocation records regarding an account or contact. However, even though I provide an id which most definitely has got a SPDL record associated the result of a count on the EntityCollection that is returned is alway 0. Why does my query not return SPDL records?

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);
}

The following code does work

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();
            }
        }
    }
}

It retrieves 4 records, and when I debug the plugincode above it retrieves 3 records.

Was it helpful?

Solution

Everything looks good with your QueryExpression, although I'd write it a little more concise (something like this):

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

Because I don't see anything wrong with the QueryExpression that leads me with two guesses.

  1. You're using impersonation on the IOrganizationService and the impersonated user doesn't have rights to the SharePointDocumentLocation. You won't get an error, you just won't get any records returned.

  2. The id you're passing in is incorrect.

I'd remove the Criteria and see how many records you get back. If you don't get all of the records back, you know your issue is with guess #1.

If you get all records, add the regardingobjectid to the ColumnSet and retrieve the first record without any Criteria in the QueryExpression, then call this method passing in the id of the regardingobject you returned. If nothing is received when adding the regardingobjectid constraint, then something else is wrong.

Update

Since this is executing within the delete of the plugin, it must be performing its cascade deletes before your plugin is firing. You can try the Pre-Validation.

Now that I think of it, it must perform the deletion of the cascading entities in the Validation stage, because if one of them is unable to be deleted, the entity itself can't be deleted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top