TransWikia.com

Add category into a catalog programatically

Sitecore Asked by Vlad Iobagiu on December 9, 2021

How can I add a category programatically ?

In postman are examples how to add category into a catalog or how to add a subcategory.

When I am calling AddCategory I am receiving next exception:

{
    "@odata.context": "https://localhost:5000/Api/$metadata#Commands/$entity",
    "@odata.type": "#Sitecore.Commerce.EntityViews.Commands.DoActionCommand",
    "Id": "cd8621751643493f9a0a860db2e98a85",
    "ResponseCode": "Error",
    "Messages": [
        {
            "MessageDate": "2018-05-04T08:29:55.7725232Z",
            "Code": "ValidationError",
            "Text": "Action has failed due to Optimistic Concurrency failure.",
            "CommerceTermKey": "OptimisticConcurrencyFailure"
        }
    ],
    "Models": [],
    "ActionUrl": null
}

I am doing a PUT call to {{ServiceHost}}/{{ShopsApi}}/DoUxAction()

This are my headers:

Content-Type:application/json
ShopName:{{ShopName}}
ShopperId:{{ShopperId}}
Language:{{Language}}
Currency:{{Currency}}
Environment:{{Environment}}
GeoLocation:{{GeoLocation}}
CustomerId:{{CustomerId}}
Authorization:{{SitecoreIdToken}}

This is my body :

{
    "@odata.context": "http://localhost:5000/Api/$metadata#Sitecore.Commerce.EntityViews.EntityView",
    "@odata.type": "#Sitecore.Commerce.EntityViews.EntityView",
    "Name": "Details",
    "Policies": [],
    "DisplayName": "Details",
    "EntityId": "{{CatalogId}}",
    "Action": "AddCategory",
    "ItemId": "",
    "Properties": [
        {
            "Name": "Version",
            "Policies": [],
            "DisplayName": "Version",
            "Value": "2",
            "IsHidden": true,
            "OriginalType": "System.Int32",
            "IsReadOnly": true,
            "UiType": "",
            "IsRequired": true
        },
        {
            "Name": "Name",
            "Policies": [],
            "DisplayName": "Name",
            "Value": "PostmanCategory",
            "IsHidden": false,
            "OriginalType": "System.String",
            "IsReadOnly": false,
            "UiType": "",
            "IsRequired": true
        },
        {
            "Name": "DisplayName",
            "Policies": [],
            "DisplayName": "Display Name",
            "Value": "Postman Category",
            "IsHidden": false,
            "OriginalType": "System.String",
            "IsReadOnly": false,
            "UiType": "",
            "IsRequired": true
        },
        {
            "Name": "Description",
            "Policies": [],
            "DisplayName": "Description",
            "Value": "Sample category created through Postman",
            "IsHidden": false,
            "OriginalType": "System.String",
            "IsReadOnly": false,
            "UiType": "",
            "IsRequired": false
        },
        {
            "Name": "IsSearchable",
            "Policies": [],
            "DisplayName": "Is Searchable",
            "Value": "false",
            "IsHidden": false,
            "OriginalType": "System.Boolean",
            "IsReadOnly": false,
            "UiType": "",
            "IsRequired": false
        }
    ],
    "ChildViews": [],
    "DisplayRank": 500,
    "UiHint": "Flat",
    "Icon": "dashboard"
}

enter image description here

One Answer

I got this error because of Version parameter which I add it into Body call.

{
            "Name": "Version",
            "Policies": [],
            "DisplayName": "Version",
            "Value": "2",
            "IsHidden": true,
            "OriginalType": "System.Int32",
            "IsReadOnly": true,
            "UiType": "",
            "IsRequired": true
        }

You need to pass the right version into Body, is not documented why we need this parameter.

How to get the right version ?

You need to get all catalogs using a rest call.

enter image description here

You have the Version value in the json result for every catalog :

        {
            "CompositeKey": null,
            "DateCreated": "2018-05-06T17:41:38.3000725Z",
            "DateUpdated": "2018-05-06T18:57:14.8980297Z",
            "DisplayName": "Postman Catalog11111",
            "FriendlyId": "PostmanCatalog11111",
            "Id": "Entity-Catalog-PostmanCatalog11111",
            "Version": 6,
            "IsPersisted": true,
            "Name": "PostmanCatalog11111",
            "Policies": [],
            "SitecoreId": "f9505e96-72de-c413-50aa-b00b13277142",
            "ParentCatalogList": null,
            "ParentCategoryList": null,
            "ChildrenCategoryList": "802660cb-22d6-f3d7-2e7e-cd4284ccd0c8|a0ad547b-7387-59bf-60f5-1e8574fbe08b|e6e1b61a-4ecc-28b7-97ba-0e541d856608|72ddf689-9d07-580a-1477-84db68117cd9|b18f0e40-5eb4-321f-f5fc-4f0e71193c55",
            "ChildrenSellableItemList": null,
            "ItemVariations": null,
            "PriceBookName": "",
            "PromotionBookName": "",
            "DefaultInventorySetName": "",
            "Currency": null
        }

Answered by Vlad Iobagiu on December 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP