Adding Items

Adding Items

Sitecore.Configuration.Factory

  1. A class that encapsulates the general information available in the web.config file and contains methods that provide access to this information and the means to override it
  2. Create an object for the database where the item is located - replace SitecoreDB with the name of the Sitecore database where you want to perform the update, typically “master”
  3. Create an object to test whether the target item already exists - replace itempath with the relative path to the item in the Content Tree, such as “home/about/item1” or start the path with a backslash (/) to indicate an absolute path, such as “/sitecore/content/home/about/item1”– a null value will be set in either case if the item does not exist - Sitecore does compare case when evaluating item paths
  4. Test to see if a null value was generated and only create the item if it does not already exist
  5. Create an object that will act as a container for the new item - replace parentpath with the relative path to the parent item in the Content Tree, such as “home/about” or start the path with a backslash (/) to indicate an absolute path, such as “/sitecore/content/home/about” – a null value will be set in either case if the parent item does not exist
  6. Create an object for the template that will be used to create the new item - replace templatepath with the relative path to the template, such as “user defined/template1” or start the path with a backslash (/) to indicate an absolute path, such as “/sitecore/templates/user defined/template1” – a null value will be set in either case if the template item does not exist
  7. Verify that the specified parent and template items exist by checking for null values, and only create the new item if they both exist
  8. Use a security disabler to allow changes and place in a using block to ensure cleanup occurs
  9. Add the new item as a child of the parent object using the template object – replace itemname with the name of the new item

C# Web Control Code

var database = Sitecore.Configuration.Factory.GetDatabase("master");

 

                Item item = database.Items["/sitecore/content/home/moje_novice"];

                if (item != null)

                {

                    using (var disabler1 = new SecurityDisabler())

                    {

                        // add a new Item based on  the Document Template

                        var itm =

item.Add(item_ime.Text, database.Templates["Starter Kit/Content Templates/News Article"]);

                      

                        if (itm != null)

                        {                    

                            Label1.Text = itm.Paths.Path;

                        }

                    }

                }



OR



Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("SitecoreDB");

Sitecore.Data.Items.Item item = db.GetItem("itempath");

if (item == null)

{

      Sitecore.Data.Items.Item parent = db.GetItem("parentpath");

      Sitecore.Data.Items..TemplateItem template = db.GetTemplate("templatepath");

      if (parent!=null && template!=null)

      {

             using (new Sitecore.SecurityModel.SecurityDisabler())

             {

                   Sitecore.Data.Items.Item newitem = parent.Add(”itemname”, template);

             }

      }

}



OR



using (new Sitecore.SecurityModel.SecurityDisabler())

{

      Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("SitecoreDB");

      Sitecore.Data.Items.Item item = db.Items["itempath"];         

      if (item == null)

{

             Sitecore.Data.Items.TemplateItem template = db.Templates[“templatepath”];

             item = db.CreateItemPath(“itempath”, template);

}

}



OR



Sitecore.Data.Items.Item item = item.Add("New Node1", database.Templates["templatepath"]);

if ( item != null )

{

      // display the Item path to the User

      Response.Write(item.Paths.Path);

}



OR

 

Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("SitecoreDB");

Sitecore.Data.Items.Item item = db.GetItem("parentpath");

Sitecore.Data.Items.TemplateItem template = db.Templates["templatepath"];

 

Sitecore.Data.Items.Item newitem = item.Add("newitemname", template);

Notes

  • Properties, like those in the CreateItemPath snippet, could be used here instead of the “get” methods
  • Sitecore provides specialized classes for templates and media items. Please refer to snippets that invoked these specialized classes if the item to be accessed is a template or a media item. The referenced template must exist before the item can be created