Copied to clipboard

Flag this post as spam?

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


  • Jeroen Oostwouder 100 posts 296 karma points
    Jul 14, 2016 @ 11:37
    Jeroen Oostwouder
    0

    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:

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

    Any suggestions?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jul 14, 2016 @ 12:03
    Dirk De Grave
    0

    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)

    <!-- 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&amp;guid=ON&amp;script=0"/>
    </div>
    </noscript>
    

    and extension method

    public static string ReplaceCDataDeclarations(this string input)
    {
      return input.Replace("{CDATA}", "/* <![CDATA[ */").Replace("{CDEND}", "/* ]]> */");
    }
    
  • Jeroen Oostwouder 100 posts 296 karma points
    Jul 14, 2016 @ 12:12
    Jeroen Oostwouder
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft