在内容树中,结构如下
Home
-England
-France
-Germany
有一个子布局(CommentsForm.ascx),用于所有3个页面。当用户浏览“法国”并提交评论时,“评论”项目应保存在“法国”之下,依此类推。
在这种情况下,父项(必须在其下创建新项)是动态的。那么,在这种情况下如何获取父项。这是对的吗?
protected void btnSubmit_Click(object sender, EventArgs e)
{
Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
Item parentItem = Sitecore.Context.Item;
string name = "Comment_" + Sitecore.DateUtil.IsoNow;
TemplateItem template = masterDb.GetTemplate("/sitecore/templates/userdefined/Comment");
using (new SecurityDisabler())
{
//how to go about here??
//newItem["Author"] = txtAuthor.text;
//newItem["CommentText"] = txtComments.Text;
//parentItem.Add("name", template);
}
}
答案 0 :(得分:8)
您可以在生产中使用UserSwitcher更安全,但您也可以使用SecurityDisabler(newSecurityDisabler()){}
编辑和重命名必须在Editing.BeginEdit()事务
中进行 Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
Item parentItem = Sitecore.Context.Item;
string name = "Comment_" + Sitecore.DateUtil.IsoNow;
var template = masterDb.GetTemplate("/sitecore/templates/userdefined/Comment");
using (new Sitecore.SecurityModel.SecurityDisabler())
{
try
{
Item newItem = parentItem.Add("Name", template);
if (newItem!=null)
{
newItem.Editing.BeginEdit();
newItem["Author"] = txtAuthor.text;
newItem["CommentText"] = txtComments.Text;
newItem.Editing.EndEdit();
}
}
catch
{
newItem.Editing.CancelEdit();
}
}
答案 1 :(得分:3)
Create items programmatically based on template in sitecore使用简单代码
// The SecurityDisabler is required which will overrides the current security model, allowing the code
// to access the item without any security.
using (new Sitecore.SecurityModel.SecurityDisabler())
{
// Get the master database
Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master");
// Get the template for which you need to create item
Items.TemplateItem template = master.GetItem("/sitecore/templates/Sample/Sample Item");
// Get the place in the site tree where the new item must be inserted
Item parentItem = master.GetItem("/sitecore/content/home");
// Add the item to the site tree
Item newItem = parentItem.Add("NameOfNewItem", template);
// Set the new item in editing mode
// Fields can only be updated when in editing mode
// (It's like the begin transaction on a database)
newItem.Editing.BeginEdit();
try
{
// Assign values to the fields of the new item
newItem.Fields["Title"].Value = "NewValue1";
newItem.Fields["Text"].Value = "NewValue2";
// End editing will write the new values back to the Sitecore
// database (It's like commit transaction of a database)
newItem.Editing.EndEdit();
}
catch (System.Exception ex)
{
// Log the message on any failure to sitecore log
Sitecore.Diagnostics.Log.Error("Could not update item " + newItem.Paths.FullPath + ": " + ex.Message, this);
// Cancel the edit (not really needed, as Sitecore automatically aborts
// the transaction on exceptions, but it wont hurt your code)
newItem.Editing.CancelEdit();
}
}
答案 2 :(得分:1)
using (new Sitecore.SecurityModel.SecurityDisabler())
{
Item newItem = parentItem.Add("Name", TemplateItem.TemplateId);
newItem.Editing.BeginEdit();
newItem.Fields[Constants.IDs.Fields.SicParent.Code].Value = row.SicCode.ToString();
newItem.Fields[Constants.IDs.Fields.SicParent.Description].Value = row.Description;
// this field is a DropList
newItem.Fields[Constants.IDs.Fields.SicParent.Grouping].SetValue(groupItem, true);
newItem.Editing.EndEdit();
}
答案 3 :(得分:1)
您可以向parentItem
添加项目,然后按以下方式修改新项目:
using (new SecurityDisabler())
{
Item newItem = parentItem.Add("name", template);
newItem.Editing.BeginEdit();
newItem["Author"] = txtAuthor.text;
newItem["CommentText"] = txtComments.Text;
newItem.Editing.EndEdit();
}