0

I am trying to query product related information in a Dynamics 365 tenant (Version 9.2.22101.170) and with Version 9.0.2.46 of the Microsoft.CrmSdk. Mostly I am interested in querying products by the product number to retrieve price information, but later on, I would introduce more parameters. The below is one of the many methods I've tried (I am aware I am projecting only the name for the time being, eventually I would require price information etc):

var cols = new ColumnSet(new String[] { "name" });
QueryByAttribute query = new QueryByAttribute("product");
query.ColumnSet = cols;

query.Attributes.AddRange("productnumber");
query.Values.AddRange("100002");

var results = service.RetrieveMultiple(query);

if (results != null)
{
    var entities = results.Entities.ToList();

    if (entities != null)
    {
        var productEnt = (Product)entities.FirstOrDefault();

        Console.WriteLine(productEnt.Name);     
    }
}

This is the error message returned, on the RetrieveMultiple call:

The entity with a name = 'product' with namemapping = 'Logical' was not found in the MetadataCache. MetadataCacheDetails: ProviderType=Dynamic, StandardCache=True, IsLoadedInStagedContext = False, Timestamp=8343791, MinActiveRowVersion=8343791

The same message is returned when calling any other method. It's clear that the issue is not the query, or the columns being returned but the "product".

Sure enough, I am using the below method to get the list of entity names, and the word "Product" does not show up. I think this explains the error message.

public static EntityMetadata[] GetEntities(IOrganizationService organizationService)
    {
        Dictionary<string, string> attributesData = new Dictionary<string, string>();
        RetrieveAllEntitiesRequest metaDataRequest = new RetrieveAllEntitiesRequest();
        RetrieveAllEntitiesResponse metaDataResponse = new RetrieveAllEntitiesResponse();
        metaDataRequest.EntityFilters = EntityFilters.Entity;

        // Execute the request.

        metaDataResponse = (RetrieveAllEntitiesResponse)organizationService.Execute(metaDataRequest);

        var entities = metaDataResponse.EntityMetadata;

        return entities;
    }

Is this a permission issue? Do I need to do some extra loading prior to the query? How do you query product/pricing related information in a Dynamics 365 tenant?

I tried searching for related information online, but I was surprised to practically find almost nothing related to Products.

Jurgen Cuschieri
  • 658
  • 14
  • 33

1 Answers1

1

you are using QueryByAttribute rather you should be using QueryExpression Try below query and it should help.

// Define Condition Values
    var query_productnumber = "100002";
    
    // Instantiate QueryExpression query
    var query = new QueryExpression("product");
    query.Distinct = true;
    
    // Add columns to query.ColumnSet
    query.ColumnSet.AddColumns("productid", "name");
    
    // Define filter query.Criteria
    query.Criteria.AddCondition("productnumber", ConditionOperator.Equal, query_productnumber);
var results = service.RetrieveMultiple(query);

if (results != null)
{
    var entities = results.Entities.ToList();

    if (entities != null)
    {
        var productEnt = (Product)entities.FirstOrDefault();

        Console.WriteLine(productEnt.Name);     
    }
}
AnkUser
  • 5,421
  • 2
  • 9
  • 25
  • Thank you for taking your time to reply! I tried this approach, but the error is exactly as reported in the initial question. Do you know if there is a way to turn on querying for some entities? Why doesn't "product" show up in the list of entity names as returned by GetEntities (see snippet in initial question)? Any help would be appreciated :) – Jurgen Cuschieri Nov 07 '22 at 16:43