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

1 Answer 1

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);
2
  • 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? Commented Nov 23, 2016 at 22:51
  • You can get the standard value of a field through Sitecore.Data.StandardValuesManager.GetStandardValue(Field field). Commented Nov 24, 2016 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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