Moamen Naanou Moamen Naanou - 3 months ago 16
ASP.NET (C#) Question

Set a templates Field as Shared or Unversioned Programmatically

I'm using Sitecore Version 8.1 and trying to create

Templates
,
Fields
and
Items
programmatically(to ensure some scenarios in case item/template not exist) and I'm trying to set specific template's field as
shared
or
unversioned
but couldn't because:

bool TemplateFieldItem.Shared
attribute has only
getter
method (there is no setter).

My code:

var icon = PageTemplate.AddField("Icon", "Data");
using (new EditContext(icon.InnerItem))
{
icon.Type = "Image";
icon.Sortorder = 3;
icon.Shared = false;
}


So my problem is only in
icon.Shared = false;
as well as
icon.Unversioned = false;

Answer

You need to set it on the inner item. In theory changing field from version to unversioned or from shared to unshared is a complex process, but assuming that you're adding those fields and there should be no items with them yet, you should be ok,

Use:

icon.InnerItem[TemplateFieldIDs.Unversioned] = "1"; // or null

or:

icon.InnerItem[TemplateFieldIDs.Shared] = "1"; // or null
Comments