Command Template: allow insert of items according to logic rather than predefined structures.
The command template defines a class and method to be called during an insert operation. Unlike data templates and branch templates, which consist of predefined structures, command templates reference Sitecore UI commands to invoke wizards or other logic used to create new items.
Step 1: Writing our Command
Create our Command Class in your solution and inherit Command Class. Command Class is available in Sitecore.Shell.Framework.Commands namespace in Sitecore.Kernel.dll
using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Globalization;
using Sitecore.Shell.Framework.Commands;
using Sitecore.Web.UI.Sheer;
using System;
using System.Collections.Specialized;
namespace SitecoreMaster82.Models.CommandTemplate
{
public class CustomCommandTemplate : Command
{
public override void Execute(CommandContext context)
{
if (context.Items.Length == 1)
{
Item item = context.Items[0];
NameValueCollection parameters = new NameValueCollection();
parameters["id"] = item.ID.ToString();
parameters["language"] = item.Language.ToString();
parameters["database"] = item.Database.Name;
Context.ClientPage.Start(this, "Run", parameters);
}
}
protected void Run(ClientPipelineArgs args)
{
if (!args.IsPostBack)
{
//Means we are in the initial step, we want to ask for the name of the news
Context.ClientPage.ClientResponse.Input("Enter the name of the news item:", Translate.Text("New NewsItem"), Sitecore.Configuration.Settings.ItemNameValidation, "'$Input' is not a valid name.", 100);
args.WaitForPostBack();
}
else
{
//Now we got a postback, which means we got a response
if (!String.IsNullOrEmpty(args.Result) && args.Result != "undefined")
{
Database db = Sitecore.Configuration.Factory.GetDatabase(args.Parameters["database"]);
Item parent = db.GetItem(args.Parameters["id"], Language.Parse(args.Parameters["language"]));
//Create the folder structure if it doesn't exist
string year = DateTime.Now.Year.ToString();
string month = DateTime.Now.Month.ToString();
TemplateItem folderTemplate = db.Templates[TemplateIDs.Folder];
Item yearFolder = parent.Children[year];
if (yearFolder == null)
yearFolder = parent.Add(year, folderTemplate);
Item monthFolder = yearFolder.Children[month];
if (monthFolder == null)
monthFolder = yearFolder.Add(month, folderTemplate);
//Create the news Item
TemplateItem template = Context.ContentDatabase.Templates[new ID("{5401DFB9-D884-418A-8EB2-1CCC3931F6EE}")]; //This GUID is of data templates based on you want to create item /sitecore/templates/User Defined/News
Item item = monthFolder.Add(args.Result, template);
//Load the Item in the content editor
Context.ClientPage.SendMessage(this, "item:load(id=" + item.ID.ToString() + ")");
}
}
}
}
}
Step 2: Add Command in your Commands.config file
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<commands>
<command name="CommandTemplate:TestCommandTemplate" type="SitecoreMaster82.Models.CommandTemplate.CustomCommandTemplate,SitecoreMaster82" />
</commands>
</sitecore>
</configuration>
Step 3: Add Command Template Item
- Right Click on Branches and select Command Template
- Give our Command Template a proper Name
- Now add command Name to our Command Template
- Keep in mind that Command_Name would be the same name used in a command.config file
- That’s it our Command Template is ready to use, we just need to add it in Insert Options and its done. Whenever we will add new Items using this Command Template it will automatically create our Item with proper folder structure.
Conclusion
Finally, I would like to conclude this post, I hope I have been able to help you and justify the topic.
No comments:
Post a Comment