2012-12-03 6 views
6

Mam niestandardowy przycisk sitecore, który zmienia szablon bieżącego elementu, dość prosty.Kopiowanie renderowania sitecore do nowego szablonu programowo za pomocą metody renderingDefinition.ItemId?

Jednak jako część tego staram się również migrować renderingi starego układu do nowego układu, jeśli jest to określony typ sublayout przez ItemId. Jednak zwracana wartość ItemId jest zawsze pusta, jedyną wartością, którą otrzymam z powrotem z RenderingDefinition, jest UniqueId.

Co robię źle?

Użyłem jako przewodnika this blog post.

Kodeks

public class ConvertToNewTemplateCommand : Command 
{ 
protected void Run(ClientPipelineArgs args) 
{ 
    if (!SheerResponse.CheckModified()) 
     return; 

    Item item = Context.ContentDatabase.Items[args.Parameters["id"]]; 
    if (args.IsPostBack) 
    { 
     if (args.Result == "yes") 
     { 
      //Get current layout details 
      var originalLayoutXml = item[FieldIDs.LayoutField]; 

      //Get new template 
      TemplateItem hubTemplate = Context.ContentDatabase.GetTemplate("some guid..."); 
      //Change template 
      item.ChangeTemplate(hubTemplate); 
      //Reset laytout 
      ResetLayout(item); 
      //Get reset layout 
      var newLayoutXml = item[FieldIDs.LayoutField]; 

      //Add all the module containers to the new layout in the central column 
      MoveModuleContainers(item, originalLayoutXml, newLayoutXml); 
     } 
    } 
} 

private void MoveModuleContainers(Item item, string oldXml, string newXml) 
{ 
    var oldLayout = LayoutDefinition.Parse(oldXml); 
    var newLayout = LayoutDefinition.Parse(newXml); 

    bool updated = false; 

    var oldRenderings = (oldLayout.Devices[0] as DeviceDefinition).Renderings; 
    var newRenderings = (newLayout.Devices[0] as DeviceDefinition).Renderings; 

    foreach (RenderingDefinition rendering in oldRenderings) 
    { 
     // Here is where the rendering.ItemID is always null 
     if (rendering != null && !String.IsNullOrEmpty(rendering.ItemID) && new Guid(rendering.ItemID) == new Guid("matching guid...")) 
     { 
      rendering.Placeholder = "middlecolumn"; 
      newRenderings.Add(rendering); 
      updated = true; 
     } 
    } 

    if (updated) 
    { 
        // Save item... 
      } 
} 
} 

Odpowiedz

6

mam na Sitecore support w końcu, który poinformował mnie, że powinienem użyć:

Sitecore.Data.Fields.LayoutField.GetFieldValue(item.Fields[Sitecore.FieldIDs.LayoutField]) 

zamiast:

item[FieldIDs.LayoutField] 

aby uzyskać Rzeczy prawidłowo poprawione: layoutField. Powoduje to, że wartości renderowania są poprawnie przetwarzane i, jak mówią, reszta jest historią.

+0

Niezły, Dan. Witamy w Stack Overflow! –

+0

+1 Skały wsparcia Sitecore! Dziękujemy za podzielenie się swoją opinią. –

+1

Chociaż ta odpowiedź dotarła do mnie w końcu (i uratowała mnie od wielu kłopotów, zastanawiając się, dlaczego źródła danych nie zostały zwrócone), jest nieco niekompletna. GetFieldValue jest tak naprawdę statyczną metodą w klasie Sitecore.Data.Fields.LayoutField. Zajęło mi to trochę czasu, aby to rozgryźć, więc mam nadzieję, że to uratuje następną osobę trochę zamieszania :) – Chris

Powiązane problemy