I'm having some trouble with getting a piece of content.
It's a textarea in the backend, where people can enter a piece of javascript.
I want to use that javascript in the website. And it was always just a matter of getting the stringvalue and placing it within the page. But now, there is a piece of javascript which contains a CDATA block. And that seems to mess up the XML Cache or something.
The solution I've found, was to retrieve the data like this:
public static string GetFieldFromDB(int pageId, string fieldName) {
umbraco.cms.businesslogic.web.Document document = new umbraco.cms.businesslogic.web.Document(pageId);
if (document == null || document.getProperty(fieldName) == null) {
return "";
}
return document.getProperty(fieldName).Value.ToString();
}
And this works fine, but Visual Studio keept hinting that this method is obsolete.
Warning 1 'umbraco.cms.businesslogic.web.Document' is obsolete: 'Obsolete, Use Umbraco.Core.Models.Content'
But how do I use Models.Content? It seems that it needs a lot more info to retrieve a document when I try this:
Umbraco.Core.Models.Content document = new Umbraco.Core.Models.Content()
From your views or surface controllers, you can use
Umbraco.TypedContent(pageId)
to get the published data.
It won't help you getting the CDATA section from javascript as it just doesn't work (been there, done that)
Got a workaround by implementing an extension method on IPublishedContent to replace {CDATA} and {CDEND} with the appropriate tags.
Of course, this solution requires the javascript in textarea is formatted as such (not sure if you can do it in your scenario)
<!-- Google Code for Remarketing Tag -->
<!--------------------------------------------------
Remarketing tags may not be associated with personally identifiable information or placed on pages related to sensitive categories. See more information and instructions on how to setup the tag on: http://google.com/ads/remarketingsetup
--------------------------------------------------->
<script type="text/javascript">
{CDATA}
var google_conversion_id = xxxxxxxxx;
var google_custom_params = window.google_tag_params;
var google_remarketing_only = true;
{CDEND}
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/xxxxxxxxx/?value=0&guid=ON&script=0"/>
</div>
</noscript>
Get propertydata directly from database
Hi there,
I'm having some trouble with getting a piece of content. It's a textarea in the backend, where people can enter a piece of javascript.
I want to use that javascript in the website. And it was always just a matter of getting the stringvalue and placing it within the page. But now, there is a piece of javascript which contains a CDATA block. And that seems to mess up the XML Cache or something.
The solution I've found, was to retrieve the data like this:
And this works fine, but Visual Studio keept hinting that this method is obsolete.
But how do I use Models.Content? It seems that it needs a lot more info to retrieve a document when I try this:
Any suggestions?
Jeroen,
From your views or surface controllers, you can use
Umbraco.TypedContent(pageId)
to get the published data.
It won't help you getting the CDATA section from javascript as it just doesn't work (been there, done that)
Got a workaround by implementing an extension method on IPublishedContent to replace {CDATA} and {CDEND} with the appropriate tags.
Of course, this solution requires the javascript in textarea is formatted as such (not sure if you can do it in your scenario)
and extension method
Thanks Dirk,
getting regular data isn't the problem, as I use Umbraco.TypedContent indeed.
But in this case, I can't ask the end-user to alter the javascript. They barely know what to paste in these fields :)
But if there is no other solution, I'll stay with the document option. That works like a charm, only Visual Studio telling me it's obsolete.
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.