Thursday, September 18, 2014

Example of Sitecore Command Template Using Java Script:

Command template define 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.

We can create a command template that displays no user interface, a command template that uses a JavaScript prompt to collect an item name, or a command template that displays an ASP.NET or Sitecore Sheer user interface.

We can assign command templates along with data templates and branch templates using insert options.

Following example demonstrates creation of a command template which would check if a folder with current year name exists under main item “News”  and add a “News Item” in that folder. This example uses a JavaScript prompt to collect News Item name.

Step 1: Create two templates “News” & “News Item” with required fields.

Step 2: Create a class that inherits from Sitecore.Shell.Framework.Commands.Command, and override the Execute() method.

namespace Demo.Commands
{
    public class NewsItemCommand : Sitecore.Shell.Framework.Commands.Command
    {
        public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context)
        {
        }
    }

}

Step 3: Add a /configuration/command element to the file /App_Config/Commands.config.







Step 4: Under the appropriate project-specific folder within /sitecore/Templates, insert a command template definition item using /System/Branches/Command Template data template.























Step 5: In the command template definition item, in the Data section, for the Command field, enter the command code.


















The parameter id=$ParentID passes the ID of the item under which the user is inserting a new item. Without this parameter, Sitecore would pass the ID of the selected item rather than the item the user right-clicked.

Step 6: Assign this command template to standard values of the “News” template created in step1.




















Step 7: Complete required code in the command class.

  public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context)
        {
            if (context.Items != null && context.Items.Length > 0)
            {
                Item contextItem = context.Items[0];
                NameValueCollection parameters = new NameValueCollection();
                parameters["id"] = contextItem.ID.ToString();
                parameters["name"] = contextItem.Name;
                parameters["database"] = contextItem.Database.Name;
                parameters["language"] = contextItem.Language.ToString();
                Sitecore.Context.ClientPage.Start(this,"Run", parameters);// Executes custom method with required task
            }
        }
        protected void Run(Sitecore.Web.UI.Sheer.ClientPipelineArgs args)
        {
            if (args.IsPostBack)
            {
                if (!(String.IsNullOrEmpty(args.Result)) && (args.Result != "undefined") && (args.Result != "null"))
                {
                    Sitecore.Data.Database currentDatabase = Sitecore.Configuration.Factory.GetDatabase(args.Parameters["database"]);
                    Item parentItem = currentDatabase.GetItem(args.Parameters["id"]);
                    if (parentItem != null)
                    {
                        //check if News item contains a folder with current year
                        Item[] children = parentItem.Axes.GetDescendants();
                        Item currentYearItem = children.Where(x => x.TemplateName == "Folder" && x.Name == DateTime.Now.Year.ToString()).SingleOrDefault();
                        if (currentYearItem == null)
                        {                         
                            Sitecore.Data.Items.TemplateItem folderTemplate = currentDatabase.GetTemplate(Sitecore.TemplateIDs.Folder);
                            if (folderTemplate != null)
                            {
                                Item i = parentItem.Add(DateTime.Now.Year.ToString(), folderTemplate);
                                currentYearItem = i;
                            }
                        }
                        //create a news item and add it to the current folder
                        Sitecore.Data.Items.TemplateItem newsItemTemplate = currentDatabase.GetTemplate(new Sitecore.Data.ID("{D8BE36D8-20F0-4BCF-856C-D069E845D136}"));
                        Item newsItem = currentYearItem.Add(args.Result, newsItemTemplate);
                    }
                }
            }
            else
            {
                //Java script prompt to get the news item name to be created
                Sitecore.Context.ClientPage.ClientResponse.Input("Enter News Item Name", Sitecore.Globalization.Translate.Text("Name"), Sitecore.Configuration.Settings.ItemNameValidation, "'$Input' is not a valid name.", 10);
                args.WaitForPostBack();
            }
        }    

Step 8: Create a main news item based on “News” template.

Step 9: Create a news item under main item by clicking the command template which would create a folder with current year and a “News Item” under that folder.






















No comments:

Post a Comment