Developer forum

Forum » Templates » Loop throug ItemRelations list on "external" item using the API ItemManager storage

Loop throug ItemRelations list on "external" item using the API ItemManager storage

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

I want to control the footer of a bootstrap based website, by having an itembasedpage where i create/edit the footercols in a list.

 

I have createt an page item - where the item is named "Footer" - and this Item has a field named "Cols" that is an itemrelationlist to another item, called "FooterCols". On the item FooterCols I have a richtext field, called "Content"

Now, I am trying to get the values of the populated list on my master.cshml template.

The "Footer" item has ID "1"

But I canĀ“t get it to work. Guess there is something wrong with my loop, or maybe i have to convert the footerobject to an array?

If I just outpoot the @footerloop variable, I get a number (336)

@{
 var footerloop="";
    var footerobject = Dynamicweb.Content.Items.ItemManager.Storage.GetById("Footer", "1");
}
   @if(footerobject != null){
      footerloop = footerobject["Cols"].ToString();
 
     
foreach(LoopItem i in GetLoop(@footerloop)){
       @i.GetString("FooterCols.Content")
     }
 
   }
   

 

Would really appreciate some help.

 

/Hans


Replies

 
Konstantin Landyshev
Reply
This post has been marked as an answer

Hi This is correct example

@{

    var footerobject = Dynamicweb.Content.Items.ItemManager.Storage.GetById("Footer", "1");

    if (footerobject != null)
    {
        var meta = Dynamicweb.Content.Items.ItemManager.Metadata.GetItemType(footerobject);
        var columnsField = meta.GetAllFields().Find("Cols");
        var editor = columnsField.CreateEditorInstance(false);
        var fieldValue = footerobject["Cols"];    
        var fieldViewModel = (List<Dynamicweb.Frontend.ItemViewModel>)editor.GetViewModelValue(fieldValue, columnsField);

        foreach (var column in fieldViewModel)
        {
            @column.GetValue("Content");
        }
    }
}

Best regards
Konstantin Landyshev
Frontend developer

Votes for this answer: 1

 

You must be logged in to post in the forum