Copied to clipboard

Flag this post as spam?

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


  • Duarte Carreira 47 posts 66 karma points
    Feb 01, 2011 @ 00:29
    Duarte Carreira
    0

    C# - list node's properties names and values

    Hi there.

    I'm having trouble getting properties names, and not aliases, from nodes in C#.

    I'm using node factory to get a node from its id. Then I can iterate its properties and get value and alias. But how can I get the name of the properties? These are human readable, so I'd like to use them in a table, like so:

    propname1 = propvalue1
    propname2 = propvalue2

    The big picture is to list search results. I'm getting the result.id and using it to get the node's properties.

    var umbNode = new umbraco.presentation.nodeFactory.Node(result.Id);
    string propValue = umbNode.GetProperty("propertyAlias1").Value;

    Any advice on how to do this?

    Thanks,

    Duarte

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 01, 2011 @ 10:23
    Sebastiaan Janssen
    0

    Just use:

    string propName = umbNode.GetProperty("propertyAlias1").PropertyType.Alias;

    But, since you're already specifying a string with the property alias, maybe you mean that you just want a list of properties and their values?

    This should work:

                var properties = umbNode.getProperties;
    foreach (var property in properties) { var name = property.PropertyType.Alias; var value = property.Value; // do something }
  • Duarte Carreira 47 posts 66 karma points
    Feb 01, 2011 @ 13:34
    Duarte Carreira
    0

    Hi Sebastiaan.

    I don't see the getProperties method nor property.PropertyType...

    I'm on 4.5.1.

    I have a reference to umbraco and umbraco.presentation.nodeFactory. Any idea why?

    Duarte

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 01, 2011 @ 13:42
    Sebastiaan Janssen
    0

    Try adding the cms.dll to your references and in your usings add umbraco.cms.businesslogic and umbraco.cms.businesslogic.web. 

  • Duarte Carreira 47 posts 66 karma points
    Feb 01, 2011 @ 16:45
    Duarte Carreira
    0

    My compiler output shows that I'm loading all dlls fine. I'm writing an ashx in umbraco's root, so all \bin assemblies are being loaded.But I have also now added <@Assembly> directives to be sure.

    I can only see getProperties on umbraco.cms.businesslogic.web.Document, and PropertyType on umbraco.cms.businesslogic.property.

    Nothing on Nodes... I can't see umbraco.presentation.nodeFactory.Property.PropertyType... nor Node.GetProperties, just Node.Properties. And  .Properties returns nodeFactory.Property which doesn't access the property name. Is there a cast or queryinterface I can do?

    Duarte

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 01, 2011 @ 16:54
    Sebastiaan Janssen
    0

    Oh I'm sorry, you're working with the nodeFactory! I was working with the umbraco Document object.

    The properties should be available like this (using umbraco.presentation.nodeFactory):

                var props = umbNode.Properties;
                foreach (Property property in props)
                {
                    var name = property.Alias;
                    var value = property.Value;
                }

     

  • Duarte Carreira 47 posts 66 karma points
    Feb 01, 2011 @ 16:58
    Duarte Carreira
    0

    ;)

    ok, so now back to the start... :)

    That's my original problem. I can only get the Alias which is really a shorthand version of the property name. I need the name to show the user in a list of results from a search. Do I have to use Documents (I was avoiding this since I saw comments about performance).

    Thanks for your patience.

    Duarte

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 01, 2011 @ 17:42
    Sebastiaan Janssen
    0

    Right, no problem we're getting there! 

    In order to get the description that you've set on the docType (as opposed to the alias), you'd have to access the document type, which means a hit to the database, you should probably cache your macro if this gets used a lot:

                var docType = DocumentType.GetByAlias(umbNode.NodeTypeAlias);
                foreach (var propertyType in docType.PropertyTypes)
                {
                    var propName = propertyType.Name;
                    var propValue = umbNode.GetProperty(propertyType.Alias).Value;
                }

     

  • Duarte Carreira 47 posts 66 karma points
    Feb 05, 2011 @ 20:55
    Duarte Carreira
    0

    Ok. So now I'm thinking I should go the xml route... So... with result.id I can use GetXmlNodeById, and access all properties names?

    Duarte

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 07, 2011 @ 10:36
    Sebastiaan Janssen
    0

    In XSLT you can simply do something like:

          <xsl:for-each select="$currentPage/data">
            <xsl:variable name="currentPropertyAlias" select="@alias"/>
            Name: <xsl:value-of select="$currentPropertyAlias"/><br />
            Value: <xsl:value-of select="$currentPage/data [@alias = $currentPropertyAlias]"/><br />
            <hr />
          </xsl:for-each>

    Of course, instead of $currentPage, you can put the node in there that you go through GetXmlNodeById.

  • Edwin van Koppen 156 posts 270 karma points
    Jul 02, 2014 @ 12:51
    Edwin van Koppen
    0

    Old post but i'm trying to make a object generator for Umbraco and i need to find the c# type of a PropertyType. Where can i find those?

Please Sign in or register to post replies

Write your reply to:

Draft