我需要以编程方式在Sitecore节点下的每个部分(内容,布局,渲染,MediaLibrary,模板)中创建空文件夹。
请告知。
答案 0 :(得分:3)
Sitecore中的文件夹是一个项目,例如模板:/ sitecore / templates / Common / Folder {A87A00B1-E6DB-45AB-8B54-636FEC3B5523}
所以你需要代码来添加一个项目:
请参阅: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
例如在布局文件夹下,您可以使用其他模板,模板:/ sitecore / templates / System / Layout / Renderings / Sublayout文件夹 所以有更多的文件夹模板,当然你可以创建自己的,然后添加你需要的插入选项或在标准值中设置一个漂亮的icoon。
<强>总结强>
答案 1 :(得分:0)
//首先获取master数据库
Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase(“master”);
//在“Renderings”下创建一个文件夹。根据要求更改路径
Sitecore.Data.Items.Item parentNode = masterDB.GetItem(“/ sitecore / layout / Renderings”);
//始终从此位置获取文件夹模板
Sitecore.Data.Items.Item文件夹= masterDB.GetItem(“/ sitecore / templates / Common / Folder”);
//在所需位置添加文件夹
parentNode.Add(“Folder Name”,new TemplateItem(folder));
答案 2 :(得分:0)
正如Jan Bluemink所说:
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;
}
和电话:
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);