Creating attributes

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

  1. Get the catalog manager.
    Get an instance of the CatalogManager object.
  2. Create new product attribute.
    To create new product attribute, call the CreateProductAttribute method of the manager.
  3. Set the properties of the ProductAttribute instance.
    In this example the following properties are set:
    • Title
    • Name
    • IsActive
  4. Set the AppliedTo property.
    If the attribute can be applied to all product types, use "0". If the attribute is applied only to specific product types use "1".
  5. Set the specified product types.
    If there are specified product types, add them to the ProductTypes collection of the attribute. To do so, you must perform the following:
    1. Create a product attribute link.
      Create an instance of the ProductAttributeLink class.
    2. Set the properties of the ProductAttributeLink instance.
      Set the following properties:
      • Id
      • ProductTypeId
      • ProductAttribute
    3. Add the ProductAttributeLink instance.
      Add the instance to the ProductTypes collection of the attribute.
  6. Save the changes.
    Save the changes to the catalog manager.

Here is a code example:

public static void CreateProductAttribute(string title, List<string> productTypeTitles, bool isActive)
{
    CatalogManager catalogManager = CatalogManager.GetManager();
 
    ProductAttribute productAttribute = catalogManager.CreateProductAttribute();
 
    productAttribute.Title = title;
    productAttribute.Name = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
    productAttribute.IsActive = isActive;
 
    if (productTypeTitles == null || productTypeTitles.Count == 0)
    {
        productAttribute.AppliedTo = "0";       // "0" denotes attribute is linked to ALL product types
    }
    else
    {
        productAttribute.AppliedTo = "1";       // "1" denotes attribute is linked to one or more specific product types
 
        foreach (string productTypeTitle in productTypeTitles)
        {
            ProductType productType = catalogManager.GetProductTypes().Where(t => t.Title == productTypeTitle).FirstOrDefault();
 
            if (productType != null)
            {
                ProductAttributeLink productAttributeLink = new ProductAttributeLink();
                productAttributeLink.Id = Guid.NewGuid();
                productAttributeLink.ProductTypeId = productType.Id;
                productAttributeLink.ProductAttribute = productAttribute;
                productAttribute.ProductTypes.Add(productAttributeLink);
            }
        }
    }
 
    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