Developer forum

Forum » CMS - Standard features » Can not show Item Relation List Field in QueryPublsiher

Can not show Item Relation List Field in QueryPublsiher

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

I have an ItemField of the type Item Relation List, that no mather what i do - i can not display it with the Query Publsiher

I assume the field should be of the type String[] ? I have tried by creating a field myself, and have also tried. - using the schema extender. @TemplateTags( ) displays all other fields I need, but not this one :/

Is this not possible to do, or am I missing something?

Br. Hans


Replies

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Hans,

I believe the ItemRelation field in this situation will probably render an int value that is the ID of the ItemList.

You need to use the API to get the Loop of items.

Something like this: 

ItemList payments = new ItemList();
payments = ItemList.GetItemListById(paymentLists);
And then, you will have to transform each element into an Item object using GetItemById:
foreach (var str in payments.Relations)
{
Dynamicweb.Content.Items.Item tempItem = Dynamicweb.Content.Items.Item.GetItemById("Payment",str.Id);
if (tempItem != null && tempItem["PaymentOption"] != null && tempItem["PaymentType"] != null && tempItem["PaymentType"].ToString() =="CC")
{
 creditCartPaymentID = tempItem["PaymentOption"].ToString();
 }
}

I hope this will help you move on.

Thank you,
Adrian

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi Adrian

Thank you, I will try that. How do you know what the ID of the ItemList is?

/Hans

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi Again Adrian

 

My template is like this:


@using Dynamicweb.Content.Items

@{
string minmynd="";}

@foreach (var i in GetLoop("QueryResultItem"))
    {
 
<text> @{
ItemList myndir = new ItemList();
myndir = ItemList.GetItemListById(FleiriMyndir);
 
 
  foreach (var str in myndir.Relations)
{
Dynamicweb.Content.Items.Item tempItem = Dynamicweb.Content.Items.Item.GetItemById("Myndir_Elements",str.Id);
if (tempItem != null && tempItem["Bruk_i_bokum"].ToString() == "Skipalistin")
{
 minmynd = tempItem["Mynd"].ToString();
 }
}
 
  }
</text>

    @i.TemplateTags()
    
    <a href="?id=@i.GetString("PageId")" class="col-md-3 col-3 col-lg-3 col-xl-3 col-sm-3">    
<img src="@minmynd" style="width:100%%; height:auto;" /><br />
<h2>@i.GetString("SSnavn")</h2>
</a>
    }
   


Where "FleiriMyndir" is the system name of the FIeld of the type Item Relation List, and the Item I am relating to, has the system name of "Myndir_Elements"

The value I am trying the fetch, is the SystemName "Mynd" - And I only want to fetch it, if the systemName "Bruk_i_bokum" has a value of "skipalistin"

 

 

But this gives me the error message :

 

Error compiling template "QueryPublisher/ListAllShips.cshtml"
Line 40: The name 'SkipCopy' does not exist in the current context

 

Anyone who can guide me on the right course?

 

/Hans

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Hans,

Based on the error you shared with us, I would say the error is in a different place. I don;t see any mention of SkipCopy in your code.

This type of error usually means that you are trying to use a variable (or object) that is not defined.

This usually is either a wrong suntax or a typo. Can also be a situation where your code block is closed improperly.
For example, here: 

myndir = ItemList.GetItemListById(FleiriMyndir); 

FleiriMyndir should be an Integer and I don't see it defined anywhere

And I would also avoid this approach:

<a href="?id=@i.GetString("PageId")" 

Maybe you can define a variable and use it instead of combining the code in the html tag

Another possible failure can be here:

tempItem["Bruk_i_bokum"].ToString() == "Skipalistin"

If tempItem["Bruk_i_bokum"] is null, you will get an error when trying to convert null to string.

I would check the code step by step in order to find the point of failure. Check if you get the right ItemList, then if the item list has nodes, then if the parameters are not empty.

I don;t see much use of the i variable that you have declared on the QueryResultItem Loop. If your list is not dependant on the loop, you can take it outside of the loop.

Of course these are just first glance suggestions. They might not solve your problem completely. But maybe they can guide you in the right direction.

Adrian

 

You must be logged in to post in the forum