如何在 Sitecore 中以编程方式创建项目

How to create items programmatically in Sitecore

在内容树中,结构如下

Home
 -England
 -France
 -Germany

有一个子布局(CommentsForm.ascx),它被用于所有 3 个页面。当用户浏览 'France' 并提交评论时,'Comment' 项应保存在 'France' 下,依此类推。

在这种情况下,父项(必须在其下创建新项)是动态的。那么,在这种情况下如何获取父项。这是正确的吗?

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);
 }
}
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();

}

您可以将项目添加到 parentItem,然后像这样编辑新项目:

using (new SecurityDisabler())
{
    Item newItem = parentItem.Add("name", template);
    newItem.Editing.BeginEdit();
    newItem["Author"] = txtAuthor.text;
    newItem["CommentText"] = txtComments.Text;
    newItem.Editing.EndEdit();
}

您可以在生产中更安全地使用 UserSwitcher,但您也可以使用 SecurityDisabler using(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();
    }
 }

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();
  }
}