2012-06-21 67 views
3

我想使用后面的代码创建一个项目到sitecore。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(); 
    } 
    } 
} 

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

我想到了2种方法。

方法1

抢,我创建了ItemID

我可以抓住Name,但Name可能会被复制。

方法2

在字段中填写创建Item

之前,但随后再次..我不知道如何做那些2种方法。

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

在此先感谢。

+0

可能值得一提的是,良好的做法通常是使用项目ID而不是路径。这样你的代码就不依赖于内容树中的IA。我觉得一般的性能也更好地使用本机的ID – geedubb

回答

5

方法item.Add()返回创建的项目,以便您的代码看起来应该是这样:

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

您也可以使这更错误的证据,像这样: '使用(新的编辑上下文(的newitem)){ // 变化领域这里 }' –

+0

@KamFigy我觉得这个职位仍然适用。 http://sitecore.alexiasoft.nl/2006/04/03/new-way-of-editing-items/这可能是古老的,但我从来没有见过EditContext从此时起就被正式推荐过。 –

1

下面是完整的代码,你可以用它来Create items programmatically based on template in sitecore

using Sitecore.Data.Items;  
// 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(); 
    } 
} 
相关问题