Developer forum

Forum » Development » RE: ItemManager.Storage.GetById interate through itemlist

RE: ItemManager.Storage.GetById interate through itemlist

Jan Sangill
Reply

Hi,

 

I have an item I am fecthing here:

Item item = Dynamicweb.Content.Items.ItemManager.Storage.GetById(item_type, item_id);

Know in this I have a itemlist:

item["Indhold"]

How would one iterate through this list?

a simple foreach wont work, so somehow I need to cast it to something else or??

//jan

 


Replies

 
Mikkel Ricky
Reply
This post has been marked as an answer

The value of item["Indhold"] is a comma-separated list of item ids and you have to use the Items API to get the actuals items from this list.

You can do this using helper methods like these

@using Dynamicweb.Content.Items

@functions {
    IEnumerable<Item> GetItems(List<string> ids, string itemType)
    {
        using (var repo = ItemManager.Storage.Open(itemType))
        {
            return repo.SelectByIds(ids).OrderBy(i => ids.IndexOf(i.Id)).ToList<Item>();
        }
        return null;
    }

    IEnumerable<Item> GetItems(string ids, string itemType)
    {
        return GetItems(ids.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList<string>(), itemType);
    }
}

In your template you can then use GetItems like this

@foreach (var i in GetItems(item["Indhold"] as string, «item type of item list items»))
{
    …
}

Best regards,
Mikkel

Votes for this answer: 1
 
Jakob Westhausen
Reply

Works like a charm, thanks again Mikkel :)

 

You must be logged in to post in the forum