Developer forum

Forum » Templates » Access item relations list om page from paragraph using the API

Access item relations list om page from paragraph using the API

Charlotte Mogensen
Reply

Hi,

I have been trying to get out some values using the API - it didn't go well :).

 
Problem: How to extract item relation list properties (Link and LinkText) from a page settings item on a normal page to a normal paragraph template (where "normal" means non-item) using the API.

 

I have a page properties item, used on all pages on a website (attached in websites module as page settings item): 
System name: PageProperties 

One of the page properties is a list of links: 
Item type: item relation list 
System name: Links

The item relation list uses an itemtype called Links, item source: inline
For the Links item:
It has two properties: Link and LinkText
Item type: Item list 
System name: Links

 

I guess I have to extract the link properties via a loop using item list relation, but I can't get it to work. This is the values I found so far, but when it comes to the loop I can't get it to work:

 

int PageID = Convert.ToInt32(GetGlobalValue("Global:Page.Top.ID"));
string PAGEPROPERTYITEMID = Dynamicweb.Extensibility.ServiceLocator.Current.GetPageService().GetPage(PageID).PropertyItemId.ToString();
string ITEMLISTRELATIONITEMLISTID = Item.GetItemById("PageProperties", PAGEPROPERTYITEMID)["Links"].ToString();
 
 
 
Hope it makes sense...How to construct the loop to put out all the Link and LinkText values? 
 

Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Charlotte,

 

You have 2 ways of doing that.

 

If you're using ViewModels, it's simple enough:

var items = Model.Item.GetItems("SystemNameForTheItemRelationList");

foreach(var item in items) {
   // code here
}

 

If you're not using ViewModels you need to do a bit more work:

var item = Item.GetItemById(Pageview.Page.ItemType, Pageview.Page.ItemId);
var itemListRelationId = Convert.ToInt32(item["SystemNameForTheItemRelationList"]);
var items = ItemList.GetItemListById(itemListRelationId);

foreach(Item item in items) {
   // code here
}

This will give you a list of ItemEntry instead of Items, that's why in the foreach I need to use Item instead of var

 

For simplicity I am not checking for nulls, but you should. Hope this helps.

 

Best Regards,

Nuno Aguiar

 
Charlotte Mogensen
Reply

Hi Nuno and thanks for your help :)

I tried your non-viewmodel version, but I get some errors from the foreach:

1. 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'

2. A local or parameter named 'item' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

 

Guess error #2 is just a renaming issue, but what about #1? 

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Charlotte,

 

Yeah, my bad. I just typed the code by heart and made a couple of mistakes. Here's everything again so it's easier to get back to it in the future.

 

Using ViewModels:

var items = Model.Item.GetItems("SystemNameForTheItemRelationList");

foreach(var item in items) {
   // code here
}

 

Without ViewModels:

var item = Item.GetItemById(Pageview.Page.ItemType, Pageview.Page.ItemId);
var itemListRelationId = Convert.ToInt32(item["SystemNameForTheItemRelationList"]);
var itemRelations = ItemList.GetItemListById(itemListRelationId).Relations;

foreach(Item item in itemRelationss) {
   // code here
}

This will give you a list of ItemEntry instead of Items, that's why in the foreach I need to use Item instead of var

 

Try it now ;)

 

Best Regards

Nuno Aguiar

 
Charlotte Mogensen
Reply
I tried this:
 

@{
  var item = Item.GetItemById(Pageview.Page.ItemType, Pageview.Page.ItemId);
  var itemListRelationId = Convert.ToInt32(item["Links"]);
  var itemRelations = ItemList.GetItemListById(itemListRelationId).Relations;

  foreach(Item itemX in itemRelations) {
     // code here
  }
}

 

I get this error: 

System name of the target item cannot be an empty string.

Note: if I don't rename "item" in the foreach I get this error:

A local or parameter named 'item' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

 

There are 3 links in the item relations list, so 3 values in the itemlistrelation table, i.e. 3 x ITEMLISTRELATIONID

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply
This post has been marked as an answer

Hi Charltte,

 

I finally got a little more time. Here's 2 examples I managed to get working for Page properties

 

// Using Pageview
var pageItem = Pageview.Page.PropertyItem;
var itemListRelationId = Convert.ToInt32(pageItem["RelationList-SystemName"]);
var itemsFromPageview = ItemList.GetItemListById(itemListRelationId).Relations;
foreach(Item item in itemsFromPageview) {
    // @item["Field-SystemName"]
}

// Using Model
var itemsFromModel = Model.PropertyItem.GetItems(
"RelationList-SystemName");
foreach(var item in itemsFromModel) {
    //@item.GetString("Field-SystemName")
}

 

Notice the orange string, that needs to be replaced by your Page Property field name of type "Item relation list", and in blue the field name within that list.

 

Let me know how that works out for you.

 

Best Regards,

Nuno Aguiar

Votes for this answer: 1
 
Charlotte Mogensen
Reply

Yes it works now :) - thanks a lot!!

Have a nice weekend.

/Charlotte 

 

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

You are welcome

 

You must be logged in to post in the forum