Developer forum

Forum » Development » Loop itemrelationslist in itempublisher

Loop itemrelationslist in itempublisher

Christian Hansen Nørgaard
Reply

Hi DW.

Is it not possible to loop an itemrelationslist inside an itempublisher? When I loop my itempublisher I can get the fields directly from the item (like the Field.Id) but the looping of the itemrelationslist returns an exception "Object reference not set to an instance of an object". I can see tags like this:

  1. ItemPublisher:Item.Links:3
  2. ItemPublisher:Item.Links.IsRequired:false
  3. ItemPublisher:Item.Links.Name:"Links"
  4. ItemPublisher:Item.Links.SystemName:"Links"
  5. ItemPublisher:Item.Links.Type:"Item relation list"
  6. ItemPublisher:Item.Links.Value:3

foreach (LoopItem it in GetLoop("ItemPublisher:Items.List"))
{
    @it.GetString("ItemPublisher:Item.Field.Id")

    foreach (LoopItem item in it.GetLoop("ItemPublisher:Item.Links"))
    {
    }
}

Do I need to retrieve the itemrelationslist by some other means?


Replies

 
Christian Hansen Nørgaard
Reply

This makes no sense. I can see that ItemPublisher:Item.Links contains the number 3 and I can grab this if no other Items are present that has empty itemrelationslists.

@it.GetValue("ItemPublisher:Item.Links") or @it.GetInteger("ItemPublisher:Item.Links")

But as soon as a single item has nothing in the list I am getting "Object reference not set to an instance of an object" if I try to grab all of them. No checks has worked:

if (it.GetValue("ItemPublisher:Item.Links") != null)

if (it.GetInteger("ItemPublisher:Item.Links") != 0)

if (!String.IsNullOrEmpty(it.GetString("ItemPublisher:Item.Links")))

if (it.LoopExists("ItemPublisher:Item.Links"))

if (it.TagExists("ItemPublisher:Item.Links"))

if (it.GetInteger("ItemPublisher:Item.Links").Count() > 0)

The value looks like an empty string even though the working one has an integer? Howcome I can't create a check to see if the tag exist or even has any value?

 
Nicolai Pedersen
Reply

Hi Christian

I'll ask a developer to look into this question. Is this Dw8 or 9?

Thanks, Nicolai

 
Christian Hansen Nørgaard
Reply

This is Dw8. Thanks for looking into it.

 
Vladimir
Reply
This post has been marked as an answer

Hi Christian,

we will fix this in nearest hotfix for 9.8.1 - task 31581 is created

As temporary workaround:

                @{
                    var list = Dynamicweb.Content.Items.ItemList.GetItemListById(@GetInteger("ItemPublisher:Item.Links"));
                    if (list != null){
                        <ul>
                            @foreach (var itemEntry in list.Relations) {
                                var item = new Dynamicweb.Content.Items.Item(itemEntry);
                                <li>@item["Id"] - @item["Name"]</li>
                            }
                        </ul>
                    }
                }

Best regards,

Vladimir

Votes for this answer: 1
 
Christian Hansen Nørgaard
Reply

Hi Vladimir.

Thanks for your answer. I also found this quick fix in another forum post and I am making use of it for now although I am still getting the exception if my item relation list is empty.

I've got a problem with linking to a Product - the Item in the relation list also consists of a Product - since I don't know how to handle the URL:

string productID = item["Product"].ToString();
productID = productID.Replace("p_", "").Replace(":", "");
<a href="Product?ProductID=@productID">@item["ProductName"]</a>

Do you have any ideas for this approach?

 
Vladimir
Reply

hmm, looks good.... if it works....

I just want to note, that product field could contain several products, variants and even group
But if you sure that there only one main product here - all should be fine 

Best regards,

Vladimir

 
Christian Hansen Nørgaard
Reply

There is indeed only one product but it does'nt work. I think my URL is missing something because I am getting a 404. I only added Product to the URL (<a href="Product") to simulate what I wanted to do.. is there a more direct link to the product page?

 
Vladimir
Reply

It is strange.... looks correct. (don't forget area name, page name is "product" and located in root?)

Anyway, the most direct link is: Default.aspx?ID=@pageWithProductCatalogID&ProductID=@productID

 
Christian Hansen Nørgaard
Reply

Ah I am mistaken. I have 6 items which each has an item relation list and in these lists i have items with a product - each can belong to it's own group/category and product catalog (different pages). I've created my own Product.cshtml and placed it inside my design folder Files/Templates/Designs/MyDesign/eCom/Product/Product.cshtml.

I get your direct link but because of the difference in product catalogs i need it to be more dynamic.

 
Christian Hansen Nørgaard
Reply

I can now loop the item relationlist and retrieve the product as intended but since my item is on my frontpage the links for the product is pointing to that page (Default.aspx?ID=1) but I need it to point to the page with the EcomCatalog that has the products GroupID. With Ecom:Product.LinkGroup.Clean I can retrieve the GroupID but how do I get the PageID of the EcomCatalog?

I found this snippet but I can't get it to work properly:

foreach (Dynamicweb.Content.Paragraph para in Dynamicweb.Content.Paragraph.GetParagraphsBySql("SELECT * FROM Paragraph WHERE ParagraphModuleSystemName = 'eCom_Catalog'"))
{
    string groups = para.ModuleProperties.get_Value("ProductAndGroupsSelector");
    if (groups.Contains("g:" + Dynamicweb.eCommerce.Products.Product.Groups.get_Value(0).ID))
    {
        int pageid = para.PageID;
    }
}

So in short: I need to find which pageID has the eComCatalog where my GroupID exists and therefore also my Product so I can redirect properly.

 
Vladimir
Reply

Hi,

so complicated logic...
is each list has links to product of same type(from one group)? If so then you could add new field - link to product catalog where to display products and use it?
 

Best regards,

Vladimir

 
Vladimir
Reply

I don't understand the snippet row:
Dynamicweb.eCommerce.Products.Product.Groups.get_Value(0).ID

My variant:

var product = Dynamicweb.eCommerce.Products.Product.GetProductById(productId);

foreach(Group group in  product.RelatedGroups(true){  // 

     if (groups.Contains("g:" + group.ID)) ....

}

 
Christian Hansen Nørgaard
Reply

Yeah i know :(

Sadly no.. each list can contain products from different groups (and therefore different catalogs). Also I think my solution to call the database would be bad since I would have to make numerous calls instead of only once. I think my only option is to add a field to the item with the direct link to the product.

 

You must be logged in to post in the forum