2013-09-24 100 views
-1

你好我想創建專用頁面供我在哪裏可以改變每個渲染項目的數據源值的私人使用。 我已經創建了下一個代碼,但它不會保存項目的任何更改。Sitecore更改渲染數據源

SC.Data.Database master = SC.Configuration.Factory.GetDatabase("master"); 
      SC.Data.Items.Item itm = master.GetItem(tbPath.Text); 
      if (itm != null) 
      { 
       // Get the sublayout to update 
       //string sublayout_name = txtSublayout.Text.Trim(); 
       //if (!string.IsNullOrEmpty(sublayout_name)) 
       { 
        // Get the renderings on the item 
        RenderingReference[] refs = itm.Visualization.GetRenderings(SC.Context.Device, true); 
        if (refs.Any()) 
        { 
         //var data = refs.Select(d=>d); 
         //refs[0].Settings.DataSource 
         var sb = new StringBuilder(); 
         using (new SC.SecurityModel.SecurityDisabler()) 
         { 
          itm.Editing.BeginEdit(); 
          foreach (var d in refs) 
          { 
           if (d.Settings.DataSource.Contains("/sitecore/content/Site Configuration/")) 
           { 
            var newds = d.Settings.DataSource.Replace("/sitecore/content/Site Configuration/", "/sitecore/content/Site Configuration/" + tbLanguage.Text + "/");         
            // sb.AppendLine(string.Format("{0} old: {1} new: {2}<br/>", d.Placeholder, d.Settings.DataSource, newds)); 
            d.Settings.DataSource = newds; 
           } 
          } 
          itm.Editing.EndEdit(); 
         }       
         //lblResult.Text = sb.ToString(); 

       } 
      } 
     } 

我該如何更改數據源? 謝謝

回答

3

您在這裏混合了Sitecore中的兩個不同的東西。

  • 是在運行時分配給渲染的數據源,當Sitecore的是呈現頁面
  • 分配給某個項目

的最簡單的方法來呈現細節的數據源達到我認爲你想要實現的目標,就是這個。

Item itm = database.GetItem("your item"); 
string presentationXml = itm["__renderings"]; 
itm.Editing.BeginEdit(); 
presentationXml.Replace("what you're looking for", "what you want to replace it with"); 
itm.Editing.EndEdit(); 

(我還沒有編譯和運行該代碼,但它應該相當多的工作,是)

1

您沒有將更改保存到外地。

使用LayoutDefinitation類來解析佈局字段,並且foreach所有設備定義和呈現定義。

然後finaly將LayoutDifinition提交到佈局字段。

SC.Data.Items.Item itm = master.GetItem(tbPath.Text); 
var layoutField = itm.Fields[Sitecore.FieldIDs.LayoutField]; 


LayoutDefinition layout = LayoutDefinition.Parse(layoutField.Value); 
for (int i = 0; i < layout.Devices.Count; i++) 
{ 
    DeviceDefinition device = layout.Devices[i] as DeviceDefinition; 
    for (int j = 0; j < device.Renderings.Count; j++) 
    { 
     RenderingDefinition rendering = device.Renderings[j] as RenderingDefinition; 

     rendering.Datasource = rendering.DataSource.Replace("/sitecore/content/Site Configuration/", 
      "/sitecore/content/Site Configuration/" + tbLanguage.Text + "/"); 
    } 
} 

itm.Editing.BeginEdit(); 
var xml =layout.ToXml() 

layoutField.Value = xml; 
itm.Editing.EndEdit(); 

的代碼不是testet,但事我必須在生產上copy事件

0

替換數據源如果任何一個尋找單行Linq的改變:(測試)

var layoutField = item.Fields[Sitecore.FieldIDs.LayoutField]; 
    if (layoutField != null) 
    { 
     var layout = LayoutDefinition.Parse(layoutField.Value); 
     if (layout != null) 
     { 
      foreach (var rendering in layout.Devices.Cast<DeviceDefinition>() 
            .SelectMany 
            (device => device.Renderings.Cast<RenderingDefinition>() 
            .Where 
            (rendering => rendering.ItemID == "RenderingYoulooking"))) 
            { 
             rendering.Datasource = "IDYouWantToInsert"; 
            } 
      layoutField.Value = layout.ToXml(); 
     } 
    }