classumbracoumbraco7app-code

Umbraco 7 generic node class


With the help of other Stackoverflow users, I have gone some way to my solution but have come to a halt.

I would like to build some generic classes in an app_code .cshtml file eg one would be to return property values from documents from a function eg

public static string docFieldValue(int docID,string strPropertyName){
    var umbracoHelper = new  Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    var strValue = "";
    try{
        strValue = umbracoHelper.Content(docID).GetPropertyValue(strPropertyName).ToString();   
    }
    catch(Exception ex){
        strValue = "Error - invalid document field name (" + strPropertyName + ")";
    }


    var nContent = new HtmlString(strValue);

    return nContent;

}

This works ok for returning one field (ie property) from a document. However, if I wanted to return 2 or more, ideally, I would store the returned node in a variable or class and then be able to fetch property values repeatedly without having to look up the document with each call ie without calling

umbracoHelper.Content(docID).GetPropertyValue(strPropertyName).ToString();

with different strPropertyName parameters each time, as I assume that will mean multiple reads from the database).

I tried to build a class, with its properties to hold the returned node

using Umbraco.Web;

using Umbraco.Core.Models;

...
public static Umbraco.Web.UmbracoHelper umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
public static IPublishedContent docNode;

...
docNode = umbracoHelper.Content(docID);

but this crashed the code. Can I store the node in a property on a class, and if so, what type is it?


Solution

  • First of all, using a .cshtml file is unnecessary, use a .cs file instead :-) CSHTML files are for Razor code and HTML and stuff, CS files are for "pure" C#. That might also explain why your last idea crashes.

    Second of all, UmbracoHelper uses Umbracos own cache, which means that the database is NOT touched with every request. I would at least define the umbracoHelper object outside of the method (so it gets reused every time the method is called instead of reinitialised).

    Also, beware that property values can contain all kinds of other object types than strings.

    EDIT

    This is an example of the entire class file - my example namespace is Umbraco7 and my example class name is Helpers:

    using Umbraco.Web;
    
    namespace Umbraco7
    {
        public class Helpers
        {
            private static UmbracoHelper umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            private static dynamic docNode;
    
            public static string docFieldValue(int docID, string strPropertyName)
            {
                docNode = umbracoHelper.Content(docID);
                return docNode.GetPropertyValue(strPropertyName).ToString();
            }
        }
    }
    

    This is an example how the function is called inside a View (.cshtml file inside Views folder):

    @Helpers.docFieldValue(1076, "introduction")
    

    Helpers, again, is the class name I chose. It can be "anything" you want. I've just tested this and it works.

    I suggest you read up on general ASP.NET MVC and Razor development, since this is not very Umbraco specific.