Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Meni 277 posts 534 karma points
    May 26, 2013 @ 20:11
    Meni
    0

    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);
        <a href="@feature_news_item.Url">@feature_news_item.Name</a>

    and also

    var node = @feature_news_item.NodeById(Model.contentPicker.featureItemNews);
        <a href="@feature_news_item.Url">@feature_news_item.Name</a>

    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!

     

  • Charles Afford 1163 posts 1709 karma points
    May 26, 2013 @ 21:02
    Charles Afford
    0

    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

     

  • Meni 277 posts 534 karma points
    May 26, 2013 @ 21:38
    Meni
    0

    Hi Charlie,

    The Alias is featureItemNews

    I'm not familiar with the @umbraco.NodeFactory - is it external package or part of Umbraco?

  • Charles Afford 1163 posts 1709 karma points
    May 26, 2013 @ 21:41
    Charles Afford
    0

    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

  • Charles Afford 1163 posts 1709 karma points
    May 26, 2013 @ 21:44
    Charles Afford
    0

    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 :)

  • Meni 277 posts 534 karma points
    May 26, 2013 @ 22:24
    Meni
    0

    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?

  • Charles Afford 1163 posts 1709 karma points
    May 26, 2013 @ 22:38
    Charles Afford
    0

    Yes, should work.  Can i just ask your umbraco setup, i guess you are using MVC mode as opposed to web forms?  Charlie. :)

  • Meni 277 posts 534 karma points
    May 29, 2013 @ 13:26
    Meni
    0

    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

  • Charles Afford 1163 posts 1709 karma points
    May 29, 2013 @ 21:37
    Charles Afford
    0

    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.

  • Charles Afford 1163 posts 1709 karma points
    May 29, 2013 @ 21:40
    Charles Afford
    0

    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

     

  • Meni 277 posts 534 karma points
    May 31, 2013 @ 01:18
    Meni
    0

    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....

     

     

  • Charles Afford 1163 posts 1709 karma points
    May 31, 2013 @ 10:02
    Charles Afford
    0

    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 :)

  • Meni 277 posts 534 karma points
    Jun 06, 2013 @ 01:07
    Meni
    0

    Thanks Charlie!

    Now it's works!!

     

    :)

    Thank you so much for your help!

     

  • Charles Afford 1163 posts 1709 karma points
    Jun 06, 2013 @ 09:51
    Charles Afford
    0

    No problem, could you post the code? I would be intrested.  Glad you got it too work :).  Charlie

  • Meni 277 posts 534 karma points
    Jun 07, 2013 @ 03:07
    Meni
    0

    With pleasure my friend!

    At the top of the script I use 

    @using umbraco.NodeFactory;

     

    then

     

    Node The_article = new Node(1111);
    string MyArticleStr = The_article.GetProperty("featureItemNews").Value;
    int MyArticleID = Convert.ToInt32(MyArticleStr);
    var feature_story_HomePage = Model.NodeById(MyArticleID);

     

     

    and then I can use whatever I want - feature_story_HomePage.Url etc

Please Sign in or register to post replies

Write your reply to:

Draft