Specific DB Creating

Specific DB Creating

Specific DB Creating

The Sitecore.Configuration.Factory class 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
  1. 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”
  2. Create an object for the target template in the database - replace templatepath with the absolute path to the template in the Content Tree, such as “/sitecore/Templates/User Defined”
  3. Check if template Item does not exists
  4. Use a security disabler to allow changes and place in a using block to ensure cleanup occurs
  5. Get the parent Item of the template (folder)
  6. Create the template under the given parent Item

Alternatively:
  1. Use a security disabler to allow changes and place in a using block to ensure cleanup occurs
  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 template already exists - replace templatepath with the relative path to the template in the Content Tree, 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 does not exist
  4. Test to see if a null value was generated and only create the template if it does not already exist
  5. Create an object for the target container for the template - replace templateparent with the relative path to the container for the template, such as “user defined” or start the path with a backslash (/) to indicate an absolute path, such as “/sitecore/templates/user defined/”
  6. Add the new template – replace templatepath with the same path that you used above to test if the template already existed, such as - such as “user defined/template1” or start the path with a backslash (/) to indicate an absolute path, such as “/sitecore/templates/user defined/template1”

C# Web Control Code

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

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

if (template == null)

{

      using (new Sitecore.SecurityModel.SecurityDisabler())

      {

             Sitecore.Data.Items.Item parent= db.Items.GetItem(“templatepath”);

             templateItem = db.Templates.CreateTemplate(“templatename”, parent);

      }

}

OR

using (new Sitecore.SecurityModel.SecurityDisabler())

{

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

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

if (template == null)

{

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

template = db.Templates.CreateTemplate("templatepath", parent);

}

}

OR

using (new Sitecore.SecurityModel.SecurityDisabler())

{

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

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

if (template == null)

{

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

template = db.Templates.CreateTemplate("templatepath", parent);

}

}