You are here: Tutorials > Catalog System > Creating Catalog Entries

Creating Catalog Entries

Key classes and files involved

Before you use the sample code:

  1. connectionStrings.config and Web.config should be in the root level of the project, not in the Configs folder.
    1. Web.config should be renamed to App.config
  2. Check the Application table of your database, the value in ecf.app.config, and the application name part of your connection string. Make sure the values all match.
  3. Make sure you set the correct catalogId.
  4. The following line tries to load the MetaClass based on the name, so make sure the MetaClass exists by checking the MetaClass table in your database:
    MetaClass metaClass = MetaClass.Load(metaContext, "Publications");
  5. Make sure the catalogId in nodeRelation.CatalogId = 2; matches the catalogId you use in the beginning.

How it works

Below is sample code that shows the use of Catalog System API data transfer objects (DTOs) being used to create a catalog entry in EPiServer Commerce. The CatalogContext is the main API hook to the EPiServer Commerce Catalog System. See the code comments for details.

            MetaDataContext metaContext = new MetaDataContext();
            
            int catalogId = 0; 
            int entryid = 0; 

            CatalogDto catalogDto1 = CatalogContext.Current.GetCatalogDto(-1, new CatalogResponseGroup(CatalogResponseGroup.ResponseGroup.CatalogInfo));

            CatalogDto.CatalogRow newCatalogRow = catalogDto1.Catalog.NewCatalogRow();
            newCatalogRow.Name = "TESTCATALOG";
            newCatalogRow.StartDate = DateTime.Now;
            newCatalogRow.EndDate = DateTime.Now.AddYears(2);
            newCatalogRow.IsActive = true;
            newCatalogRow.SortOrder = 0;
            newCatalogRow.ApplicationId = AppContext.Current.ApplicationId;
            newCatalogRow.Created = DateTime.Now;
            newCatalogRow.Modified = DateTime.Now;

            // hardcoded for test:
            newCatalogRow.IsPrimary = false;
            newCatalogRow.DefaultCurrency = "eur";
            newCatalogRow.WeightBase = "kgs";
            newCatalogRow.DefaultLanguage = "en-us";
            catalogDto1.Catalog.AddCatalogRow(newCatalogRow);
            CatalogContext.Current.SaveCatalog(catalogDto1);
            catalogId = newCatalogRow.CatalogId; // Get the new ID, used below

            CatalogNodeDto catalogNodeDto = CatalogContext.Current.GetCatalogNodeDto("TESTCATALOGNODE", new CatalogNodeResponseGroup(CatalogNodeResponseGroup.ResponseGroup.CatalogNodeInfo));
            if (catalogNodeDto.CatalogNode.Count == 0)
            {
                Mediachase.MetaDataPlus.Configurator.MetaClass metaClass = Mediachase.MetaDataPlus.Configurator.MetaClass.Load(metaContext, "CatalogNodeEx");
                CatalogNodeDto.CatalogNodeRow catalogNodeRow = catalogNodeDto.CatalogNode.NewCatalogNodeRow();
                catalogNodeRow.CatalogId = catalogId;
                catalogNodeRow.StartDate = DateTime.Now;
                catalogNodeRow.EndDate = DateTime.Now.AddYears(2);
                catalogNodeRow.Name = "TEST CATALOG NODE";
                catalogNodeRow.Code = "TESTCATALOGNODE";
                catalogNodeRow.ParentNodeId = 0;
                catalogNodeRow.IsActive = true;
                catalogNodeRow.SortOrder = 0;
                catalogNodeRow.ApplicationId = AppContext.Current.ApplicationId;
                // hardcoded for now
                catalogNodeRow.TemplateName = "NodeEntriesTemplate";
                catalogNodeRow.MetaClassId = metaClass.Id;
                catalogNodeDto.CatalogNode.AddCatalogNodeRow(catalogNodeRow);
                CatalogContext.Current.SaveCatalogNode(catalogNodeDto);
            }

            CatalogDto catalogDto = CatalogContext.Current.GetCatalogDto(catalogId, new CatalogResponseGroup(CatalogResponseGroup.ResponseGroup.CatalogInfo));
            CatalogNodeDto catalogNodeDto1 = CatalogContext.Current.GetCatalogNodeDto("TESTCATALOGNODE", new CatalogNodeResponseGroup(CatalogNodeResponseGroup.ResponseGroup.CatalogNodeInfo));


            if (catalogDto.Catalog.Count > 0)
            {
                // Get a CatalogEntryDto object.
                CatalogEntryDto entry = CatalogContext.Current.GetCatalogEntryDto("PRODUCT_CODE",
                  new CatalogEntryResponseGroup(CatalogEntryResponseGroup.ResponseGroup.CatalogEntryInfo));

                if (entry.CatalogEntry.Count == 0)
                {

                    MetaClass metaClass = MetaClass.Load(metaContext, "DigitalCamera");
                    // Get a new entry row for your catalog entry.
                    CatalogEntryDto.CatalogEntryRow newEntryRow = entry.CatalogEntry.NewCatalogEntryRow();

                    // Set entry properties.
                    newEntryRow.ApplicationId = AppContext.Current.ApplicationId;
                    newEntryRow.CatalogId = catalogDto.Catalog[0].CatalogId; 
                    newEntryRow.ClassTypeId = "Variation";
                    newEntryRow.Code = "PRODUCTCODE";
                    newEntryRow.EndDate = DateTime.Now.AddYears(2).ToUniversalTime();
                    newEntryRow.IsActive = true;
                    newEntryRow.MetaClassId = metaClass.Id;
                    newEntryRow.Name = "PRODUCTNAME";
                    newEntryRow.StartDate = DateTime.UtcNow;
                    newEntryRow.TemplateName = "DigitalCameraTemplate"; // system-defined template of type 'entry'
                    newEntryRow.SetSerializedDataNull();
                    if (newEntryRow.RowState == DataRowState.Detached)
                        entry.CatalogEntry.AddCatalogEntryRow(newEntryRow);

                    // Set variation properties.
                    CatalogEntryDto.VariationRow newVariationRow = entry.Variation.NewVariationRow();
                    newVariationRow.ListPrice = Convert.ToDecimal(1000.00);
                    newVariationRow.MaxQuantity = 50;
                    newVariationRow.SetMerchantIdNull();
                    newVariationRow.MinQuantity = 5;
                    newVariationRow.PackageId = 0;
                    newVariationRow.TaxCategoryId = 0;
                    newVariationRow.TrackInventory = true;
                    newVariationRow.WarehouseId = 0;
                    newVariationRow.Weight = Convert.ToDouble(4);


                    newVariationRow.CatalogEntryId = entry.CatalogEntry[0].CatalogEntryId;
                    if (newVariationRow.RowState == DataRowState.Detached)
                        entry.Variation.AddVariationRow(newVariationRow);

                    // Set inventory properties.
                    CatalogEntryDto.InventoryRow newInventoryRow = entry.Inventory.NewInventoryRow();
                    newInventoryRow.AllowBackorder = true;
                    newInventoryRow.AllowPreorder = true;
                    newInventoryRow.ApplicationId = AppContext.Current.ApplicationId;
                    newInventoryRow.BackorderAvailabilityDate = DateTime.UtcNow;
                    newInventoryRow.BackorderQuantity = 10;
                    newInventoryRow.InStockQuantity = Convert.ToDecimal(75);
                    newInventoryRow.InventoryStatus = 1;
                    newInventoryRow.PreorderAvailabilityDate = DateTime.UtcNow;
                    newInventoryRow.PreorderQuantity = 10;
                    newInventoryRow.ReorderMinQuantity = 10;
                    newInventoryRow.ReservedQuantity = 10;
                    newInventoryRow.SkuId = newEntryRow.Code;
                    if (newInventoryRow.RowState == DataRowState.Detached)
                        entry.Inventory.AddInventoryRow(newInventoryRow);

                    // Set SEO properties.
                    CatalogEntryDto.CatalogItemSeoRow newSeoRow = entry.CatalogItemSeo.NewCatalogItemSeoRow();
                    newSeoRow.ApplicationId = AppContext.Current.ApplicationId;
                    newSeoRow.CatalogEntryId = entry.CatalogEntry[0].CatalogEntryId;
                    newSeoRow.CatalogNodeId = 66;
                    newSeoRow.Description = "DESCRIPTION";
                    newSeoRow.LanguageCode = "en-us";
                    newSeoRow.Uri = "SEO-FRIENDLY-URL" + ".aspx";
                    if (newSeoRow.RowState == DataRowState.Detached)
                        entry.CatalogItemSeo.AddCatalogItemSeoRow(newSeoRow);

                    CatalogContext.Current.SaveCatalogEntry(entry);

                    // Save the meta data attributes associated with the catalog entry.
                    CatalogContext.MetaDataContext.UseCurrentUICulture = false;
                    CatalogContext.MetaDataContext.Language = "en-us";
                    MetaObjectSerialized serialized = new Mediachase.Commerce.Storage.MetaObjectSerialized();
                    MetaObject metaObj = MetaObject.NewObject(metaContext, entry.CatalogEntry[0].CatalogEntryId, metaClass.Id, "admin");
                    MetaHelper.SetMetaFieldValue(metaContext, metaObj, "DisplayName", new object[] { "NEW CAMERA" });
                    MetaHelper.SetMetaFieldValue(metaContext, metaObj, "Description", new object[] { "Testing Description" });
                    metaObj.AcceptChanges(metaContext);

                    serialized.AddMetaObject("en-us", metaObj);
                    newEntryRow.SerializedData = serialized.BinaryValue; 
                    CatalogContext.MetaDataContext.UseCurrentUICulture = true;
                    CatalogContext.Current.SaveCatalogEntry(entry);

                    // Set the entry node relation.
                    CatalogRelationDto relation = new CatalogRelationDto();
                    CatalogRelationDto.NodeEntryRelationRow nodeRelation = relation.NodeEntryRelation.NewNodeEntryRelationRow();
                    nodeRelation.CatalogId = catalogId;
                    nodeRelation.CatalogEntryId = entry.CatalogEntry[0].CatalogEntryId;
                    entryid = entry.CatalogEntry[0].CatalogEntryId;

                    nodeRelation.CatalogNodeId = catalogNodeDto1.CatalogNode[0].CatalogNodeId;
                    nodeRelation.SortOrder = 0;
                    if (nodeRelation.RowState == DataRowState.Detached)
                        relation.NodeEntryRelation.AddNodeEntryRelationRow(nodeRelation);

                    CatalogContext.Current.SaveCatalogRelationDto(relation);

                }
            }

Configuring CatalogContext for Importing Catalog Data

Configuration

The CatalogContext gets its configuration from ecf.catalog.config. In there is:

<Connection connectionStringName="EcfSqlConnection"/>

This defines the use of the EcfSqlConnection connection string.

 


Version: EPiServer Commerce 1 R2 SP2| Last updated: 2012-06-29 | Copyright © EPiServer AB | Send feedback to us