Developer forum

Forum » Templates » Get paragraph item values on module template?

Get paragraph item values on module template?

Jacob Storgaard Jensen
Reply

Hi,

I've got a paragraph item, where I've got a field which is the field type "ItemType". This WidthSettings item I reuse a lot and it's working fine as long as I fetch the values within my paragraph template context... When I, on a ItemPublisher template, try to get the value from one of the selectboxes within the parent item (WidthSettings.WidthMobile) it throws an error: "System.NullReferenceException: Object reference not set to an instance of an object."

This code doesn't work:

@{
    
    Dynamicweb.Content.Items.Item parentItem = Dynamicweb.Content.Items.ItemManager.Storage.GetById(Dynamicweb.Frontend.PageView.Current().CurrentParagraph.ItemType, Dynamicweb.Frontend.PageView.Current().CurrentParagraph.ItemId);
    string testvalue = parentItem["WidthSettings.WidthMobile"].ToString();
}
@testvalue

But this code does work and renders the text from a richtext editor field on the parent item:

@{
    
    Dynamicweb.Content.Items.Item parentItem = Dynamicweb.Content.Items.ItemManager.Storage.GetById(Dynamicweb.Frontend.PageView.Current().CurrentParagraph.ItemType, Dynamicweb.Frontend.PageView.Current().CurrentParagraph.ItemId);
    string testvalue = parentItem["Text"].ToString();
}
@testvalue

Is there a special way to get the values from fieldtypes "ItemType"?


Replies

 
Jacob Storgaard Jensen
Reply

I'll just answer my own question again... So behind the field type ItemType, there is a seperate item. The only value stored on our parent item is the ID of that "child" item. We can access that item and get the values we need from it:

@{

//So first we find the ID of the Item on our current paragraph

    Dynamicweb.Content.Items.Item parentItem = Dynamicweb.Content.Items.ItemManager.Storage.GetById(Dynamicweb.Frontend.PageView.Current().CurrentParagraph.ItemType, Dynamicweb.Frontend.PageView.Current().CurrentParagraph.ItemId);

//Then we get the ID of the Item that is created by the item fields that have field type ItemType

    string parentWidthSettingsItemId = parentItem["WidthSettingsContactPerson"].ToString();

//Then we get that particular item we just got the ID for

    Dynamicweb.Content.Items.Item parentWidthSettingsItem = Dynamicweb.Content.Items.ItemManager.Storage.GetById("WidthSettings",parentWidthSettingsItemId);
}

//Get the value/values you need
@parentWidthSettingsItem["WidthMobile"]
@parentWidthSettingsItem["SomeOtherValue"]
 
Nicolai Pedersen
Reply

You can get a job in our service desk :-)!

 
Jacob Storgaard Jensen
Reply

:-D

 

You must be logged in to post in the forum