Sitecore create item programmatically with over 100 fields using GlassMapper

85 views Asked by At

I need to write a job to create 1000s of items in Sitecore. As there are 100s fields in my Sitecore Data Template I would like to avoid repeating the below code line for each field in my code:

newItem.Fields["Title"].Value = "NewValue1";

Can I use GlassMapper (which I believe needs to run in the MVC context) or an equivalent to do this instead?

1

There are 1 answers

0
Vikrant Punwatkar On

You have to map at least once even if it 100s of fields. You do not need to repeat that for 1000s of items. You should get all 1000 sourceObjects in a list and iterate over it to map its properties to Item fields.

 // Iterate through 1000s of items to create
foreach(var sourceObject in SourceObjects)
{
    var newItem = New Item();
    newItem.Fields["Title"].Value = sourceObject.Title;
    // Field mapping for rest 99 fields
}