What is the correct Razor code for retrieving the data stored in the uComponents URL Picker? I want to use a conditional statement to check what type of link and whether it opens in a new window, but I can't find any examples that I have been able to use (or understand).
I would very mutch like to know how to do this ...
i found how to get the url, but i can't access the new-window or link-title like this, because razor does not recognize these xml elements with the '-' in the middlee.
so here is what i have so far:
var cs = Model.LinkToCustomerPage; var csUrl = cs.url; var csNewWindow = cs.new-window;
sadly, the first 2 lines work correctly, razor discovers the xml and makes it available trough '.' notation, so cs.url
but as soon as this '-' comes in, it stops working
I found a way to do it, but i'm not really sure this is the best way.
you can use xpath to fetch the correct element inside the xml content.
so, to correct my code in the above post:
varcs =Model.LinkToCustomerPage; varcsUrl =cs.url; // this way can be used for url but not for new-window because of the '-' in new-window. var NewUrl = @cs.XPath("//new-window"); // returns 'True' or 'False' as a string.
The Sander Houttekier razor code doesn't work in Umbraco 4.7.1 and doesn't retrieve the correct value but give me the message "umbraco.MacroEngines.DynamicXml"
I have set the data format to JSON then used uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize method to get the UrlPicker data
var urlPicker = uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize(source.UrlPicker); var title = urlPicker.Title var mode = urlPicker.Mode // returns 'Content' var nodeId = urlPicker.NodeId var newWindow = urlPicker.NewWindow // returns 'false'
How did you even get the access to uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize method? What do I need to add into References (i assume)?
It's actually simpler than that. In 4.7.1, I'm able to access the properties with Razor in the following fashion (when the property alias is urlPicker):
@Model.urlPicker.mode
@Model.urlPicker.linktitle
@Model.urlPicker.url
@Model.urlPicker.nodeid
@Model.urlPicker.newwindow
Just make sure that the data format is set to Xml or @Model.urlPicker will return a string instead of an umbraco.MacroEngines.DynamicXml object.
I want to use the url picker for a media Image this is my code. My document property is always null or gives me a string like this: Truehttp://www.google.nl/ what is wrong? This code works well in the content nodes.
After upgrading a site from 4.7.2 to 4.9.0 (and uninstalling uComponents v3 and installing uComponents v5 beta), we found our razor script crashing, with the following message:
'uComponents.DataTypes.UrlPicker.Dto.UrlPickerState' does not contain a definition for 'url'
The fix was surprising: we capitalized "url" to "Url" and it started working again. (That is, we changed @node.myUrlPicker.url to @node.myUrlPicker.Url)
After upgrading to Umbraco 4.9 and uComponents 5 I was struggling to get the Multi-Url Picker values, but if you have Razor Model Binding set then the following works:
@Pete, you'll need to include the "http://" in the url that you add in with the picker, otherwise it will think it's a relative url and try to navigate to it as if it is in your site.
Any ideas why this doesn't work or how I can get it to work? It's off a 4.7.1 site. The urlPicker isn't recognised. It's a multi-url picker of alias "magazineLink".
Are you getting an error message, or simply no output at all? Or are you getting output, but missing a piece of it?
(To see error messages and C# razor stack traces, you need to (1) enable debugging and tracing in Web.config, and (2) add ?umbDebugShowTrace=true to the end of your page URL.)
Without knowing your document type structure and property names, etc., I'm not seeing anything that jumps out at me. Although,... maybe .url should be .Url? These are case-sensitive I believe.
In any case, best of luck to you and I hope you get this resolved.
I was getting no output at all. However, due to time pressure I replaced the datatype with a text box. Not ideal, but had to get the site modification done.
But the compiler doesn't recognize the urlPicker property:
"Error222'Umbraco.Web.Models.PartialViewMacroModel' does not contain a definition for 'urlPicker' and no extension method 'urlPicker' accepting a first argument of type 'Umbraco.Web.Models.PartialViewMacroModel' could be found (are you missing a using directive or an assembly reference?)"
I'm working with an Umbraco 6.1.5 install an uComponents v5.5.0
Anthony, sorry I wasn't very clear with my code. "urlPicker" happens to be the name of our field/property that we use to store the URL Picker datatype.
It looks like your URL Picker field is named mediaArticleLink, so just replace "urlPicker" with "mediaArticleLink" in your above code.
I'm a little unclear what the if/then statement is doing, but regardless, just substituting the fieldname should help. Feel free to reply back if that doesn't solve it.
And Ali's right, you can swap "CurrentPage" for "Model" throughout any Umbraco 6+ code (I just forget to use that newer syntax).
Retrieve URL Picker data in Razor
What is the correct Razor code for retrieving the data stored in the uComponents URL Picker? I want to use a conditional statement to check what type of link and whether it opens in a new window, but I can't find any examples that I have been able to use (or understand).
I would very mutch like to know how to do this ...
i found how to get the url, but i can't access the new-window or link-title like this, because razor does not recognize these xml elements with the '-' in the middlee.
so here is what i have so far:
sadly, the first 2 lines work correctly, razor discovers the xml and makes it available trough '.' notation, so cs.url
but as soon as this '-' comes in, it stops working
so i would need tofind a solution for that too
best regards
Sander Houttekier
I found a way to do it, but i'm not really sure this is the best way.
you can use xpath to fetch the correct element inside the xml content.
so, to correct my code in the above post:
best regards
Sander Houttekier
Sander,
Here is how i am doing it
@if (Model.usefulLinks.ToString() != string.Empty)
{
<div class="sixcol">
<ul>
@foreach (var item in Model.usefulLinks.BaseElement.Elements("link"))
{
if (item.Attribute("type").Value == "internal")
{
<li><a href="@umbraco.library.NiceUrl(int.Parse(item.Attribute("link").Value))">@item.Attribute("title").Value</a></li>
}
else {
<li><a href="@item.Attribute("link").Value" rel="external">@item.Attribute("title").Value</a></li>
}
}
</ul>
</div>
}
Regards
Ismail
The Sander Houttekier razor code doesn't work in Umbraco 4.7.1 and doesn't retrieve the correct value but give me the message "umbraco.MacroEngines.DynamicXml"
Anybody know the reason why ?
I have set the data format to JSON then used uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize method to get the UrlPicker data
var urlPicker = uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize(source.UrlPicker);
var title = urlPicker.Title
var mode = urlPicker.Mode // returns 'Content'
var nodeId = urlPicker.NodeId
var newWindow = urlPicker.NewWindow // returns 'false'
Hope this helps
It seems like the new-window and link-title nodes can be called by newwindow and linktitle
@Sean Dooley
How did you even get the access to uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize method? What do I need to add into References (i assume)?
@Carl Schéle
You shouldn't need to add any references if using the full uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize method like mentioned.
Below is an example of how I've used my sample above using a foreach loop on a MultiUrlPicker
var items = uComponents.Core.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState.Deserialize(Model.MultiUrlPicker).Items;
foreach(var item in items) {
if(item.Mode == uComponents.Core.DataTypes.UrlPicker.UrlPickerMode.Content) {
<p>@item.Title - @item.Mode - @item.NodeId - @item.Url - @item.NewWindow</p>
}
else if(item.Mode == uComponents.Core.DataTypes.UrlPicker.UrlPickerMode.Media) {
<p>@item.Title - @item.Mode - @item.NodeId - @item.Url - @item.NewWindow</p>
}
else if(item.Mode == uComponents.Core.DataTypes.UrlPicker.UrlPickerMode.Upload) {
<p>@item.Title - @item.Mode - @item.NodeId - @item.Url - @item.NewWindow</p>
}
else if(item.Mode == uComponents.Core.DataTypes.UrlPicker.UrlPickerMode.URL) {
<p>@item.Title - @item.Mode - @item.NodeId - @item.Url - @item.NewWindow</p>
}
}
Hope this helps
It's actually simpler than that. In 4.7.1, I'm able to access the properties with Razor in the following fashion (when the property alias is urlPicker):
Just make sure that the data format is set to Xml or @Model.urlPicker will return a string instead of an umbraco.MacroEngines.DynamicXml object.
Douglas Ludlow, thank you! Your four-month-old post is still pertinent, works great, and saved me much hassle! So easy, just not so discoverable!
I want to use the url picker for a media Image this is my code. My document property is always null or gives me a string like this: Truehttp://www.google.nl/ what is wrong? This code works well in the content nodes.
@inherits umbraco.MacroEngines.DynamicNodeContext
@using uComponents.Core
@using uComponents.Core.uQueryExtensions
@{
var mediaFolder = Model.MediaById(Parameter.Map);
@foreach (var n in mediaFolder.Children)
{
var media = n.umbracoFile.ToString();
var href = "";
var blank = "";
var urlPicker = n.url; //The Document property
if (urlPicker != null){
blank = (urlPicker.newwindow.ToString() == "True") ? "target=\"_blank\"" : "" ;
string mode = urlPicker.mode.ToString();
switch(mode){
case "Content":
href = Model.NodeById(urlPicker.nodeid).Url.ToString();
break;
case "Media":
case "Upload":
href = Model.MediaById(urlPicker.nodeid).umbracoFile.ToString();
break;
case "URL":
href = urlPicker.url.ToString();
break;
default:
href = "";
break;
}
}
}
}
I have figured it out :)
@inherits umbraco.MacroEngines.DynamicNodeContext
@using uComponents.Core
@using uComponents.Core.uQueryExtensions
@{
umbraco.cms.businesslogic.media.Media mediaFolder = uQuery.GetMedia(Convert.ToInt32(Parameter.Map));
<div class="rotate">
@foreach (var n in mediaFolder.Children)
{
var media = n.getProperty("umbracoFile").Value.ToString();
var href = "";
var blank = "";
var urlPicker = uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize(n.getProperty("url").Value.ToString()); //The Document property
if (urlPicker != null){
blank = urlPicker.NewWindow ? "target=\"_blank\"" : "" ;
string mode = urlPicker.Mode.ToString();
switch(mode){
case "Content":
href = Model.NodeById(urlPicker.NodeId).Url.ToString();
break;
case "Media":
case "Upload":
href = Model.MediaById(urlPicker.NodeId).umbracoFile.ToString();
break;
case "URL":
href = urlPicker.Url.ToString();
break;
default:
href = "";
break;
}
}
<a style="display: block;position: absolute; left: 0; top: 0;" href="@href" @Html.Raw(blank)><img src="@media" alt="" /></a>
}
</div>
}
For reference here's the codee I use to load a URL from the URLPicker.
I pass the alias of the URLPicker field and a Css Class into the macro.
@inherits umbraco.MacroEngines.DynamicNodeContext
@using uComponents.Core.DataTypes.UrlPicker.Dto;
@{
var propertyName = Parameter.URLPickerFieldAlias;
var link = UrlPickerState.Deserialize(Model.GetProperty(propertyName).Value);
string linkHTML;
linkHTML = string.Format("{3}",
link.Url,
link.NewWindow ? " target=\"_blank\"" : "",
string.IsNullOrWhiteSpace(Parameter.Css_Class) ? "" : " class=\"" + Parameter.Css_Class + "\"",
link.Title
);
}
@Html.Raw(linkHTML)
After upgrading a site from 4.7.2 to 4.9.0 (and uninstalling uComponents v3 and installing uComponents v5 beta), we found our razor script crashing, with the following message:
The fix was surprising: we capitalized "url" to "Url" and it started working again. (That is, we changed @node.myUrlPicker.url to @node.myUrlPicker.Url)
After upgrading to Umbraco 4.9 and uComponents 5 I was struggling to get the Multi-Url Picker values, but if you have Razor Model Binding set then the following works:
@foreach(var link in Model._footerLinks1.Items)
{
<li><a href="@link.Url" @Html.Raw(link.NewWindow ? "target=\"_blank\"" : "")>@link.Title</a></li>
}
I'm using this, but I keep getting the domain infront on the chosen url?
Any ideas?
its showing www.mydomain.com/www.google.com.
@Pete, you'll need to include the "http://" in the url that you add in with the picker, otherwise it will think it's a relative url and try to navigate to it as if it is in your site.
Any ideas why this doesn't work or how I can get it to work? It's off a 4.7.1 site. The urlPicker isn't recognised. It's a multi-url picker of alias "magazineLink".
Cheers,
Craig
Hi Craig,
Are you getting an error message, or simply no output at all? Or are you getting output, but missing a piece of it?
(To see error messages and C# razor stack traces, you need to (1) enable debugging and tracing in Web.config, and (2) add ?umbDebugShowTrace=true to the end of your page URL.)
Without knowing your document type structure and property names, etc., I'm not seeing anything that jumps out at me. Although,... maybe .url should be .Url? These are case-sensitive I believe.
In any case, best of luck to you and I hope you get this resolved.
I was getting no output at all. However, due to time pressure I replaced the datatype with a text box. Not ideal, but had to get the site modification done.
Cheers,
Craig
Capitalization of properties is key; the following Razor works in Umbraco 6.1.x with the URLPicker uComponent:
@Eric
I tried this in my Partial View Macro File:
if (Model.Content.GetProperty("mediaArticleLink").ToString() != String.Empty)
{
<a href="@Model.urlPicker.Url" title="@Model.urlPicker.Title" target="_blank">
@Model.urlPicker.Title
</a>
}
else{
<a href="@Model.urlPicker.Url" target="_blank">@Model.urlPicker.Url</a>
}
But the compiler doesn't recognize the urlPicker property:
"Error222'Umbraco.Web.Models.PartialViewMacroModel' does not contain a definition for 'urlPicker' and no extension method 'urlPicker' accepting a first argument of type 'Umbraco.Web.Models.PartialViewMacroModel' could be found (are you missing a using directive or an assembly reference?)"
I'm working with an Umbraco 6.1.5 install an uComponents v5.5.0
any help is much appreciated,
Anthony
solution is ( for the all the properties with dash) for umbraco 6 use CurrentPage and for v4 Model
Anthony, sorry I wasn't very clear with my code. "urlPicker" happens to be the name of our field/property that we use to store the URL Picker datatype.
It looks like your URL Picker field is named mediaArticleLink, so just replace "urlPicker" with "mediaArticleLink" in your above code.
I'm a little unclear what the if/then statement is doing, but regardless, just substituting the fieldname should help. Feel free to reply back if that doesn't solve it.
And Ali's right, you can swap "CurrentPage" for "Model" throughout any Umbraco 6+ code (I just forget to use that newer syntax).
This might help
http://refreshwebsites.co.uk/blog/get-data-from-ucomponents-multi-url-picker/
Regards
is working on a reply...