So I have an Ultimate Picker which lets me select nodes based on a Data Type which has a Media Picker and a Content Picker.
@foreach (var nodeId in @Model.intermalPromotions) { @nodeId; }
Returns a comma seperated list of nodes. From here I need to be able to create an image based on the media picker and a link based on a content picker.
As documentation is so slim on the ground can someone please help?
You probably need to split the comma separated list and loop through each node, then use Library.NodeById to get the referenced node. Then you can access it's properties from that. Something like this:
Does it give you any additional error info? You might be able to get more info by adding ?umbdebugshowtrace=1 to your querystring and checking the trace logs at the bottom for any errors.
Also it will fail if the current page doesn't have internalPromotions set, but you can fix that by wrapping in a check (see below).
No sadly absolutely no errors whatsoever other than the meaningless output I gave.
I have a Document Type with an alias of 'internalPromotion'. This has two properties 'internalPromotionBackground' (Type Media Picker) and 'internalPromotionLink' (Type Content Picker).
I'm able to select the related content in the backoffice, but can't figure out the Macro.
What about if you scroll down under "Trace Information" - any errors there?
What version of Umbraco are you using?
Also just a thought, maybe try this instead, perhaps there is some casting going on with that datatype:
if (@Model.HasProperty("internalPromotions")) { string[] nodes = @Model.GetPropertyValue("internalPromotions").Split(',');
foreach (string nodeId in nodes) { var curNode = Library.NodeById(nodeId);
if (curNode.HasProperty("yourMediaPickerAlias")) { var media = Library.MediaById(curNode.yourMediaPickerAlias); <img src="@media.umbracoFile" /> }
if (curNode.HasProperty("yourContentPickerAlias")) { var content = Library.NodeById(curNode.yourContentPickerAlias); <a href="@content.Url">@content.Name</a> }
Make sure you are wrapping the above in @{ } also...ex:
@{ if (@Model.HasProperty("internalPromotions")) { string[] nodes = @Model.GetPropertyValue("internalPromotions").Split(',');
foreach (string nodeId in nodes) { var curNode = Library.NodeById(nodeId);
if (curNode.HasProperty("yourMediaPickerAlias")) { var media = Library.MediaById(curNode.yourMediaPickerAlias); <img src="@media.umbracoFile" /> }
if (curNode.HasProperty("yourContentPickerAlias")) { var content = Library.NodeById(curNode.yourContentPickerAlias); <a href="@content.Url">@content.Name</a> }
That means the current page doesn't have a property called "internalPromotions" - have you double checked? Also I noticed in your first example you used intermalPromotions - is there a typo maybe?
Maybe you can start removing things line by line to see if you can determine where the error is. I don't know why you aren't getting better error messages. On 4.7.1 you should get good errors unless umbracoDebugMode is turned off (at least in my experience)
Try outputting the value of nodeId, it should be one of the IDs in your internalPromotions list. Also you can output the internalPromotions to make sure it's set...
Also are you sure that all of the node IDs listed in your tests are published? I think Ultimate Picker can work with unpublished data but Razor can only access published data.
That's strange. You could try making a test macro somewhere with just the two lines to isolate the problem. If it fails I suspect you have bigger problems :)
@{ var testNode = Library.NodeById(1234); // put the id of the node that's failing here... <p>@testNode.Name</p> }
If the nodeId is "1104" in the first example (have you verified that?) I don't see why it would work in #2 but not #1...there should be no reason for that. The only difference is it's a string in the first example, but you could test also like Library.NodeById("1104") but there should be no difference.
It sounds like we're either missing something simple or you have some other issues with your site. I don't know what type of site this is but is it possible to give me access temporarily? I would gladly take a quick look for you. If that's possible just hit me up on twitter...
In case anyone else is following this, we got it worked out via twitter, turned out the nodes selected weren't fully published (link to document was #) and this weren't available for Razor. Re-publishing each node fixed the issue.
Content Picker in Ultimate Picker?
So I have an Ultimate Picker which lets me select nodes based on a Data Type which has a Media Picker and a Content Picker.
Returns a comma seperated list of nodes. From here I need to be able to create an image based on the media picker and a link based on a content picker.
As documentation is so slim on the ground can someone please help?
Hi,
You probably need to split the comma separated list and loop through each node, then use Library.NodeById to get the referenced node. Then you can access it's properties from that. Something like this:
Hope this helps,
Tom
Thanks a lot for the reply. The syntax is not something I'm familiar with at all! I've pasted in:
But I get this error 'Error loading MacroEngine script (file: InternalPromotions.cshtml)'
Hi,
Does it give you any additional error info? You might be able to get more info by adding ?umbdebugshowtrace=1 to your querystring and checking the trace logs at the bottom for any errors.
Also it will fail if the current page doesn't have internalPromotions set, but you can fix that by wrapping in a check (see below).
-Tom
No sadly absolutely no errors whatsoever other than the meaningless output I gave.
I have a Document Type with an alias of 'internalPromotion'. This has two properties 'internalPromotionBackground' (Type Media Picker) and 'internalPromotionLink' (Type Content Picker).
I'm able to select the related content in the backoffice, but can't figure out the Macro.
Try checking your web.config - maybe you have DebugMode disabled which will hide the "real" errors. Check that this key is set to true:
Also did you try appending the ?umbdebugshowtrace=1 to see if you get a better error?
-Tom
<add key="umbracoDebugMode" value="true" />
?umbdebugshowtrace=1 seems to do something...
Scroll down and look for any text in red, you should find something like Error Loading Razor Script, that hopefully has a "real" error message in it
Actually the get parameter just broke my existing javascript. The error message is
<div title="Macro Tag: 'InternalPromotions'" style="border: 1px solid #009;">Error loading MacroEngine script (file: InternalPromotions.cshtml)</div>
What about if you scroll down under "Trace Information" - any errors there?
What version of Umbraco are you using?
Also just a thought, maybe try this instead, perhaps there is some casting going on with that datatype:
Make sure you are wrapping the above in @{ } also...ex:
@{
<divclass="one"></div>
if (@Model.HasProperty("internalPromotions"))
{
<divclass="two"></div>
}
}
Outputs div one, but not div two?
That means the current page doesn't have a property called "internalPromotions" - have you double checked? Also I noticed in your first example you used intermalPromotions - is there a typo maybe?
Weird. Ok I've double checked and fixed the typo:
http://screencast.com/t/XRUxVfeIiB
@{
<divclass="one">one</div>
if (@Model.HasProperty("internalPromotions"))
{
<divclass="two">two</div>
}
}
I've also republished everything.
one and two is now output, which is a start!
Maybe you can start removing things line by line to see if you can determine where the error is. I don't know why you aren't getting better error messages. On 4.7.1 you should get good errors unless umbracoDebugMode is turned off (at least in my experience)
@{
if (@Model.HasProperty("internalPromotions"))
{
string[] nodes = @Model.GetPropertyValue("internalPromotions").Split(',');
foreach (string nodeId in nodes)
{
var curNode = Library.NodeById(nodeId);
<div class="two">s @curNode</div>
}
}
}
Ok a bit further... @curNode appears to be empty? The 3 divs are returned correctly but only contain the prefix 's'.
Try outputting the value of nodeId, it should be one of the IDs in your internalPromotions list. Also you can output the internalPromotions to make sure it's set...
in your loop:
Also I don't think you can just output the node, try @curNode.Name instead
-Tom
Internal Promotions: @Model.GetPropertyValue("internalPromotions")
@curNode.Name
In the loop is empty!
What version of umbraco are you using?
Also are you sure that all of the node IDs listed in your tests are published? I think Ultimate Picker can work with unpublished data but Razor can only access published data.
v4.7.1.1 yup everything is published! I'll try and publish it all again.
if (@Model.HasProperty("internalPromotions"))
{
string[] nodes = @Model.GetPropertyValue("internalPromotions").Split(',');
foreach (string nodeId in nodes)
{
var curNode = @Model.NodeById(nodeId);
<p>current node id: @nodeId</p>
<p>current node id: @curNode.Name</p>
}
}
So, @nodeId is correct, @curNode.Name is empty.
That's strange. You could try making a test macro somewhere with just the two lines to isolate the problem. If it fails I suspect you have bigger problems :)
@{
var testNode = Library.NodeById(1104); // put the id of the node that's failing here...
<p>@testNode.Name</p>
}
Where 1104 is the id of a file in the media library works correctly.
Are you testing with the same Node ID that is missing in your other example? I don't see why it would show there but not in your test
Yup.
foreach (string nodeId in nodes)
{
var curNode = Library.NodeById(nodeId);
<p>current node id: @curNode.Name</p>
}
Doesn't work.
foreach (string nodeId in nodes)
{
var curNode = Library.NodeById(1104);
<p>current node id: @curNode.Name</p>
}
Does work.
So the question is why on earth can't I pass in a variable containing an id, but can pass in the value directly.
I'm kind of at a loss here :)
If the nodeId is "1104" in the first example (have you verified that?) I don't see why it would work in #2 but not #1...there should be no reason for that. The only difference is it's a string in the first example, but you could test also like Library.NodeById("1104") but there should be no difference.
It sounds like we're either missing something simple or you have some other issues with your site. I don't know what type of site this is but is it possible to give me access temporarily? I would gladly take a quick look for you. If that's possible just hit me up on twitter...
-Tom
Sure!! My twitter is dominic_kelly.
Library.NodeById("1104") and Library.NodeById(1104) both work, whereas Library.NodeById(nodeId); does not.
In case anyone else is following this, we got it worked out via twitter, turned out the nodes selected weren't fully published (link to document was #) and this weren't available for Razor. Re-publishing each node fixed the issue.
whew... :)
is working on a reply...