Showing posts with label template. Show all posts
Showing posts with label template. Show all posts

Wednesday, September 28, 2016

Sitecore Command Template

Hi All,

Today we discuss about command template.
Command Template: allow insert of items according to logic rather than predefined structures.
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 BasicSitecore.Custom_Code
{
    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: 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 insert option created in step1.


Step 7: Create a new aspx page in “/sitecore modules/shell” With name News.aspx because we give reference of this page in command class code.










Step 8: 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);//Run method executes on

            }
        }
        protected void Run(Sitecore.Web.UI.Sheer.ClientPipelineArgs args)
        {
            if (args.IsPostBack)
            {
                if (!(String.IsNullOrEmpty(args.Result)) && (args.Result != "undefined") && (args.Result != "null"))
                {
                }
            }
            else
            {
                Sitecore.Text.UrlString url = new Sitecore.Text.UrlString("/sitecore modules/shell/News.aspx");
                url.Append("id", args.Parameters["id"]);
                url.Append("database", args.Parameters["database"]);
                Sitecore.Context.ClientPage.ClientResponse.ShowModalDialog(url.ToString(), true);
                args.WaitForPostBack(true);
            }
        }   

Step 9: Handle “OK” and “Cancel” buttons of the user interface News.aspx

protected void btnOK_Click(object sender, EventArgs e)
        {
            if (! String.IsNullOrEmpty(txtNewsItem.Text))
            {
                Sitecore.Data.Database currentDatabase = Sitecore.Configuration.Factory.GetDatabase(Sitecore.Web.WebUtil.GetQueryString("database"));
                Item parentItem = currentDatabase.GetItem(Sitecore.Web.WebUtil.GetQueryString("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
//Enter your template ID for news item
                    Sitecore.Data.Items.TemplateItem newsItemTemplate = currentDatabase.GetTemplate(new Sitecore.Data.ID("{42B8742F-FD44-4310-8717-39F1CD1EA7F6}"));
                    Item newsItem = currentYearItem.Add(txtNewsItem.Text, newsItemTemplate);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Close", "window.top.dialogClose();", true);
                }
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Warning", "alert('Please enter news item');", true);
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Close", "window.top.dialogClose();", true);
        }

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

Step 11: 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.












Helpful link or references:
http://techblogsearch.com/a/example-of-sitecore-command-template-with-user-interface.html
http://sitecoreworld.blogspot.in/2014/09/example-of-sitecore-command-template_21.html


How Use Branch Templates in Sitecore CMS

Hi All,
Today we discuss about Branch template in sitecore.
At first question come in mind what is Branch template.

A Template defines the fields and sections associated with a type of item. Items in sitecore can be created using three different template types.

·         Data Template:  form the framework around which items are built. They define fields used to control how data is entered and can inherit from other templates to enable reuse.

·         Branch Template: allow you to create a set of items rather than a single item at a time.

·         Command Template: allow insert of items according to logic rather than predefined structures.

Data Template: A data template defines a data type. Sitecore associates a data template with each item. The data template defines the structure of all items associated with that data template. A data template contains a number of data template sections, each of which contains a number of data template fields.

Branch Template: A branch template consists of a branch template definition item, which can contain a single item, a hierarchy of items, or multiple hierarchies of items. When a user invokes a branch template, Sitecore duplicates the item(s) beneath the branch template definition item, including any field values, and then performs token substitution on item names and field values.

Steps for creating brach template:
1. At first navigate to "/sitecore/templates/User Defined" or "/sitecore/templates" create a template which is the base for branch template or either you can select folder(/sitecore/templates/Common/Folder) for that as well if you don't wanna create any template for that.
In my case I create a "Website Base" template that inherit the base template of website and give the blue folder icon that give unique look to my branch template.



2. Then navigate to "/sitecore/templates/Branches/User Defined"
3. Under this node either create new branch template directly or create a separate folder then create branch template inside.
4. Right click and select "New Branch" option.
5. Select the template for that in my case "Website Base" or if you don't create any then select Folder(/sitecore/templates/Common/Folder).
6. A new branch template item will have been created including a Website Base item name $name.
7. $name is token that is replaced by selected name when branch template used.
8. Under $name item create a tree structure that you wanna create from branch template.

9. For creating item under sitecore/content node, right click and select insert from template.
10. navigate to branch template and select that and give suitable name. A tree structure as you described in branch template automatically created. 


Here are the helpful links:
https://www.geekhive.com/buzz/post/2015/02/how-to-create-branch-templates-in-sitecore-cms/
http://sitecoreworld.blogspot.in/2014/09/sitecore-branch-template-example.html