Developer forum

Forum » Development » How to get value from paragraph item field?

How to get value from paragraph item field?

Bjarne Rosendal
Reply

Do any of you guys have any experience of retrieving values from paragraph item fields using ItemManager.Storage.GetById?

I created an item type with system name SortedNewsList and one field called Dato (of type Date). This item type is only enabled for paragraphs:

http://screencast.com/t/MlDjqfmzfnMf

Now, I am using a Razor template for a news list to retrieve the value of the Dato field.

The context of the news list module rendering is a paragraph that is an instance of my paragraph item type just described.

The first lines of my Razor template look like this:

@{
var pageview = Dynamicweb.Frontend.PageView.Current();
var id = pageview.CurrentParagraph.ID;

var item = Dynamicweb.Content.Items.ItemManager.Storage.GetById("SortedNewsList", id.ToString());
var date = item["Dato"];
}

The first two lines are allright. The id variable holds the ID of the current paragraph.

Then I want to use that ID to retrieve the item ( line 3 ) and get the value of the Dato field ( line 4).

But when I try to render @date in the markup, I just get a null reference exception.


Replies

 
Jonas Mersholm
Reply

What is the indexcount in the item array? Is it simply 0 or are there actually elements in there?

 

If there is, could you try to iterate over them and check the key/value?

 
Mikkel Ricky
Reply
This post has been marked as an answer

You have to use an item id to get your item rather than the paragraph id:

@{
var paragraph = Pageview.CurrentParagraph;
if (paragraph != null && paragraph.ItemType != null && paragraph.ItemId != null) {
  var item = Dynamicweb.Content.Items.ItemManager.Storage.GetById(paragraph.ItemType, paragraph.ItemId);
  <pre>@Newtonsoft.Json.JsonConvert.SerializeObject(item, Newtonsoft.Json.Formatting.Indented)</pre>
}
}

Best regards,
Mikkel

 

Votes for this answer: 1
 
Bjarne Rosendal
Reply

Thanks Mikkel, that did the trick! :)

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Similar to this, I want to get the value of an item field - called "Image" - and is located on page ItemType called "NewsEditor", and I want to do this when I am on a PageItem called "NewsItem"

How can this be done?

/Hans

 
Nicolai Pedersen
Reply

Hi Hans

You can use the PageService to get the page by id and then access the Item property on the page instance: https://doc.dynamicweb.com/api/html/7a6e654f-5c75-9547-2cad-664e9dbc2672.htm

If you do not have a page, but an item id you can do like this: 

var item = Dynamicweb.Content.Items.ItemManager.Storage.GetById("NewsEditor", "123");

BR Nicolai

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi Nicolai

Thanx for your reply. Tried this, but outputting the variable item gives me this string "Dynamicweb.Content.Items.Item"

Is that correct? How do I fetch the value of a specific field on the Item I am fetching?

/Hans

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Hans

Like this:

var somevariable = item["FieldSystemName"];

BR Nicolai

Votes for this answer: 1
 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Brilliant Nicolai :)

That is exactly what i Need

Thank you very mutch

/Hans

 

You must be logged in to post in the forum