Wednesday 14 June 2017

Change Item’s Template in Sitecore Dynamically

This is useful when the wrong template was selected while creating an item, or if a new template was created and existing items are to be updated to the new template.

NOTE:-
Be careful when changing templates. If the original template has fields that aren’t in the new template, the data in those fields will be lost. 

Changing Templates:-

Select the item you'd like to change.
Click the "Change" button in the "Template" section of the "Configure" tab in the ribbon.
Item->Configure->Change 





New popup will be appear for "Select The Template" where "change" button select.


Select the template you had like to change item to. Click "Next" button to continue.
New "Change" Popup will be appear, With notice for any fields where data may be lost.
In Below screen point number 2, All fields data will be lost when template changes done.



Click "Next" to continue. Confirmation Popup will come and click "Finish" will close the popup. and item will be updated to selected template.


Programmatically Change Template:-

We can change template from code behind dynamically using following code.
If you have more than 1 items and want to change template of all that item, than you can change template dynamically with write below few line of code.

 


Here, Sitecore provide "ChangeTemplate(template)" Method. we can pass new template in this method.


var template = Sitecore.Context.Database.Templates["system/media/Unversioned/Image"];
item.ChangeTemplate( template );
 Happy Searching!! :)