Copied to clipboard

Flag this post as spam?

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


  • Johan 188 posts 380 karma points
    Sep 02, 2013 @ 16:05
    Johan
    0

    Get property in inline C#

    I'm trying to show content in my masterpage depending on a true/false property on my document type. I'm using inline C# and i know this looks like it would be easier to solve with a macro but with the full context it's not.

    I got this so far.

    <% if (umbraco.library.GetXmlNodeById("0").GetProperty("protectContent").Value.ToString() == "1") {%> 
        <pre>Showing protected content</pre>
    <%}%>
    

    and umbraco.library.GetXmlNodeById("0") is instead of $currentPage

    Update: This is the error message i recieve when running this code:

    Compiler Error Message: CS1061: 'System.Xml.XPath.XPathNodeIterator' does not contain a definition for 'GetProperty' and no extension method 'GetProperty' accepting a first argument of type 'System.Xml.XPath.XPathNodeIterator' could be found (are you missing a using directive or an assembly reference?)

  • Charles Afford 1163 posts 1709 karma points
    Sep 02, 2013 @ 18:58
    Charles Afford
    0

    Hi, what data are you trying to get?  And why are you using GetXmlNodeById

    Charlie :)

  • Bo Damgaard Mortensen 712 posts 1189 karma points
    Sep 02, 2013 @ 22:13
    Bo Damgaard Mortensen
    100

    Hi Johan,

    I think this should be solved by creating a macro, but I see you're already aware of this ;-)

    The error in your code is exactly what your error messages says: the System.Xml.XPath.XPathNodeIterator doesn't contains a method with the name "GetProperty".

    The return type of the umbraco.library.GetXmlNodeById() method is an XPathNodeIterator. What you'll want is to get a new Node object when in the context of ASP.NET. So, something like this should help you out:

    <%
         var node = new Node(1234);
         if(node.GetProperty("protectedContent").Value.ToString().Equals("1")
         {
            //Your output here
         }
    %>
    

    Without knowing anything about the context of this issue, I guess youøre already aware of the procedure of protecting content from members? :-)

    / Bo

  • Johan 188 posts 380 karma points
    Sep 03, 2013 @ 11:53
    Johan
    0

    Thanks Bo! The final code looks like this:

    <%if (umbraco.NodeFactory.Node.GetCurrent().GetProperty("protectContent").Value.ToString().Equals("1")) {%> 
            <pre>Showing protected content</pre>
    <%}%>
    

    I got the members part under control, thanx :)

  • Bo Damgaard Mortensen 712 posts 1189 karma points
    Sep 03, 2013 @ 20:48
    Bo Damgaard Mortensen
    0

    No problem :-) happy to help.

  • 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.

Please Sign in or register to post replies