Abhijit Banik
Abhijit Banik

Reputation: 11

How to insert an empty folder with desired name under an item in Sitecore programmatically?

I need to create empty folders in each sections (content, Layout, renderings, MediaLibrary, Templates) under Sitecore node programmatically.

Please advise.

Upvotes: 1

Views: 2384

Answers (3)

SilentTremor
SilentTremor

Reputation: 4912

As Jan Bluemink said:

public static Item AddFolder(String name, Item Parrent = null)
        {
            Database myDatabase = Sitecore.Context.Database;
            if (Parrent == null)
            {
                return null;
            }
            Item kiddo = null;
            try
            {
                Sitecore.Data.Items.TemplateItem FolderTemplate = myDatabase.GetTemplate("{EB395152-CC2F-4ECB-8FDD-DE6822517BC8}");
                using (new Sitecore.SecurityModel.SecurityDisabler())
                {
                    kiddo = Parrent.Add(name, FolderTemplate);                    
                    //Insert values in fileds
                    // posibly you need to change language and add version to update;
                    // let say a template "article with some id {00101010101010-100110-1010100-12323}" with two fiels single line text, multiline text  or rich text editor


                    //kiddo.Editing.BeginEdit();
                    //try
                    //{
                    //    kiddo.Fields["Title"].Value = "Title 1";
                    //    kiddo.Fields["Description"].Value = "description 1";
                    //    kiddo.Editing.EndEdit();
                    //}
                    //catch
                    //{
                    //    kiddo.Editing.CancelEdit();
                    //}
                }
            }
            catch (Exception ex) {
                return null;
            }
            return kiddo;
        }

and the call:

Item content = Sitecore.Context.Database.GetItem("/sitecore/content");
        Item contentFolder =  AddFolder("folder", content);

        Item medialib = Sitecore.Context.Database.GetItem("/sitecore/media library/medialib");
        Item medialibFolder =  AddFolder("folder", medialib);

Upvotes: 0

Abhijit Banik
Abhijit Banik

Reputation: 11

//Get the master database first

Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");

//Creating a folder under "Renderings". Change path as per requirement

Sitecore.Data.Items.Item parentNode= masterDB.GetItem("/sitecore/layout/Renderings");

//Always get the folder template from this location

Sitecore.Data.Items.Item folder = masterDB.GetItem("/sitecore/templates/Common/Folder");

//Add the folder at desired location

parentNode.Add("Folder Name", new TemplateItem(folder));

Upvotes: 0

Jan Bluemink
Jan Bluemink

Reputation: 3487

A folder in Sitecore is a Item, with for example Template: /sitecore/templates/Common/Folder {A87A00B1-E6DB-45AB-8B54-636FEC3B5523}

So you need code to add a item:

See: How to programmatically populate Sitecore items (Add item and fields)?

https://briancaos.wordpress.com/2011/01/14/create-and-publish-items-in-sitecore/

http://learnsitecore.cmsuniverse.net/en/Developers/Articles/2009/06/ProgramaticallyItems2.aspx

For Example Under the Layout folder you can use a other Template, Template: /sitecore/templates/System/Layout/Renderings/Sublayout Folder So there are more folder templates, and of course you can create your own, and adding the insert options you need or set a nice icoon in the standard values.

Summarized:

  • You need privileges to create an Sitecore item, You can use the SecurityDisabler or User Switcher.
  • Get the parent item.
  • Create the item with the template you want.

Upvotes: 3

Related Questions