Developer forum

Forum » Development » How to instantiate a product field

How to instantiate a product field

Lars Larsen
Reply

Hi,

how do I instantiate a product field in eCommerce from code through the API? I would like to instantiate product fields at application startup instead of creating the product fields by hand in the backend.

 

 


Replies

 
Dmitriy Benyuk
Reply
This post has been marked as an answer

Hi Lars,
here is the code for doing this:

using Dynamicweb;

namespace Dynamicweb.Examples.CSharp.Notifications.Standard
{
    [Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.Standard.Application.Start)]
    public class ApplicationStartObserver : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            if (args == null)
                return;

            if (!(args is Dynamicweb.Notifications.Standard.Application.StartArgs))
                return;

            Dynamicweb.Notifications.Standard.Application.StartArgs item = (Dynamicweb.Notifications.Standard.Application.StartArgs)args;

            //Creating new ProductField
            Dynamicweb.eCommerce.Products.ProductField pf = new eCommerce.Products.ProductField();                                       
            pf.Name = "Field1";
            pf.SystemName = "Field1";
            pf.TemplateName = "Field1";
                //Avavilable ProductField types
                //1: Tekst (255)
                //2: Lang tekst
                //3: Checkboks
                //4: Dato
                //5: Dato/tid
                //6: Heltal
                //7: Kommatal
                //8: Link
                //9: Filarkiv
                //10: Tekst (100)
                //11: Tekst (50)
                //12: Tekst (20)
                //13: Tekst (5)
                //14: Editor
                //15: List box          
            pf.TypeID = 1;
            pf.Save("0");
        }
    }
}

Regards,
Dmitrij

Votes for this answer: 1

 

You must be logged in to post in the forum