How to retrieve data from the Content Picker with Razor?
Hello
I try to use the Content picker w/o success..
I created a doc type which the properties are Content pickers, so what I try to do is along the magazine to use this page to pick up the features news / etc and display them on home page. w/o success..
I used something like:
dynamic feature_news_item = Library.NodeById(1111); (tried also the same but with Model: Model.NodeById(1111);
var node = @feature_news_item.NodeById(Model.contentPicker); <a href="@feature_news_item.Url">@feature_news_item.Name</a>
Now, my content picker property is featureItemNews
so I also tried
var node = @feature_news_item.NodeById(Model.featureItemNews);
Doesen't retrieve the feature_news_item.Url nor the feature_news_item.Name
Actually what I want to do is according to the information I get from the content picker to retrieve the properties from the page that the content picker choose and then to display the Page.title page.Image page.summary (title, image and summary are properties of typical news page in the magazine).
its part of core and has been for a very long time, if you refrence everything by there type then someone can easily tell what you are doing when you read it :). Charlie
Can't understand why it's so complicated... - just a simple property ...
Again, the property is "Content Picker" and the alias is "feature_article". What I try to do is to - first, at least as a begining to get the ID of the selected Node - which this also I can't do...
I use this [simple..] code:
var feature_story = Model.NodeById(1561); in order to get the right page (Node) which has this property. Now, when I try this:
var node = @Library.NodeById(feature_story.contentPicker); <a href="@node.Url">@node.Name</a>
Just get error, nor can't get the requested values (url, etc).
So I tried this option:
var node = @Library.NodeById(feature_story.feature_article); <a href="@node.Url">@node.Name</a>
and I even just tried to show to ID in order to check if it's work - guest what - error ...
something like int story1 = @feature_story.feature_article - gives error...
If I put the @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
than I get error because I'm using also dynamic ongoing_news = Library. -which cause an error
If I use however the @umbraco.NodeFactory.Node featureStoryNode I can save the file - not get error - however, can't display the script on the page I - get error on the output, someting like:
Error loading MacroEngine script (file
I'm confused already... - just want to use a simple Content Picker....
How to retrieve data from the Content Picker with Razor?
Hello
I try to use the Content picker w/o success..
I created a doc type which the properties are Content pickers, so what I try to do is along the magazine to use this page to pick up the features news / etc and display them on home page. w/o success..
I used something like:
Now, my content picker property is featureItemNews
so I also tried
and also
var node = @feature_news_item.NodeById(Model.contentPicker.featureItemNews);
Nothing...
Doesen't retrieve the feature_news_item.Url nor the feature_news_item.Name
Actually what I want to do is according to the information I get from the content picker to retrieve the properties from the page that the content picker choose and then to display the Page.title page.Image page.summary (title, image and summary are properties of typical news page in the magazine).
Not work ...
Any idea pls how can I do it?
Thanks!
Hi, not totally sure what going on up there.
What is your property name and alias? When you try to get content from the ContentPicker property XML is returned that needs to be parsed.
So if you do something like
@umbraco.NodeFactory.Node ctxNode = new umbraco.NodeFactory.Node.GetCurrent();
string contentPicker = ctxNode.GetProperty(PASSINCONTENTPICKERALIAS).Value;
contentPicker should now have the xml that needs to be parsed.
It might be that you get the ID, if you do then just create a new node and access the properties.
Hope this helps. Charlie
Hi Charlie,
The Alias is featureItemNews
I'm not familiar with the @umbraco.NodeFactory - is it external package or part of Umbraco?
its part of core and has been for a very long time, if you refrence everything by there type then someone can easily tell what you are doing when you read it :). Charlie
I am assumeing here you are using Razor and in a view or partial view? If you are in view or partial you can use
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
Then
@Model.Context.Current.... (This will be the node in context)
Charlie :)
Thx Charlie,
Actually I'm not using partial view - I'm just using simple Razor Script - Can I use it also in a Razor Script?
Yes, should work. Can i just ask your umbraco setup, i guess you are using MVC mode as opposed to web forms? Charlie. :)
Hi Charlie,
It's just not work...
Can't understand why it's so complicated... - just a simple property ...
Again, the property is "Content Picker" and the alias is "feature_article". What I try to do is to - first, at least as a begining to get the ID of the selected Node - which this also I can't do...
I use this [simple..] code:
var feature_story = Model.NodeById(1561); in order to get the right page (Node) which has this property. Now, when I try this:
var node = @Library.NodeById(feature_story.contentPicker);
<a href="@node.Url">@node.Name</a>
Just get error, nor can't get the requested values (url, etc).
So I tried this option:
var node = @Library.NodeById(feature_story.feature_article);
<a href="@node.Url">@node.Name</a>
and I even just tried to show to ID in order to check if it's work - guest what - error ...
something like int story1 = @feature_story.feature_article - gives error...
How do I use this property??? (Content picker...)
Thanks again
Ok the first thing to do is to stop using var. This is confusing. In my code above i have shown you how to get access to the context node :).
Have you put the: @inherits Umbraco.Web.Mvc.UmbracoTemplatePage at the top of your page?
@umbraco.NodeFactory.Node featureStoryNode = Model.NodeById(1561); //Get the right page (Node) which has this property
int id = featureStoryNode .getProperty("feature_article").Value
Try this to get the ContentPicker id.
Charlie :). Sorry for my late responce
I think i showed you how to do it above.
Basically what you need to do is:
Get the node as a Node not var.
Get the property on this node by alias (this will be the id) set it to an int variable
Pass the int variable into the GetNodeById(your int variable) // gets the node from the content picker
get the property from this node
Hi Charlie,
Thanks, but unfortunatelly stll doesn't work ...
If I put the @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
than I get error because I'm using also dynamic ongoing_news = Library. -which cause an error
If I use however the @umbraco.NodeFactory.Node featureStoryNode I can save the file - not get error - however, can't display the script on the page I - get error on the output, someting like:
Error loading MacroEngine script (file
I'm confused already... - just want to use a simple Content Picker....
Hi, why are you using dynamic? Do you really need this?
Can you post your code and i can tell you what you are doing wrong?
Charlie :)
Thanks Charlie!
Now it's works!!
:)
Thank you so much for your help!
No problem, could you post the code? I would be intrested. Glad you got it too work :). Charlie
With pleasure my friend!
At the top of the script I use
then
and then I can use whatever I want - feature_story_HomePage.Url etc
is working on a reply...