Creating products

To create a product, you must perform the following:

  1. Get the manager.
    Get an instance of the CatalogManager object.
  2. Check if the product already exists.
    To check if the product already exists, make a query for a product with the specified name and check if it returns null.
  3. Get the specified product type.
    Get an instance of the product type that the product will be associated with. Note that you can’t create product without specifying a product type.
  4. Create a new product.
    To create a new product type, call the CreateProduct method of the manager and pass the CLR product type as an argument.
  5. Set the properties of the product.
    Make sure to set at least the following properties:
    • Title
    • IsActive
    • Sku
    • UrlName
    • ClrType
  6. Set the status of the product.
    Set the Status property of the product to GenericContent.Model.ContentLifecycleStatus.Master. This is the master version of the product and will be used when modifying it. In the next steps, you will publish the product, thus creating a live version. For more information about content life cycle, read this article.
  7. Recompile the URLs of the product.
    To recompile the URLs of the product, call the RecompileItemUrls and pass the product as an argument.
  8. Save the changes.
    Save the changes to the catalog manager.
  9. Publish the product.
    To publish the product, call the MessageWorkflow method of the WorkflowManager class and pass the required arguments as shown in the example below. This will create a live version of the product that will be used in the frontend.

Here is a code example:

private static Guid CreateProductInternal(string productTypeName, string title, string description, string sku, double? weight, bool isShippable, decimal price, DateTime saleEndDate, DateTime saleStartDate, decimal salePrice)
{
 
    CatalogManager manager = CatalogManager.GetManager();
    if (manager.GetProducts().Where(t => t.Title == title).SingleOrDefault() != null)
    {
        return Guid.Empty;     // Product already exists
    }
 
    ProductType productType = manager.GetProductTypes().Where(t => t.Title == productTypeName).SingleOrDefault();
    if (productType == null)
    {
        return Guid.Empty;     // Product Type does not exist
    }
 
    Product product = manager.CreateProduct(productType.ClrType);
    product.ClrType = productType.ClrType;
 
    product.Title = title;
    product.AssociateBuyerWithRole = Guid.Empty;
    product.DateCreated = DateTime.Now;
    product.Description = description;
    product.IsShippable = isShippable;
    product.Price = price;
    product.SaleEndDate = saleEndDate;
    product.SaleStartDate = saleStartDate;
    product.SalePrice = salePrice;
    product.Sku = sku;
    product.UrlName = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
    product.Visible = true;
    product.Weight = weight;
 
    product.Status = GenericContent.Model.ContentLifecycleStatus.Master;
 
    manager.Provider.RecompileItemUrls(product);
    manager.SaveChanges();
 
    var contextBag = new Dictionary<string, string>();
    contextBag.Add("ContentType", product.GetType().FullName);
 
    string workflowOperation = "Publish";
 
    WorkflowManager.MessageWorkflow(
                                    product.Id,
                                    product.GetType(),
                                    "OpenAccessDataProvider",
                                    workflowOperation,
                                    false,
                                    contextBag);
    return product.Id;
}

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK