Developer forum

Forum » Templates » Item relation list returns a single value instead of an array of multiple values

Item relation list returns a single value instead of an array of multiple values

Pernille Mortensen
Reply
I would like to get all values from an item relation list that is part of a page item type, but I have a problem because the item relation list returns a single value instead of an array of all the items.
 
This is the code I use:
 
// Get all pages under a certain parent page
var products = Dynamicweb.Services.Pages.GetPagesByParentID([my_page_id]).Where(x => x.Active && x.Published);
 
// Loop through the pages in order to get values for each single page
foreach(var product in products){
 
<span>Produktnavn:</span> @product.MenuText<br/>
<span>Træsort:</span> @product.Item["Tr_sorter"]
<br/><br/>
 
}
 
The last line (@product.Item["Tr_sorter"]) refers to a item relation list (where item source = paragraphs). It contains several items/paragraphs, but it only returns a single number - and I'm not sure how this number corresponds to the item - it is not the paragraph ID nor the item ID. I would expect it to return an array / multiple values.
 
How can I output a list of all the items/paragraps in the item relation list?

Replies

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Pernille,

That ID is the ID of the List. You will need to use the API to get the elements of that list, but keep in mind that the APi will return the IDs of the Items attached to that list. Which means you might need an additional conversion from ItemId to the actual Item before you can access the Item properties.

I have used something like this in the past:

var customList = ItemList.GetItemListById(customListID);

Combined with :

foreach(var item in customList){
 Dynamicweb.Content.Items.Item tempItem = Dynamicweb.Content.Items.Item.GetItemById("DiscountsImages", item);
}

There might be better alternatives in newer versions of DW.

I hope this helps.

Adrian

 

 
Pernille Mortensen
Reply
Thank you very much for your answer. It makes sense that the number is the ID of the list.
 
I have tried you code but get an error when trying to loop through the list.
 
My code:
 
var treeList = Dynamicweb.Content.Items.ItemList.GetItemListById(treeListId);
 
foreach(var item in treeList){
<div>item</div>
}
 
It returns the template error: foreach statement cannot operate on variables of type 'Dynamicweb.Content.Items.ItemList' because 'Dynamicweb.Content.Items.ItemList' does not contain a public definition for 'GetEnumerator'
 
Any ideas what I'm doing wrong?
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Pernille,

I have given you incomplete information.

The foreach should be done against a Relations property:

foreach(var item in treeList.Relations){

...

}

I am sorry I missed that. I have not used it in a while :)

Adrian

 
Pernille Mortensen
Reply

Thank you. It works! :)

Here is the code I used in order to get the field values of my item relation list items (for anyone stumbling upon this thread in the future):

foreach(var item in treeList.Relations){

 // Convert itemEntry to Item
 Dynamicweb.Content.Items.Item myItem = new Dynamicweb.Content.Items.Item(item);
 // This is a field with the system name "Title"
var itemTitle = myItem["Title"]; 
 // This is a field with the system name "Image"
var itemImage = myItem["Image"];  
 
}
 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Pernille,

 

You should be able to do this

var customList = ItemList.GetItems(customListID);
foreach(var item in customList){
 // the 'item' var is already your item
}

 

That makes it simper than having to go through the relations and then get each individual item.

 

Best Regards,

Nuno Aguiar

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Nuno's alternative is a lot more elegant and efficient.

I will update my code to use his instead.

Thank you Nuno!

Adrian

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

You are welcome!

 

You must be logged in to post in the forum