Creating attribute values

To create a product attribute value, you must perform the following:

  1. Get the catalog manager.
    Get an instance of the CatalogManager object.
  2. Get the parent attribute.
    Get an instance of the specified parent attribute.
  3. Create new product attribute value.
    To create new product attribute value, call the CreateProductAttributeValue method of the manager.
  4. Set the properties of the ProductAttributeValue instance.
    In this example the following properties are set:
    • Title
    • Name
    • IsActive
    • Description
    • Ordinal
  5. Associate the attribute value to the attribute.
    To do this, set the Parent property to the instance of the parent attribute.
  6. Save the changes.
    Save the changes to the catalog manager.

Here is a code example:

public static void CreateProductAttributeValue(Guid parentAttributeId, string title, string description, bool isActive)
{
    CatalogManager catalogManager = CatalogManager.GetManager();
 
    ProductAttribute parentAttribute = catalogManager.GetProductAttribute(parentAttributeId);
 
    if (parentAttribute == null)
    {
        return; //Parent attribute does not exist.
    }
 
    ProductAttributeValue productAttributeValue = catalogManager.CreateProductAttributeValue();
 
    productAttributeValue.Title = title;
    productAttributeValue.Name = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
    productAttributeValue.IsActive = isActive;
    productAttributeValue.Description = description;
    productAttributeValue.Ordinal = parentAttribute.TotalProductAttributeValuesCount;
    productAttributeValue.Parent = parentAttribute;
 
    catalogManager.SaveChanges();
}

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