4

Is there no standard way to incorporate/ consider a template's standard values when creating a Glassmapper model programmatically? This is all I can find online, but it seems that you have to rename an object just to consider standard values:

https://stackoverflow.com/questions/26674102/how-to-apply-standard-values-to-an-item-created-with-glass-mapper

0

When you create the item directly from the model GlassMapper has no way of knowing if the values of the item's properties are to be overwritten by the standard values or if you intended them to be null/empty.

What you want is to first create the item by specifying a parent and a name for the new item.

var itemName = "My new item";
var item = SitecoreContext.Create<IItem, IParentItem>(parent, itemName);

The model of the new item is the first type parameter. The template for the new item is also inferred from this type parameter (IItem in this case) assuming it has been mapped correctly specifying its template ID (or branch ID) - otherwise you will get an exception.

This new item will now have any standard values set and you can change any values as you please and then save those changes.

item.SomeProperty = "Some value";
SitecoreContext.Save(item);
| improve this answer | |
  • What if I need to create the model outside of Sitecore first? I use a separate system to get information for the sitecore items, then comparisons to decide if the item needs to be created or updated. Is there any way to access the standard values of a template programmatically? – rfraczkowski Nov 23 '16 at 22:51
  • You can get the standard value of a field through Sitecore.Data.StandardValuesManager.GetStandardValue(Field field). – Søren Kruse Nov 24 '16 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.