Generating Test Data

Posted 07/15/2016 by Ray Ashby

Eventually, I would add the ability to select a template and set some data generation parameters such as field masks.

 But first, just a basic generator for the sample item template.


A simple input form for the utility.

<html xmlns="">
<head runat="server">
    label { display: table-cell; }
    input { display: table-cell; }
    <form id="form1" runat="server" style="display:table">
        <p>This utility will insert n items into the tree at the desired point with names like itemnameNNN   </p>
    <div style="display: table-row;">
    <asp:Label runat="server" ID="lblCount"  AssociatedControlID="txtCount"  >Count (Max 100) :</asp:Label>
    <asp:TextBox runat="server" ID="txtCount" Width="100" />  </div>
    <div style="display: table-row;">
    <asp:Label runat="server" ID="lblTreeLoc"  AssociatedControlID="txtTreeLoc" >Content Tree Location :</asp:Label>
    <asp:TextBox runat="server" ID="txtTreeLoc" Width="400" placeholder="Insert Point in Content tree" /> </div>
    <div style="display: table-row;">
    <asp:Label runat="server" ID="lblItemNameBase" AssociatedControlID="txtItemNameBase"  >Name Prefix :</asp:Label>
    <asp:TextBox runat="server" ID="txtItemNameBase" Width="100" placeholder="NewItemName" /> </div>
    <asp:Button runat="server" ID="btnAddItems" OnClick="btnAddItems_Click" Text="Add Items" />


And the basic code to read the input form and add the requested items to the content tree.

using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore;

namespace Training.Website.sitecore_modules.TestData
    public partial class Create : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)

        protected void btnAddItems_Click(object sender, EventArgs e)
            //  just for running pon test server.  Otherwise SecurityDisabler may not be the best choice.
              using (new Sitecore.SecurityModel.SecurityDisabler())
          //First get the requested parent item from the master database based on input of tree location
          Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master");
          Item parentItem = masterDb.Items[txtTreeLoc.Text];
          if (parentItem == null)
           //Template to use.   Future: Accept this as user inout
          TemplateItem template = masterDb.GetTemplate("sample/sample item");
          //Determine how many item to insert
          int itemCount;
          if (!Int32.TryParse(txtCount.Text, out itemCount))
          itemCount = Math.Min(itemCount, 100);

          for (int n = 1; n <= itemCount; n++)
            string itemName = txtItemNameBase.Text + n.ToString();
            Item item = parentItem.Add(itemName, template);
              //Add the fields  Future:  Display fields from selected template and allow more user direction.
              item.Fields["Title"].Value = "Item " + n.ToString();
              item.Fields["Text"].Value = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

 So there is the basic structure.     This was enough to add the items and have the test data I needed.

Next up:  Add some more features and package this up for the Sitecore Desktop.