1

How to find entity by sitecoreId ? When I query for all categories, return model looks like this

{
            "CompositeKey": null,
            "CreatedBy": "sitecore\\Admin",
            "UpdatedBy": "sitecore\\Admin",
            "DateCreated": "2018-09-25T09:43:47.6186016Z",
            "DateUpdated": "2018-09-25T09:44:06.8559231Z",
            "DisplayName": "X",
            "FriendlyId": "X-X",
            "Id": "Entity-Category-X-X",
            "Version": 3,
            "EntityVersion": 1,
            "Published": false,
            "IsPersisted": true,
            "Name": "X",
            "Policies": [],
            "SitecoreId": "fa51a9f8-06e3-3400-cf6a-cb8e8dc1ce16",
            "CatalogToEntityList": "2ad5c290-2725-64c8-0658-089ce8ceaef9",
            "ParentCatalogList": "2ad5c290-2725-64c8-0658-089ce8ceaef9",
            "ParentCategoryList": null,
            "ChildrenCategoryList": "fb073ff0-bb9a-2d50-38fe-26d8d39da8b4",
            "ChildrenSellableItemList": null,
            "ItemVariations": null,
            "Description": ""
        }

Now i want to load ChildrenCategoryList or any other list by sitecoreId, however i cannot find any service/action/pipeline for that.

Category pluginCategory = await _getCategoryCommand.Process(context, category.Id); where id is Id = Name.ToCategoryId(ParentCatalogId); this is by name, but i want to do the same by sitecoreId

4
  • where are you trying to use this code to get entity by sitecore id? Commented Sep 26, 2018 at 10:46
  • in commerce engine's plugin Commented Sep 26, 2018 at 10:52
  • why do you need sitecore id in commerce engine ? Commented Sep 26, 2018 at 10:59
  • I need to get all children of category and 'ChildrenCategoryList' is represented by sitecoreId ("Guid") Commented Sep 26, 2018 at 11:08

1 Answer 1

1

I'm not sure that you need to get Sitecore Item to load subcategoroies.

But you may use the concept of Commerce Lists to load children categories. For Model you've provided in your question you may use list CategoryToCategory that will load all subcategories for your category. You can try to use Postman for your model. API URL will look like this: {{ServiceHost}}/{{ShopsApi}}/GetList(id='CategoryToCategory-{FriendlyId}',type='Sitecore.Commerce.Plugin.Catalog.Category, Sitecore.Commerce.Plugin.Catalog',skip=0,take=100)?$expand=Items in your example {FriendlyId} equals to X-X.

If you need to call the same command from code, then you can use the following snippet:

await findEntitiesInListCommand.Process(commerceContext: commerceContext, type: typeof(Sitecore.Commerce.Plugin.Catalog.Category).ToString(), listName: "CategoryToCategory", skip: 0, take: 100);

You may then use the code you've provided in your question to load additional information: Category pluginCategory = await _getCategoryCommand.Process(context, category.Id); where id is Id = Name.ToCategoryId(ParentCatalogId);

Update on CategoryToCategory string

I haven't found any documentation on this but explored it while looking at SitecoreCommerce_SharedEnvironments database. I was looking through default Commerce pipelines and found following block in InitializeRelationshipDefinitionsBlock:

public override async Task<string> Run(string arg, CommercePipelineExecutionContext context)
{
  InitializeRelationshipDefinitionsBlock definitionsBlock = this;
  string relationship1 = await definitionsBlock.CreateRelationship("CatalogToCategory", "Represents a relationship between a catalog and a category", typeof (Sitecore.Commerce.Plugin.Catalog.Catalog), typeof (Category), context, false, false);
  string relationship2 = await definitionsBlock.CreateRelationship("CatalogToSellableItem", "Represents a relationship between a catalog and a sellable item", typeof (Sitecore.Commerce.Plugin.Catalog.Catalog), typeof (SellableItem), context, false, false);
  string relationship3 = await definitionsBlock.CreateRelationship("CategoryToCategory", "Represents a relationship between a category and another category", typeof (Category), typeof (Category), context, false, false);
  string relationship4 = await definitionsBlock.CreateRelationship("CategoryToSellableItem", "Represents a relationship between a category and a sellable item", typeof (Category), typeof (SellableItem), context, false, false);
  string relationship5 = await definitionsBlock.CreateRelationship("PromotionBookToCatalog", "Represents a relationship between a promotion book and a catalog", typeof (PromotionBook), typeof (Sitecore.Commerce.Plugin.Catalog.Catalog), context, false, false);
  string relationship6 = await definitionsBlock.CreateRelationship("PriceBookToCatalog", "Represents a relationship between a price book and a catalog", typeof (PriceBook), typeof (Sitecore.Commerce.Plugin.Catalog.Catalog), context, false, false);
  return arg;
}
3
  • Can you explain what is "CategoryToCategory ". It seems to be some magic value. How I am suppose to be know of such variable? Is there any documentation/examples? Commented Oct 3, 2018 at 13:28
  • @user1075940, updated my answer. Found it while exploring Commerce database.
    – vzaverugo
    Commented Oct 3, 2018 at 15:05
  • I don't understand how this is the approved answer, as it does not at all address the OP's question. When you query a sellable item in commerce, the ParentCategoryList property has a guid value, which is the Sitecore Id for the Category. The OP (and I) want to know how to get a reference to the Category itself using that guid.
    – Greg M.
    Commented Mar 1, 2019 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.