gpt4 book ai didi

c# - Sitecore 创建包含字段 [] 的项目

转载 作者:行者123 更新时间:2023-11-30 14:14:49 25 4
gpt4 key购买 nike

我想使用隐藏代码创建一个项目到 sitecore。

我找到了这段代码,它工作得很好。

public void CreateItem(String itmName)
{
//Again we need to handle security
//In this example we just disable it
using (new SecurityDisabler())
{
//First get the parent item from the master database
Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master");
Item parentItem = masterDb.Items["/sitecore/content/SOHO/Settings/Metadata/Project"];


//Now we need to get the template from which the item is created
TemplateItem template = masterDb.GetTemplate("SOHO/Misc/Project");
//Now we can add the new item as a child to the parent
parentItem.Add(itmName, template);


//We can now manipulate the fields and publish as in the previous example
}
}

但我也想填写字段。喜欢..

Item.Fields["test"].Value="testing";

为此我找到了如何编辑一个项目

public void AlterItem()
{
//Use a security disabler to allow changes
using (new Sitecore.SecurityModel.SecurityDisabler())
{
//You want to alter the item in the master database, so get the item from there
Database db = Sitecore.Configuration.Factory.GetDatabase("master");
Item item = db.Items["/sitecore/content/home"];


//Begin editing
item.Editing.BeginEdit();
try
{
//perform the editing
item.Fields["Title"].Value = "This value will be stored";
}
finally
{
//Close the editing state
item.Editing.EndEdit();
}
}
}

但我不知道如何将这两件事结合起来。

我想到了两种方法。

方法一

获取我创建的 ItemID

我可以获取 NameName 可能会重复。

方法二

在创建 Item 之前填写字段

但是……我又不知道如何使用这两种方法。

如果我能得到一些提示,我将不胜感激。

提前致谢。

最佳答案

方法 item.Add() 返回创建的项目,因此您的代码应如下所示:

    Item newItem = parent.Add(itemName, template);
newItem.Editing.BeginEdit();
newItem.Fields["fieldName"].Value = "fieldValue";
newItem.Editing.EndEdit();

关于c# - Sitecore 创建包含字段 [] 的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11133541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com