Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Apr 08, 2014 @ 13:30
    Ismail Mayat
    0

    Best practice to get ipublished in class

    Hello,

    I have the following in a config service class:

                    UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);                
                var siteSettings = helper.TypedContent(siteSettingsNode);
    

    Is this the best way to get IPublishedContent in a class? I am doing this way so then i can make use of propertyconvertors dll by Jeavon http://our.umbraco.org/projects/developer-tools/umbraco-core-property-editor-converters

    Regards

    Ismail

  • Peter Gregory 407 posts 1611 karma points MVP 3x admin c-trib
    Apr 08, 2014 @ 13:33
    Peter Gregory
    1

    This is the way I have been doing it when I am outside of the context of a controller or usercontrol. I dont think there is a better way.

  • Mads Krohn 211 posts 501 karma points c-trib
    Apr 08, 2014 @ 13:36
    Mads Krohn
    1

    Unless something new was added in 7.1 that I'm not aware of, then yes, that is the way to get IPublishedContent.
    I'm not entirely sure how much overhead there is in creating an UmbracoHelper, but I tend to at least make sure I only create one once per request when needed.

    Cheers
    Mads

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Apr 08, 2014 @ 13:37
    Ismail Mayat
    0

    Mads and Pete,

    Awesome many thanks. With regards to performance its a singleton so will only be fired up once.

    Regards

    Ismail

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Apr 08, 2014 @ 13:38
    Jeroen Breuer
    0

    This topic has some more info about it: http://our.umbraco.org/forum/developers/api-questions/37894-new-Dynamic

    This is my solution which I prefer over the Umbraco helper: http://our.umbraco.org/forum/developers/api-questions/37894-new-Dynamic?p=5#comment152555

    Jeroen

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Apr 08, 2014 @ 13:48
    Ismail Mayat
    0

    Jeroen,

    var content = Umbraco.AssignedContentItem; //Umbraco is the UmbracoHelper class.
    

    Does not work unless i am missing some references or something?

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Apr 08, 2014 @ 13:50
    Jeavon Leopold
    101

    Yes, when UmbracoHelper was born, Shannon sent me a sample which was like this:

            if (UmbracoContext.Current != null)
            {
                var umbHelper = new UmbracoHelper(UmbracoContext.Current);
                //do something
            }
    

    I have been using it ever since

    Jeavon

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Apr 08, 2014 @ 14:05
    Jeroen Breuer
    1

    @Ismail that example should work if you have the UmbracoHelper available. 

    So this could also work:

    UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);  
    var currentPage = helper.AssignedContentItem;

    But why not use the other example?

    var currentPage = UmbracoContext.Current.PublishedContentRequest.PublishedContent;

    That will always return the current page and you don't need to create your own helper first.

    I always use it like this:

    private IPublishedContent CurrentPage
    {
        get { return UmbracoContext.Current.PublishedContentRequest.PublishedContent; }
    }

    Jeroen

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Apr 08, 2014 @ 14:14
    Jeroen Breuer
    0

    Some more info. I copied that from how the RenderMvcController also returns the current page: https://github.com/umbraco/Umbraco-CMS/blob/7.1.1/src/Umbraco.Web/Mvc/RenderMvcController.cs#LC45

    Jeroen

  • 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