コード例 #1
0
        /// <summary>
        /// Submit event handler. Responsible for creating items.
        /// </summary>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (IsValid)
            {
                foreach (RepeaterItem item in rpBulkItemEditor.Items)
                {
                    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                    {
                        var model = new ItemCreatorModel();

                        //Find controls from item in repeater
                        var ParentNode = (TextBox)item.FindControl("txtParentNode");
                        var TemplateID = (TextBox)item.FindControl("txtTemplateID");
                        var NoOfItems  = (TextBox)item.FindControl("txtNoOfItems");
                        var Languages  = (CheckBoxList)item.FindControl("cblLanguage");

                        //Set model values
                        model.NumberOfItems = System.Convert.ToInt32(NoOfItems.Text);
                        model.ParentNode    = ParentNode.Text;
                        model.TemplateId    = TemplateID.Text;
                        model.Languages     = Languages.SelectedValue;

                        //create items in CMS
                        var _itemCreator = new ItemCreator();
                        _itemCreator.ItemGenerator(model);
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Method to generate the items as per input in the CMS.
        /// </summary>
        /// <param name="ItemCreatorModel"> model containing parent item Id/Path, Template Id/Path, Languages, no. of items to create etc. </param>
        public string ItemGenerator(ItemCreatorModel model)
        {
            StringBuilder log = new StringBuilder();

            try
            {
                log.Append("Item Creation Process Started\n");

                for (int i = 0; i < model.NumberOfItems; i++)
                {
                    Sitecore.Data.Database masterDB = Sitecore.Data.Database.GetDatabase("master");

                    #region Item Creation
                    bool IsParentId = ID.IsID(model.ParentNode);
                    Item parentItem;
                    if (!IsParentId)
                    {
                        parentItem = masterDB.GetItem(model.ParentNode);
                    }
                    else
                    {
                        parentItem = masterDB.GetItem(ID.Parse(model.ParentNode));
                    }
                    TemplateItem template = masterDB.GetTemplate(model.TemplateId);
                    #endregion End Item Creation

                    Item newItem = parentItem.Add("Item-" + i, template);

                    #region Adding Language Versions

                    var languageCollection = model.Languages.Split(',');

                    foreach (string languageCode in languageCollection)
                    {
                        using (new LanguageSwitcher(languageCode))
                        {
                            Item parent = masterDB.GetItem(newItem.ParentID, Language.Parse(languageCode));

                            Item NewItem = parent.GetChildren().Where(x => x.ID == newItem.ID).FirstOrDefault();

                            NewItem.Editing.BeginEdit();

                            NewItem.Versions.AddVersion();

                            NewItem.Editing.EndEdit();
                        }
                    }

                    log.Append(newItem.Name + " Created successfully with ID: " + newItem.ID + "\n");
                    #endregion
                }
            }
            catch (Exception ex)
            {
                log.Append(ex.Message + "\n");
                log.Append(ex.StackTrace + "\n");
            }

            return(log.ToString());
        }