Question

How to assign templates to the _standard values programatically?.There is one template under Size folder.I want to add the item in the Employees _Standard values through insert option.

Steps: 1.Select the _Standard values of Employees Template. 2.add TeamSize template as follows [Click on the "Configure" >>Assign>>TeamSize The above is manual process.I want to achieve the same using programmatically.

Just tell some sample idea alone.Rough snippet is enough.enter image description here

Was it helpful?

Solution

I see you didn't take the advice of everyone who replied to your previous question

How to assign templates to another template dynamically in sitecore?

This really does sound like a bad idea, but if you genuinely have a good reason for this approach then the answer is very similar to that of your other question. In this case you just use the Insert Options field:

Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("master"); 

Item standardValItem = db.GetItem("/sitecore/templates/Insert Options/Employees/__Standard Values"); 
Item teamSizeTemplate = db.GetItem("/sitecore/templates/Insert Options/Size/TeamSize"); 

using (new Sitecore.SecurityModel.SecurityDisabler())
{
    try
    {
        standardValItem.Editing.BeginEdit();
        standardValItem[Sitecore.FieldIDs.Branches] = teamSizeTemplate.ID.ToString();
    }
    finally
    {   
        standardValItem.Editing.EndEdit();
    }
}

OTHER TIPS

To assign TeamSize template to Insert options of Employees you have on the new window that is opened , you have to go on Templates tree and extend Insert Option folder and choose Team Size. For settings dinamically Insert Options for an item you need to do :

yourItem.Template.StandardValues["__Master"] =yourTemplate.ID.ToString(); 

__Master is the name of the fields, you can find it here :

/sitecore/templates/System/Templates/Sections/Insert Options/Insert Options/__Masters 

Why do you want to change insert options dinamically ? I think you need this kind of implementation just when you have a branch website, and you want to create from that branch other Sites. And depending on the site , on some site you need to set some Insert Options and on other no .

Please also check this link , Insert Options are explained very clear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top