Copied to clipboard

Flag this post as spam?

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


  • Anthony Dang 1404 posts 2557 karma points MVP c-trib
    Jan 29, 2013 @ 11:22
    Anthony Dang
    0

    new Dynamic(id) and new Node(id) equiv in v6

    In v4 we can get any node by doing:

    new DynamicNode(id) or new Node(id)

    You can do this in a user control, in a helper class, in a service etc and you don't have to pass around a context  etc.

    In v6 you can use UmbracoHelper.TypedContent(id) to get an IPublishedContent when you are in a View (and Controller?).

    I want to be able to get an IPublishedContent when I'm in a helper class, in a service etc. But UmbracoHelper is not available. To use UmbracoHelper you have to do this:

    new UmbracoHelper(UmbracoContext.Current).TypedContent(id)

    Is there a more concise way?

    I really love being able to just type new Node(id) or new DynamicNode(id)

     

     

  • Mads Krohn 206 posts 493 karma points c-trib
    Jan 29, 2013 @ 11:29
    Mads Krohn
    0

    Yes, I came to the same conclusion.

    I spent some time looking for a PublishedContentService but didn't found any.
    Then I tried to do new DynamicPublishedContent(id) with out much luck either.

    Would love a more concise/intuitive way of getting published content.

  • Sebastiaan Janssen 4831 posts 14195 karma points MVP admin hq
    Jan 29, 2013 @ 11:38
    Sebastiaan Janssen
    0

    http://our.umbraco.org/documentation/Reference/Mvc/querying 

    @Umbraco.TypedContent(1234) seems to be about right?

  • Sebastiaan Janssen 4831 posts 14195 karma points MVP admin hq
    Jan 29, 2013 @ 11:39
    Sebastiaan Janssen
    0

    Ah okay, never mind. I don't know this one, will have to defer to Shannon.

  • Anthony Dang 1404 posts 2557 karma points MVP c-trib
    Jan 29, 2013 @ 11:40
    Anthony Dang
    0

    We want to do this when we're not in a view or controller.

     

  • Shannon Deminick 1476 posts 4950 karma points hq
    Jan 29, 2013 @ 14:28
    Shannon Deminick
    0

    We have a PublishedContentStoreResolver singleton but this is not public yet as we're waiting to finalize some APIs before 6.1

    That said we can definitely make things more concise in a better way than creating your own UmbracoHelper. Would like to know where you are wanting to use this "outside of a view or controller" ? Is it code that is called from a view or controller or completely standalone ?


  • Mads Krohn 206 posts 493 karma points c-trib
    Jan 29, 2013 @ 14:41
    Mads Krohn
    0

    I dont have a specific use case, but I'm thinking dashboards/user controls, http handlers, events etc.

  • Shannon Deminick 1476 posts 4950 karma points hq
    Jan 29, 2013 @ 14:50
    Shannon Deminick
    0

    For things like Dashboards and UserControls we should  have a base class exposing an already existing UmbracoHelper property, just like we do in controllers, views (and soon base pages). This is the most ideal way to expose the UmbracoHelper which gives you access to all of the methods you need. For custom classes that are instantiated from any of these base classes it would be ideal to just construct these classes and pass in the already existing UmbracoHelper.

    However, for custom classes that are not instantiated from these base clasess such as http handlers, etc... I think the 'easiest' way would be to expose an UmbracoHelper property on the UmbracoContext. Then you can do UmbracoContext.Current.Helper.TypedContent(1234)

    Please note that the reason I mention re-using the UmbracoHelper property on types such as UserControls, Controllers, etc... is because the UmbracoHelper is not always in the context of the currently rendered Umbraco page, sometimes it's in the context of a different page like when you are rendering a partial view that inherits from UmbracoTemplatePage and passing in a custom IPublishedContent item as it's model, then the UmbracoHelper of that view is in the context of this custom IPublishedContent item, not the currently rendered Umbraco page.

  • Mads Krohn 206 posts 493 karma points c-trib
    Jan 29, 2013 @ 20:29
    Mads Krohn
    0

    UmbracoContext.Current.Helper.TypedContent(1234) sounds fine with me :)

  • Shannon Deminick 1476 posts 4950 karma points hq
    Jan 29, 2013 @ 21:55
    Shannon Deminick
    0

    Yup, will create a task for that. 

  • Mads Krohn 206 posts 493 karma points c-trib
    Jan 29, 2013 @ 21:57
    Mads Krohn
    0

    Sounds great :)

  • Anthony Dang 1404 posts 2557 karma points MVP c-trib
    Jan 29, 2013 @ 23:37
    Anthony Dang
    0
    UmbracoContext.Current.Helper.TypedContent(1234) still looks a bit long.


    Any chance of wrapping it in a static method like IPublishedContent.Get(1234) or PublishedXmlContent (1234)


    I imagine there are a lot of people who like getting a node by just using new Node(1234).

    HttpHandlers and modules being some of the places where it would be useful. I'm not sure if it's possible but lately had cases where I could have used it also in a WebApi method and in a thread. 


  • Shannon Deminick 1476 posts 4950 karma points hq
    Jan 29, 2013 @ 23:45
    Shannon Deminick
    0

    This is what I mean about using base classes... we wil just expose an UmbracoHelper direcly as a property so you don't have to do UmbracoContext.Current.Helper.TypedContent(1234)

    The only time you'd have to use that is in rare cases where you aren't using a base class for it. We can easily have a base class for a WebApi controller. Some of this stuff is just not implemented just yet but will be with feedback such as this.

    I'm not a fan of wrapping something like this in a static method since we'll manually have to replicate nearly every method on the UmbracoHelper inside this wrapped static class. People will then think that is the correct way to do things and then start asking why all those handy extension methods on UmbracoHelper that develoeprs ship with their packages are not available using this static way of accessing things. People will then also use this static object to do *all things* when they shouldn't, especially in views where the UmbracoHelper is contextual to the view's current IPublishedContentItem. If people were to by default use the static way, then they will have issues.

     

  • Mads Krohn 206 posts 493 karma points c-trib
    Jan 29, 2013 @ 23:45
    Mads Krohn
    0

    I don't mind the changes, I just dont like newing up a context.
    And yes, we are all used to newing up documents, media and nodes, but in my opinion, it always felt a bit ... wrong ...
    I do really like the new approach of getting content via a service/helper. 

    In views, the syntax is short and easy, I don't mind it being a bit longer for custom C# needs.

    It should be fairly easy for you to do:

    var helper = UmbracoContext.Current.Helper;

    ...

    var node = helper.TypedContent(1234)
  • Shannon Deminick 1476 posts 4950 karma points hq
    Jan 29, 2013 @ 23:47
    Shannon Deminick
    1

    @Mads, yup it is a very poor design to new-up an object in order to create a query to populate it :) As said, we can definitely make/enhance base classes for most all things that people will create.

     

  • Anthony Dang 1404 posts 2557 karma points MVP c-trib
    Jan 30, 2013 @ 10:37
    Anthony Dang
    0

    I agree that newing is not the best approach. I just want something more concise.

    "the UmbracoHelper is not always in the context of the currently rendered Umbraco page"

    So does this mean we shouldnt be passing around the helper, but always getting it from UmbracoContext ?

     

     

     

  • Mads Krohn 206 posts 493 karma points c-trib
    Jan 30, 2013 @ 11:07
    Mads Krohn
    0

    But when you use UmbracoHelper.TypedContent(1234) you most likely won't care about context, you just want some content. Perhaps, there is a structual issue in mixin context aware functions with non-context aware functions?

  • Shannon Deminick 1476 posts 4950 karma points hq
    Jan 30, 2013 @ 13:32
    Shannon Deminick
    1

    @Anth: you should pass around the helper as generally where you are passing it to will be in the context of where the Helper exists.

    What you shouldn't do is if we create the 'Helper' property on the UmbracoContext is use that *everywhere* when you don't need to.  For example, if we are rendering a page with ID 1234 and we lookup another doc and render a partial view like @Html.Partial("blah", Umbraco.TypedContent(9876)), the UmbracoHelper in the partial view will be in the context of content 9876, not 1234. If you then used the UmbracoContext.Current.Helper.Field("header") in your partial view, this would execute in the context of 1234 not 9876 since you are using the global singleton, not the Helper associated with the partial view.

    @All..

    So, now that I've ranted that off... Perhaps this idea of having a singleton at all for the Helper is a bad idea :P

    I think I have a better idea and it's actually part of something I was thinking about a while ago. We'll create a 'SearchContext' class which will contain all of the methods for retreiving content like: TypedContent, Content, TypedMedia, TypedSearch, etc... The UmbracoHelper methods will just wrap the SearchContext's methods, this allows for better seperation of logic anyways. Then we can make the Search context as a property on the UmbracoContext so you could do:

    UmbracoContext.Current.Search.TypedContent(1234)

    But we can then also create a static helper class to wrap the common methods like:

    Searcher.TypedContent(1234)

    and for methods that we don't wrap, since people will make extension methods on the SearchContext, we will just expose the context on the static object too so you can still access the context itself, like:

    Searcher.Context.SomeGuysExtensionMethod()

    Does that make sense? Also, what would be the name of this static objec ? 'Searcher', 'ContentSearch', 'ContentQuery', 'QueryStuff', etc.. ?

    Cheers,

    Shan

     

  • Anthony Dang 1404 posts 2557 karma points MVP c-trib
    Jan 30, 2013 @ 14:02
    Anthony Dang
    0

    Yeah singleton sounds like a definite no no now that you gave that example.

    I might be splitting hairs, but I wouldnt call it Searcher, as that implies just "searching". 

    Possibly:

    ContentService
    DataService
    NodeService

    ContentQuery
    DataQuery
    NodeQuery

    Queryer
    uQuery (yes may conflict with the other uQuery)
    ContentFetcher 
    DataFetcher 

    Or just call it Niels

     

     

     

     

  • Shannon Deminick 1476 posts 4950 karma points hq
    Jan 30, 2013 @ 14:09
    Shannon Deminick
    0

    lol :) well we can't call it 'Service' at all as this implies its the same as our data services in 6+

    The thing is that it is only for searching/querying but perhaps Query is a better term. This static object and SearchContext will contain only methods used to retreive documents from cache. ContentQuery could be the one...

     

  • Anthony Dang 1404 posts 2557 karma points MVP c-trib
    Jan 30, 2013 @ 14:44
    Anthony Dang
    0

    Well there's the ContentService which Neils blogged about which is a replacement for the Document API.

    Since this gets from the cache, then perhaps CacheQuery ? Or will that confuse people?

    I'd be ok with ContentQuery 

  • Mads Krohn 206 posts 493 karma points c-trib
    Jan 30, 2013 @ 22:33
    Mads Krohn
    0

    I must admit that I haven’t paid too much attention to the ongoing naming discussion, but I’m guessing that the original thought was to merge Document and Node into a more streamlined API, which makes total sense. Content and PublishedContent are good names and I think it’s easy to understand the difference.

    Now, to me, ContentQuery would make me think I was querying Content and not PublishedContent. I might be alone in this, but I actually like the thought of having a PublishedContentService. Even if you are restricted to loading and querying data I would argue that it’s still a service. Furthermore, it would align very well with the other services already there. It would be a single entry point to all aspects of CRUD in Umbraco. I can see that ContentService implements IContentService and IService, MediaService implements IMediaService and IService. So, PublishedContentService could implement IPublishedContentService and IService and so on.

    I am by no means an expert in designing API's, so there might be hundres of reasons for not going with a PublishedContentService. All I know is, that I found the ContentService fairly quickly, but I had to turn to Twitter (and Anthony) to find out, if I really had to new up an UmbracoHelper, which took me quite some time to find and understand. If there had been a PublishedContentService to begin with in the same place as the ContentService, I would have known where to start in seconds.

  • Shannon Deminick 1476 posts 4950 karma points hq
    Jan 30, 2013 @ 22:43
    Shannon Deminick
    0

    What people are asking for here is a static accessing point... The other services are singletons... Anth doesn't seem to like that and wants a shorter way to access it. Also we cannot expose this via the ApplicationContext because that is a non-web based global singleton, we need a web based and thread based singleton.

    Originally having UmbracoContext.Current.Helper or UmbracoContext.Current.Searcher was what I proposed but Anth (and perhaps others) want a shorter access point/syntax. Exposing the UmbracoHelper on a singleton is still not a great idea which is why I talked about the Search/QueryContext which i planned on making anyways which just exposes all methods to query/search published content. Then if we wrap this in a static object for shorter access we need to call it something... well shorter. There's no point in calling this something like: 

    PublishedContentQueryHelper.TypeContent(1234)

    that's really not a whole lot shorter than:

    UmbracoContext.Current.Searcher.TypeContent(1234)

    ... so not much point there.

    IMO, I'm still not a fan of exposing this via a public object anyways. Using it will hopefully be a rare occurance anyways once we create the base objects to work with.

  • Jeroen Breuer 4859 posts 12120 karma points MVP 3x admin c-trib
    Feb 20, 2013 @ 13:22
    Jeroen Breuer
    0

    There are situations where you can't use the UmbracoHelper because it needs the UmbracoContext. If you can use the UmbracoHelper while the UmbracoContext is null that would be great.

    Jeroen

  • Shannon Deminick 1476 posts 4950 karma points hq
    Feb 20, 2013 @ 14:46
    Shannon Deminick
    0

    @Jeroen, when is the UmbracoContext.Current null? This will only ever be the case in non-web applications. 

  • Jeroen Breuer 4859 posts 12120 karma points MVP 3x admin c-trib
    Feb 20, 2013 @ 14:54
    Jeroen Breuer
    0

    I think it's also null in the following situation:

    Simple json Controller:

    public class JsonController : SurfaceController
    {
        //The CurrentPage property doesn't work in this controller.
    
        [HttpPost]
        public ActionResult UpdateVariantAmount(int variantId, string amount)
        {
            //Update the amount based on what's passed from the jquery ajax call.
            //If the amount isn't an integer set it to 1. 
            int amountInt;
            if (int.TryParse(amount, out amountInt))
            {
                Quotation[variantId] = amountInt;
            }
            else
            {
                Quotation[variantId] = 1;
            }
    
            return new EmptyResult();
        }
    }

    Call the json with ajax:

    $.ajax({
        url: '/umbraco/Surface/Json/UpdateVariantAmount',
        type: 'POST',
        dataType: 'json',
        data: '{ "variantId": ' + variantId + ', "amount": "' + amount + '" }',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
        }
    });

    Add the url to the web.config to umbracoReservedUrls:

    <add key="umbracoReservedUrls" value="~/config/splashes/booting.aspx,~/install/default.aspx,~/config/splashes/noNodes.aspx,~/VSEnterpriseHelper.axd,~/umbraco/Surface/Json/UpdateVariantAmount"/>

    I use the SurfaceController for my json methods because I don't need to do any extra routing, but things like UmbracoHelper or CurrentPage don't work because it seems that the UmbracoContext.Current is null. Would be nice if I could still use the UmbracoHelper for things like the NiceUrl or TypedContent methods.

    Jeroen

  • Shannon Deminick 1476 posts 4950 karma points hq
    Feb 20, 2013 @ 15:00
    Shannon Deminick
    0

    Isn't that just this bug?

    http://issues.umbraco.org/issue/U4-1537

    Are you getting a YSOD? or what does "UmbracoHelper or CurrentPage don't work " actually mean?

    BTW you also don't need to add surface controllers or any other MVC routed controller to the ignore list. 

  • Jeroen Breuer 4859 posts 12120 karma points MVP 3x admin c-trib
    Feb 20, 2013 @ 15:24
    Jeroen Breuer
    0

    Hmm just did another test and the UmbracoHelper now works in my JsonController so perhaps it was that error :-p. So ignore what I said :-).

    If I don't add that url to the ignore list it gives a 404 when I try to do the ajax call. Same with bundles: http://stackoverflow.com/questions/13772029/some-of-my-asp-mvc-4-bundles-stop-working/13772083#13772083

    This is my complete umbracoReservedUrls:

    <add key="umbracoReservedUrls" value="~/config/splashes/booting.aspx,~/install/default.aspx,~/config/splashes/noNodes.aspx,~/VSEnterpriseHelper.axd,~/bundle/jquery,~/bundle/styles,~/umbraco/Surface/Json/UpdateVariantAmount"/>

    I added them after reading this: 

    http://our.umbraco.org/Documentation/Reference/Mvc/custom-routes

    Jeroen

  • Andreas Iseli 150 posts 427 karma points
    Feb 20, 2013 @ 15:24
    Andreas Iseli
    0

    UmbracoContext is also null when using WCF services without ASP.NET compatbility (perhaps for accessing data directly from the databas as my purpose was).

    When doing an ajax call on a "json" controller in my case the UmbracoContext is available, but in my case I call the controller directly on the current page. I think your problem is the specific routing, but i don't know why. As long as the HttpContext is available, the UmbracoContext shold be available as well. Just check if this.UmbracoContext is not null within the controller itself.

    I already started a feature request for such cases:

    http://issues.umbraco.org/issue/U4-1717

    Perhaps Shannon will have mercy with us :)

  • Shannon Deminick 1476 posts 4950 karma points hq
    Feb 20, 2013 @ 15:37
    Shannon Deminick
    0

    @Jeroen these two issues relate to automatically ignoring MVC/web api routes: http://issues.umbraco.org/issue/U4-1213, http://issues.umbraco.org/issue/U4-1166. I've tested this a bunch of times without issue plus all surface controllers are routed in the ~/umbraco path which is already excluded in the reserved paths. Not sure about the bundles though, that we might be able to auto ignore too but you'd have to log an task for that.

    @Andreas, The UmbracoContext gets created on BeginRequest in the UmbracoModule (http module). So running outside of a web app will mean that the singleton is not initialized. In the task you've mentioned that the HttpContext is not available, so how then would we contruct an UmbracoContext in that case? Also, what are you needing the UmbracoContext for in these cases? If it is database/business logic access, that is why we have the ApplicationContext.Current singleton which works outside of a web request.

  • Andreas Iseli 150 posts 427 karma points
    Feb 20, 2013 @ 16:04
    Andreas Iseli
    0

    @Shannon: Perhaps I'm missing something, but what I've tried is to get the URLs of a content object over the new content service using the UmbracoHelper. But the UmbracoHelper ist not available without UmbracoContext. Am I missing a logical component?

    I personally work often with SharePoint and there we always build the SPContext based on a faked HttpContext and that is officially support (SPContext.GetContext(HttpContext:instance)). Thats why I'm asking for it. I think I will have a deeper look into the Umbraco Context classes to understand the behavior between UmbracoContext and ApplicationContext as singleton. The idea is just simply to be able to create an UmbracoContext without being in the context of the web application. If this is something you don't want in your architecture because of several reasons, then just tell us that. I can arrange me :)

  • Shannon Deminick 1476 posts 4950 karma points hq
    Feb 20, 2013 @ 16:07
    Shannon Deminick
    0

    @Andreas, cool was just seeing what it is you were trying to acheive. The difference between ApplicationContext and UmbracoContext is ApplicationContext is a global application wide singleton whereas the UmbracoContext is a per-request/per-thread singleton which also utilizes an HttpContext. We can def add functionality to the API to create your own UmbracoContext but if its outside of the web request then it will have to be constructred by your own fake HttpContext... which can of course lead to other problems if your fake http context is not completely setup, but that'd be up to people to manage themselves. 

  • Anthony Dang 1404 posts 2557 karma points MVP c-trib
    Feb 20, 2013 @ 16:52
    Anthony Dang
    0

    "when is the UmbracoContext.Current null? This will only ever be the case in non-web applications. "

    Isnt it null in a different thread? I cant remember, but using node factory or DynamicNode didnt work in a thread that I spawn myself.

     

     

  • Shannon Deminick 1476 posts 4950 karma points hq
    Feb 20, 2013 @ 16:53
    Shannon Deminick
    0

    Yes if that thread doesn't come in on a web request. That thread will also have a null HttpContext.Current.

  • Anthony Dang 1404 posts 2557 karma points MVP c-trib
    Feb 20, 2013 @ 17:09
    Anthony Dang
    0

    Thought so. I had a case recenty where we spawned a new thread to push a media item up to a CDN. When it completed, we wanted the media item to be updated with the new CDN link. Of course, the thread couldnt use the Umbraco API, so we decided to expose a WebAPI method which the thread called.

  • Andreas Iseli 150 posts 427 karma points
    Feb 20, 2013 @ 17:23
    Andreas Iseli
    0

    @Shanon: This would be very nice :) And thanks for the explanation!

  • Tom 691 posts 908 karma points
    Mar 21, 2013 @ 02:20
    Tom
    0

    did anyone actually come up with what the current practice is in v6 to replace new Node(id)?

    We've got the same issue and there's no documentation anywhere on what has changed and what best-practice would be? we retrieve some nodes in helper classes using new Node(id) and post upgrading to 6.0.2 they're now returning null.

    Cheers,

    Tom

  • Shannon Deminick 1476 posts 4950 karma points hq
    Mar 21, 2013 @ 02:25
    Shannon Deminick
    0

    @Tom in what code are you trying to retreive an IPublishedContent instance ? Using UmbracoHelper is how you would look up IPublishedContent instances by id. There's some documentation here: http://our.umbraco.org/documentation/Reference/Mvc/querying

    In most places that you'll be executing like in controllers, you'll be inheriting from base classes that will contain an UmbracoHelper instance. Otherwise, you can create an UmbracoHelper for use. We haven't made base classes for everything on the planet but are working on it so please let me know in where you are trying to retreive an IPublishedContent item by id.

  • Tom 691 posts 908 karma points
    Mar 21, 2013 @ 02:26
    Tom
    0

    so in the example I'm trying to nut out would:

    var someNodes = new Node(1111).ChildrenAsList.Where(c => c.NodeTypeAlias == "someAlias");

    Be replaced with:

    var helper = new UmbracoHelper(UmbracoContext.Current).TypedContent(1111).Children.Where(c => c.DocumentTypeAlias == "someAlias");

     

     

     

     

  • Tom 691 posts 908 karma points
    Mar 21, 2013 @ 02:30
    Tom
    0

    Hi Shannon thanks for the reply.

    I'm in the web layer of my project in a provider-y kind of class where I'm basically pull out some data from the cms to use as part of an upgrade we're doing.

    At present we haven't moved everything across to controllers as we don't have the time so as a stop gap I'm fixing up some of the older code until we get a few sprints in.

    When new versions come out is there somewhere this common stuff is getting documented (just for future reference). I do like reading through the umbraco source but sometimes with a whole bunch of legacy conventions still in the source knowing what to use when and what is now obselete is getting increasingly confusing.

     

    Thanks for the help

  • Shannon Deminick 1476 posts 4950 karma points hq
    Mar 21, 2013 @ 02:33
    Shannon Deminick
    0

    In this case your best bet is to make a base class for your provider-y class that exposes an UmbracoHelper property. Then you could just do UmbracoHelper.TypedContent(1234)

    I try to document as much as I have time but unfortunately there's never enough time. If you want to track when new documentation is added you can just follow this repository: https://github.com/Umbraco/Umbraco4Docs - anytime this repo is updated after a few hours it will appear here: http://our.umbraco.org/documentation

  • Tom 691 posts 908 karma points
    Mar 21, 2013 @ 02:37
    Tom
    0

    Thanks Shannon but is the best way to retrieve the helper to be creating a new instance of UmbracoHelper with the current context?

    i.e.

    new UmbracoHelper(UmbracoContext.Current)  ?


    Thanks,

    Tom

  • Shannon Deminick 1476 posts 4950 karma points hq
    Mar 21, 2013 @ 02:38
    Shannon Deminick
    0

    Yeah, that's about the only way to do it currently we don't currently expose a way to create your own UmbracoContext but will do soon. That'd mostly be used for unit test or console apps though, in a web app you shouldn't need to create one.

     

  • Tom 691 posts 908 karma points
    Mar 21, 2013 @ 02:45
    Tom
    0

    UmbracoContext.Current in that instance is null and when i use it in a unit test it's a no go.

    Cheers,

    Tom

  • Tom 691 posts 908 karma points
    Mar 21, 2013 @ 02:47
    Tom
    0

    how would we then unit test that kind of functionality or should we be wrapping it in an interface for now? not having new Node has blown up a pile of tests

  • Shannon Deminick 1476 posts 4950 karma points hq
    Mar 21, 2013 @ 02:53
    Shannon Deminick
    0

    yeah, thats the problem mentioned earlier in this thread, were working on a public way to create one: http://issues.umbraco.org/issue/U4-1717

    until that's done you could use reflection hacks to set UmbracoContext.Current ... The only other option i can offer you right now is that I will publicize the EnsureContext method and you could use nightly builds but you'd probably have to wait till tomorrow. You'd still have to mock the HttpContext properly ... and that is at your own risk, if not everything is mocked that is required you'll get errors, though for the excercise of getting content the httpcontext shouldn't matter too much. We also have quite a good http context mock in our test project that should suffice.

     

  • Tom 691 posts 908 karma points
    Mar 21, 2013 @ 03:07
    Tom
    0

    no worries thanks I'll wait for the core implementation but for now just implement a quick interface for a node getter of some kind

  • Tom 691 posts 908 karma points
    Mar 21, 2013 @ 04:56
    Tom
    0

    Hi All,

    I've generated a stub for IPublishedContent but when trying to stub out GetPropertyValue<string> it's blowing up with Current has not been initialised.. Current has not been initialized on Umbraco.Core.PropertyEditors.PropertyEditorValueConvertersResolver. You must initialize Current before trying to read it.

     

    Is there any way around this?

    var returnNode = MockRepository.GenerateStub<IPublishedContent>();

    returnNode.Stub(n => n.GetPropertyValue<string>("someText")).Return("");

  • Anthony Dang 1404 posts 2557 karma points MVP c-trib
    Mar 21, 2013 @ 13:46
    Anthony Dang
    1

     

    I've created a little helper library

    https://bitbucket.org/anthonydotnet/uhelpsy

    This will work anywhere you have a web context:

    var publishedNode = IPublishedContentHelper.GetNode(int nodeId)

    I created a singleton of UmbracoHelper, then call .TypedContent(nodeId).

    I'm pretty sure it's ok to do this because all I use the singleton for is getting published content. Shannon, can you confirm/deny?

     

    Also, for the content service:

    var node = IContentHelper.GetNode(int nodeId)

    This is wraps ApplicationContext.Current.Services.ContentService.GetById(nodeId)


  • Anthony Dang 1404 posts 2557 karma points MVP c-trib
    Mar 21, 2013 @ 13:53
    Anthony Dang
    1

     

    I've created a little helper library

    https://bitbucket.org/anthonydotnet/uhelpsy

    This will work anywhere you have a web context:

    var publishedNode = IPublishedContentHelper.GetNode(int nodeId)

    I created a singleton of UmbracoHelper, then call .TypedContent(nodeId).

    I'm pretty sure it's ok to do this because all I use the singleton for is getting published content. Shannon, can you confirm/deny?

     

    Also, for the content service:

    var node = IContentHelper.GetNode(int nodeId)

    This is wraps ApplicationContext.Current.Services.ContentService.GetById(nodeId)


  • firepol 125 posts 173 karma points
    Jun 14, 2013 @ 16:37
    firepol
    0

    Coll stuff Anthony, I just copied your helpers and integrated in my project. Didn't find a way to get the IPublishedContent (by id) in my razor views... I wonder if something was added, but I could not find it in umbraco 6.1.1 ... so, thanks for your work!

  • Anthony Dang 1404 posts 2557 karma points MVP c-trib
    Jun 15, 2013 @ 16:03
    Anthony Dang
    0

    No sure what you mean by " Didn't find a way to get the IPublishedContent (by id) "

    Can you elaborate?

     

  • firepol 125 posts 173 karma points
    Jun 17, 2013 @ 10:35
    firepol
    0

    Hi Anthony, oh maybe you misunderstood me. I explain.

    If you created this helper, it means you also " Didn't find a way to get the IPublishedContent (by id) "

    Else why did you create this helper if you were able to? ;)

    What I meant is that in the umbraco library helpers and methods I didn't find any way to achieve this. Your helper solves this problem and I really appreciate it. So: good job mate, really thank your for this.

    Btw if in the meanwhile umbraco added sopmething, it would be cool to know. Not that I don't like to use this helper, but I'd like to know always the official way before using external packages/helpers etc.

    Cheers

  • Jeroen Breuer 4859 posts 12120 karma points MVP 3x admin c-trib
    Jun 17, 2013 @ 12:49
    Jeroen Breuer
    1

    Don't know if I posted this before, but in 6.1+ you can do this to get the current page:

    var content =  UmbracoContext.Current.PublishedContentRequest.PublishedContent;

    And you could also do this:

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

    Jeroen

  • Shannon Deminick 1476 posts 4950 karma points hq
    Jun 18, 2013 @ 02:20
    Shannon Deminick
    0

    It is still better to start using all supplied base classes for things like UserControls, Controllers, Pages, Views, etc.. that all expose an UmbracoHelper to work with data. It should be rare that you need to create an UmbracoHelper from scratch. I realize we need more documentation on this and that will all come soon and depends on the version you are using.

  • firepol 125 posts 173 karma points
    Jun 20, 2013 @ 10:37
    firepol
    0

    @Jeroen: thank you! I'm using umbraco 6.1.1 and the first command (inside of a view) did the job! Thank you.

    I don't know if this is documented somewhere, but I don't think it can be found here: http://our.umbraco.org/documentation/reference/

    Well anyway this forum rules!

  • Shannon Deminick 1476 posts 4950 karma points hq
    Jun 20, 2013 @ 10:38
    Shannon Deminick
    0

    @firepol, why do you need to reference that inside of a view, if you are using an Umbraco view you'll already have access to the current page (Model.Content) ?

  • firepol 125 posts 173 karma points
    Jun 20, 2013 @ 13:44
    firepol
    0

    @Shannon, because in my case my "view" (.cshtml ... sorry I didn't formulate well) is actually a partial view inside a form partial view (which has a FormModel instead of a standard RenderModel). Else yeah of course I could use Model.Content in my parent view...

  • Shannon Deminick 1476 posts 4950 karma points hq
    Jun 24, 2013 @ 02:22
    Shannon Deminick
    0

    @firepol, in that case it is better to pass in the model data you need to your partial view instead of relying on the parent view's model or a singleton .

    @Html.Partial("blah", new MyBlahModel { CurrentContent = Model.Content })

     

  • Damian Green 452 posts 1433 karma points
    Jul 10, 2014 @ 15:28
    Damian Green
    0

    I have come across this trying to get the bottom of something thats happening with the UmbracoHelper.

    I am using the PublishedContentModel (factory) and I have created an UmbracoHelper passing in UmbracoContext.Current (also tried ensure context). When i try and fetch another node using the Umbraco.TypedContent() passing in a different id - another instance of the current page is getting created instead of the once i asked for.

    More details here but its like its grabbing the current page and ignoring the id I've passed in.

    http://our.umbraco.org/forum/developers/api-questions/54381-Strange-behaviour-with-PublishedContentModel-and-UmbracoTypedContent

    Stumped.

  • Ian Grainger 65 posts 125 karma points
    Jun 12, 2015 @ 15:28
    Ian Grainger
    0

    I'm trying to use .Except on a DynamicPublishedContentList, but first of all I get an error using:

    var list = Umbraco.ContentSingleAtXPath("//THEPATH").Descendants("THETYPE");
    var list2 = list.Except(otherList);
    

    Error: 'Umbraco.Web.Models.DynamicPublishedContentList' does not contain a definition for 'Except'

    Oh, - so I try to use:

    var listStatic = new List<DynamicPublishedContent>(jobList);
    

    That allows me to use:

    listStatic.Except(otherList);
    

    But I can't work out how to create a list that will actually work with this Except method? It says it expects a DynamicPublishedContentList - but I can't see how to create one of those? The best I can do is:

    var otherList = idStrList.Select(id => (DynamicPublishedContent)Umbraco.Content(id)).ToList();
    

    But that's not the same as the mystical DynamicPublishedContentList that I apparently need.

    Can anyone help?

    Also - the subject of this post was getting a dynamic node, but there's a lot of discussion about TypedContent - surely the OP was looking for Umbraco.Content(id), not Umbraco.TypedContent? (Umbraco == umbracoHelper, instance I think by the way).

  • Mads Krohn 206 posts 493 karma points c-trib
    Jun 12, 2015 @ 20:44
    Mads Krohn
    0

    Hi Ian

    This is quite an old thread, which version of Umbrco are you currently using?

    No matter the version, a few things to clear up.

    Umbraco.ContentSingleAtXPath() returns a dynamic. The Except() method is an extension method on IEnumerable

    Now, if I understand your code examples correct, you have two options.

    1: Keep using dynamics, but tell the compiler what types you are using when you need to use extension methods like Except().

    Example:

    var idStrList = new List<string> {"1050", "4339", "6559"}; // Not sure where this list comes from ?
    var otherList = (DynamicPublishedContentList)Umbraco.Content(idStrList);
    var list = DynamicPublishedContentList)Umbraco.ContentSingleAtXPath("//THEPATH").Descendants("THETYPE");
    var list2 = list.Except(otherList);
    

    2: Stop using dynamics and just use the typed versions. Later on you can always switch back to dynamics if you prefer that.

    Example:

    var otherTypedList = Umbraco.TypedContent(idStrList);
    var typedList = Umbraco.TypedContentSingleAtXPath("//THEPATH").Descendants("THETYPE");
    var typedList2 = typedList.Except(otherTypedList);
    var dynamicList = (dynamic)typedList2; // Back to dynamics
    

    Regarding the original question, no, it was not about getting a dynamic node, it was about both dynamic and typed, just a general question on how to query for them. At least, that is my understanding of it. But other than that, you are correct about the Umbraco helper :)

    Hopefully this will be of help, do let me know if things work out for you.

    Cheers
    Mads

  • Ian Grainger 65 posts 125 karma points
    Jun 14, 2015 @ 13:45
    Ian Grainger
    0

    But I think my confusion comes down to Except being included on the dynamic page of the dynamicnode cheatsheet for Umbraco v6. Has it been removed from version 7 (the version I'm using BTW!)?

    When I used the standard Except method from Linq, I couldn't get the two lists to have similar enough types - one was selected from Umbraco and the other was loaded from a set of IDs (the: // not sure where this list comes from?) - if the code: otherList = (DynamicPublishedContentList)Umbraco.Content(idStrList); is valid - then that's answered that question! I'll need to give it a try...

  • Mads Krohn 206 posts 493 karma points c-trib
    Jun 14, 2015 @ 21:58
    Mads Krohn
    0

    Both versions should indeed work :)

    And where did you find a cheatsheet with Except included ?

    Cheers
    Mads

  • Ian Grainger 65 posts 125 karma points
    Jun 15, 2015 @ 05:56
    Ian Grainger
    0

    The Umbraco v6 cheatsheet - the first page, DynmaicPublishedContent has Except in the filtering and ordering extensions section.

  • Mads Krohn 206 posts 493 karma points c-trib
    Jun 24, 2015 @ 21:06
    Mads Krohn
    0

    Alright, found it :)
    Did everything turn out ok ?

  • Ian Grainger 65 posts 125 karma points
    Jul 01, 2015 @ 08:34
    Ian Grainger
    0

    I think the only remaining annoyance is still not being able to use except - so I'm having to use where not ID contained in ID list. Which is less nice to read - but works I guess.

Please Sign in or register to post replies

Write your reply to:

Draft