You are here: Catalog System > Catalog How To Code Samples > Creating Catalog Entries

Creating Catalog Entries

Key classes and files involved

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. Please see the code comments for details.

private void SampleEntryAdd()
{
int catalogId = 2;
// Get a CatalogDto object.
CatalogDto catalogDto = CatalogContext.Current.GetCatalogDto(catalogId, new CatalogResponseGroup(CatalogResponseGroup.ResponseGroup.CatalogInfo));
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)
{
// 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 = "PRODUCT_CODE";
newEntryRow.EndDate = DateTime.Now.AddYears(2).ToUniversalTime();
newEntryRow.IsActive = true;
newEntryRow.MetaClassId = 32;
newEntryRow.Name = "PRODUCT_NAME";
newEntryRow.StartDate = DateTime.UtcNow;
newEntryRow.TemplateName = "DigitalCameras"; // 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 = false;
newInventoryRow.AllowPreorder = false;
newInventoryRow.ApplicationId = AppContext.Current.ApplicationId;
newInventoryRow.BackorderAvailabilityDate = DateTime.UtcNow;
newInventoryRow.BackorderQuantity = 0;
newInventoryRow.InStockQuantity = Convert.ToDecimal(75);
newInventoryRow.InventoryStatus = 1;
newInventoryRow.PreorderAvailabilityDate = DateTime.UtcNow;
newInventoryRow.PreorderQuantity = 0;
newInventoryRow.ReorderMinQuantity = 15;
newInventoryRow.ReservedQuantity = 10;
newInventoryRow.SkuId = "mark_test7";
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 = 62;
newSeoRow.Description = "DESCRIPTION";
newSeoRow.LanguageCode = "en-us";
newSeoRow.Uri = "SEO-FRIENDLY-URL.aspx";
if (newSeoRow.RowState == DataRowState.Detached)
entry.CatalogItemSeo.AddCatalogItemSeoRow(newSeoRow);
// Save the entry.
CatalogContext.Current.SaveCatalogEntry(entry);
// Save the meta data attributes associated with the catalog entry.
MetaDataContext metaContext = new MetaDataContext();
MetaClass metaClass = MetaClass.Load(metaContext, "Publications");
MetaObject metaObj = MetaObject.NewObject(metaContext, entry.CatalogEntry[0].CatalogEntryId, metaClass.Id, "name");
MetaHelper.SetMetaFieldValue(metaContext, metaObj, "Title", new object[] { "New Book Title" });
MetaHelper.SetMetaFieldValue(metaContext, metaObj, "ID", new object[] { "New Id" });
MetaHelper.SetMetaFieldValue(metaContext, metaObj, "Description", new object[] { "New Description" });
MetaHelper.SetMetaFieldValue(metaContext, metaObj, "Theme", new object[] { "New Book Title" });
MetaHelper.SetMetaFieldValue(metaContext, metaObj, "Highlight", new object[] { false });
metaObj.AcceptChanges(metaContext);
// Set the entry node relation.
CatalogRelationDto relation = new CatalogRelationDto();
CatalogRelationDto.NodeEntryRelationRow nodeRelation = relation.NodeEntryRelation.NewNodeEntryRelationRow();
nodeRelation.CatalogId = 2;
nodeRelation.CatalogEntryId = entry.CatalogEntry[0].CatalogEntryId;
nodeRelation.CatalogNodeId = 62;
nodeRelation.SortOrder = 0;
if (nodeRelation.RowState == DataRowState.Detached)
relation.NodeEntryRelation.AddNodeEntryRelationRow(nodeRelation);
// Save the relation.
CatalogContext.Current.SaveCatalogRelationDto(relation);
}
}
}
Labels parameters
 
Labels

 


Last updated: 2011-05-17 | Copyright © EPiServer AB | Send feedback to us