Garnaph
Garnaph

Reputation: 637

Trying to move a sitecore Item into a folder in the content tree

I've created a structure in Sitecore's content tree as follows : A Home root item, based on a template. Then I have a series of folders under the Home item, named by dates.

I'm creating a command template so that when I add a new item under the Home item, it automatically moves it under the folder represented by the current date.

However, the Item class' MoveTo method only takes an Item as a parameter. So I have no idea how to tell it to move the Item into a Folder, as a Folder cannot be represented as an Item?

I've done some digging, and I've seen that when I place an item directly under the folder, it shows a different ParentID than if I put it directly under the Home item, implying that the Folder is indeed an Item. However, when I try to retrieve the Item represented by the Folder, I get null.

e.g. Sitecore.Context.Database.Items["/sitecore/content/Home/30Nov2011"];

Upvotes: 3

Views: 3670

Answers (1)

Stephen Pope
Stephen Pope

Reputation: 3551

Everything in Sitecore is an item (even folders), you can resolve the folder as an item based on its path or GUID using the Database object.

var currentDb = Sitecore.Data.Database.GetDatabase("master");
var folderItem = currentDb.GetItem("/sitecore/content/Home/30Nov2011");
Sitecore.Context.Item.MoveTo(folderItem);

Edit: As you are working in the content editor environment you will need to explicitly tell it which database to use (the master database in this case) as the Context scope will not contain the database you are hoping to work with.

Hope this helps.

Upvotes: 5

Related Questions