4

Hello I'm trying to override the AddFromTemplate pipeline to manually create datasources for the components inside the template that i'm using.

What I would like to accomplish

experience editor insert page button

When a user clicks the insert page button in the experience editor the controls/components associated with the template selected will have a new datasource created for them.

What I have tried so far

I have been trying to override the AddFromTemplate pipeline but I don't know how to get a list of the controls/components(ex. text-block, form, etc.) associated with the template from the TemplateItem. Once I get the controls associated with the template I would like to get the templates for those controls. Then I can create a new template programmatically and assign that new template as datasource for the control on the new page I originally created.

This solution is a based off a Helix architecture using Sitecore 8.2

Let me know if there is any additional information needed.

5
  • 2
    Is your new page a branch? You can create a new page as a branch, with all the components in place. If your datasources are under the page, in the branch, you can use AddFromTemplate to change those datasources if they don't get updated. But a page as a branch is what you need.
    – Chris Auer
    Commented Feb 20, 2019 at 16:35
  • @ChrisAuer I dont believe it is a branch. Is this not possible if the page isnt a branch?
    – scouty
    Commented Feb 20, 2019 at 16:44
  • 1
    You can, but it is a lot of coding. You are going to need a lot code to find all the renderings, find all the datasources, recreate all the new items. If you are in a branch, it can all be completely built ahead of time.
    – Chris Auer
    Commented Feb 20, 2019 at 16:56
  • @ChrisAuer Yeah this is what I think I need to. Unfortunately I don't think branch template are what I need as I need the components being created to not be created underneath the page item but underneath a feature folder (/sitecore/content/MySite/Features).
    – scouty
    Commented Feb 21, 2019 at 17:01
  • A new datasource for each. I have added an alternative to my answer. You can use a command template to create your branch. And then create/alter your datasources in the code, post branch. I'll add more info on how to create a command template.
    – Chris Auer
    Commented Feb 21, 2019 at 17:11

1 Answer 1

9

What you want to do is take advantage of Sitecore's branching functionality. A branch in Sitecore is basically a prebuilt item tree that will be copied and inserted where you want it. It could be one item or a thousand items.

In your case, what it helps you do is to build out a page, with all the related data folders under the page. All delivered in an insert option for your items.

Create branch

Under the item /sitecore/templates/Branches/ insert a new branch item. This will prompt you to pick a template to base this branch on. Here you would pick you page template that you want to create. Once complete, edit this new page in the experience editor just like you would in your site tree. Add you components, any default content you need, etc... During this process you will create any sub items that are needed as data sources for the item.

Once complete, you will have a new page exactly as you want it to be created in your site. It should look like the image below.

enter image description here

Add as insert option for your site

One of the best parts of a branch is that you can add it to insert options just like any other item. What ever item you want to have this as an insert option, edit the insert options and select the branch item you just created. Do not select the $name item, but its parent which is the branch item. The $name item is the item that will be created.

NOTE: I always update my branch item to have the same icon as the item it is creating. This makes the visual experience the same and no one knows it is a branch item and not the original item.

Create a new item

Using the insert options you set above, insert a new item like normal. Once complete, the item and all its children will be created. Matching your branch item 1 for 1.

I am not sure when Sitecore added it to the base, but in Sitecore 9 all of the rendering datasources are updated automatically. This allows for every new item to reference its own sub items for datasources with no intervention required from the editors. If this does not happen for you, just use Kam's branch presets. This will do the exact same thing.

https://github.com/kamsar/BranchPresets

That's it. You can now use this same process to create as many pre-built pages as your editors need.

Alternative, Use a command template to create the branch and do the work.

public class CreateSite : Command
{
    //protected void CreateSite()
    //{
    //    //Change the eventName here
    //    SheerResponse.Timer("multisite:createSiteA", 100);
    //}

    public static string InvalidPath = "/sitecore/templates/Branches/Project/Common/Sites/Site Root";


    public override void Execute(CommandContext context)
    {
        if (context.Items.Length == 1)
        {
            var item = context.Items[0]; 
            var parameters = new NameValueCollection();
            parameters["id"] = item.ID.ToString();
            parameters["language"] = item.Language.ToString();
            parameters["database"] = item.Database.Name;
            Sitecore.Context.ClientPage.Start(this, "Run", parameters);
        }
    }

    protected void Run(Sitecore.Web.UI.Sheer.ClientPipelineArgs args)
    {
        if (!args.IsPostBack)
        {
            //Means we are in the initial step, we want to ask for the name of the news
            Sitecore.Context.ClientPage.ClientResponse.Input("Enter site name:",
                Sitecore.Globalization.Translate.Text("Site Name"),
                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")
            {
                var db = Sitecore.Configuration.Factory.GetDatabase(args.Parameters["database"]);
                var parent = db.GetItem(args.Parameters["id"], Language.Parse(args.Parameters["language"]));

                BranchItem branch = parent.Database.GetItem(Constants.BranchSite.SiteA);
                var item = parent.Add(args.Result, branch);

                // DO YOUR WORK HERE

                Sitecore.Context.ClientPage.SendMessage(this, "item:load(id=" + item.ID + ")");
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.