Retrieve custom property values from a content picker item
I have added a content picker to a document type. I would like to access the custom properties of the content item in the template but am having problems doing that.
I can get the ID of the content Item ...
@var id = CurrentPage.companyContentItem;
@var item = Umbraco.TypedContent(id);
When I get the "TypedContent" for the Id a "Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedContent" is returned.
I appear to be able to get the default properties from this type ...
@var name = item.Name //works
... but trying to get a custom property does not return anything.
@var customProperty = item.customProperty //does not work
How am I able to access the custom properties of a content item added to the content model via a "Content Picker"?
I think the issue is you're mixing the syntax for strongly typed and dynamic razor. When using "CurrentPage" instead of "Model" you're using the dynamic version. Therefore you need to use Umbraco.Content(id) - Then you should be able to fetch the custom property like you're trying above. So the full could should look like this
I just like add something to the great post Jan already has written. First of all I think that Jan missed to change TypedContent to Content in his example. And you should not have the @ in front of you variable. So the full could should look like this:
var id = CurrentPage.companyContentItem; var item = Umbraco.Content(id);
As you can see there are two MVC View Exampleexamples on how to get data from the picker, and it´s refers to the strongly typed syntaxt and the dynamics, as Jan has pointed out.
So your exmple with the check if the picker has a value on the current page, would look like this.
var id = CurrentPage.companyContentItem;
if (CurrentPage.HasValue("id")){ var item = Umbraco.Content(id); <a href="@item.Url">@item.Name</a> }
Don't be :) - It is confusing to figure out why where and how sometimes. But in short it's just two different ways of achieving the same thing. There are of course some differences and most hardcore c# developers in the community seem to prefer using strongly typed because it gives them more control of things where the dynamic approach has some limitations I think... (Somebody correct me if I'm wrong please :)).
But apart from that it's up to you to decide whether you're going to use the one or the other approach. I comes down the syntax you prefer writing I guess.
The dynamic approach is more concise than the strongly typed model approach - I think it might be worth running through Jeavons slides from the german umbraco festival earlier this year where he talks about Razor and the different syntaxes http://www.slideshare.net/JeavonLeopold/umbraco-oktoberfest-2014
Retrieve custom property values from a content picker item
I have added a content picker to a document type. I would like to access the custom properties of the content item in the template but am having problems doing that.
I can get the ID of the content Item ...
When I get the "TypedContent" for the Id a "Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedContent" is returned.
I appear to be able to get the default properties from this type ...
... but trying to get a custom property does not return anything.
How am I able to access the custom properties of a content item added to the content model via a "Content Picker"?
Thanks,
Gary
Hi Gary
I think the issue is you're mixing the syntax for strongly typed and dynamic razor. When using "CurrentPage" instead of "Model" you're using the dynamic version. Therefore you need to use
Umbraco.Content(id)
- Then you should be able to fetch the custom property like you're trying above. So the full could should look like thisYou might be able to benefit from using the "Razor cheatsheet" here https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets - It shows what one should use when doing either "Strongly typed" or "Dynamic". The first page displays the syntax for "Dynamics".
Hope this helps and makes sense.
EDITED: Corrected typo in code-sample (Stupid Copy/Paste!) :)
/Jan
If you are using TypedContent you will get a strongly type IPublishedContent object.
You can retreive the property like this with that:
Dave
Hi Gary and Jan,
I just like add something to the great post Jan already has written. First of all I think that Jan missed to change TypedContent to Content in his example. And you should not have the @ in front of you variable. So the full could should look like this:
When this is said, you can use the .HasValue method to check if the content picker has a value picked on the current page, and only display the content if this is the case. As Jan alredy has pointed out is that there are a syntax for strongly typed and dynamic Razor. Here is some documentation about the built-in Umbraco v7+ property editors https://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors-v7/ If you are taking your case, where you are using the content picker https://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors-v7/Content-Picker
As you can see there are two MVC View Exampleexamples on how to get data from the picker, and it´s refers to the strongly typed syntaxt and the dynamics, as Jan has pointed out.
So your exmple with the check if the picker has a value on the current page, would look like this.
Hope this helps,
/Dennis
I am actually embarrassed to ask ... when/how do I know when dynamic versus strongy-typed syntax should be/is being used?
Hi Gary
Don't be :) - It is confusing to figure out why where and how sometimes. But in short it's just two different ways of achieving the same thing. There are of course some differences and most hardcore c# developers in the community seem to prefer using strongly typed because it gives them more control of things where the dynamic approach has some limitations I think... (Somebody correct me if I'm wrong please :)).
But apart from that it's up to you to decide whether you're going to use the one or the other approach. I comes down the syntax you prefer writing I guess.
The dynamic approach is more concise than the strongly typed model approach - I think it might be worth running through Jeavons slides from the german umbraco festival earlier this year where he talks about Razor and the different syntaxes http://www.slideshare.net/JeavonLeopold/umbraco-oktoberfest-2014
Hope this helps a bit.
/Jan
is working on a reply...