Copied to clipboard

Flag this post as spam?

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


  • Nigel Wilson 945 posts 2077 karma points
    Jul 20, 2011 @ 06:28
    Nigel Wilson
    0

    Inline C# code to retrieve unique ID of node

    Hi there

    I was wanting to retrieve the unique ID for a node from the UmbracoNode table. This information is not stored in the umbraco.config file.

    So below is my code

    <msxml:script language="C#" implements-prefix="heat">
            <msxml:using namespace="System.IO" />
            <msxml:assembly name="System.Web" />
            <msxml:using namespace="System.Web" />
            <msxml:using namespace="umbraco.cms.businesslogic.web"/>
            <![CDATA[
            public string getUniqueID(string nodeID) {
                string uniqueID = "";  
                umbraco.cms.businesslogic.web.Document d = new umbraco.cms.businesslogic.web.Document(Convert.ToInt32(nodeID));
                try
                {
                    uniqueID = d.UniqueId.ToString();
                }
                catch
                {
                }
                return uniqueID;
             }
             ]]>
        </msxml:script>

    This however returns the following error when debugging my macro:

    Error loading XSLT ChangeRequests/ChangeRequestBPOProcessRequest.xslt
    The type or namespace name 'umbraco' could not be found (are you missing a using directive or an assembly reference?)

    Is this possible ? If so can someone advise assembly references I need.

    The alternative is to generate a unique ID and store on my node when created - this would then be a doddle to retrieve.

    I look forward to someone more knowledgeable than I sharing their wisdom.

    Cheers

    Nigel

     

  • Lee Kelleher 4025 posts 15835 karma points MVP 13x admin c-trib
    Jul 20, 2011 @ 07:37
    Lee Kelleher
    1

    Hi Nigel,

    Quick answer to the error is to add the assembly references too:

    <msxml:assembly name="businesslogic"/>
    <msxml:assembly name="cms"/>
    <msxml:assembly name="umbraco"/>

    ...

    Longer answer ... I advise that you don't use inline code snippets from within your XSLT - it is "bad practice" (read the comments of Doug's blog post about it).

    Easiest thing to do is moved the code to it's own class in the App_Code folder, marked with an XsltExtension attribute. (Ideally you'd want this as a compile DLL in the bin folder - but that's down to how comfortable you are with Visual Studio, etc).

    As for the function itself, I've been looking around for an existing code snippet - but couldn't find anything similar ... I'm tempted to add one to uComponents! ;-)

    Cheers, Lee.

  • Nigel Wilson 945 posts 2077 karma points
    Jul 21, 2011 @ 20:13
    Nigel Wilson
    0

    Hi Lee

    Thanks for the fantastic information - solves my small issues nicely.

    I will definitely look to "DLL" the functionality - I have a subscription to Umbraco TV at work so will take a look at that link there.

    Cheers again.

    Nigel

     

  • Lee Kelleher 4025 posts 15835 karma points MVP 13x admin c-trib
    Jul 22, 2011 @ 12:48
    Lee Kelleher
    0

    Hi Nigel,

    Just so you know for future reference, I've added this function to uComponents (will be in v2.2) - in the Nodes extensions.

    Cheers, Lee.

Please Sign in or register to post replies

Write your reply to:

Draft