Copied to clipboard

Flag this post as spam?

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


  • bparks 23 posts 79 karma points
    Nov 25, 2014 @ 00:18
    bparks
    1

    Getting Content by Guid in v7

    How can I get a node by its Guid in v7.1+?  We'll be using Courier down the road, and I want to avoid issues with referencing nodes by their int id in Razor.  For example, instead of doing this:

    var menuRootNode = Umbraco.TypedContent(2363);

    I need to do this (but Guid isn't a supported argument):

    var menuRootNode = Umbraco.TypedContent(someGuid);

    Referencing this old post re: avoiding the int IDs when using Courier.

    http://our.umbraco.org/forum/umbraco-pro/courier/28391-Courier-transfer-of-content-node-has-two-different-IDs

    Thanks.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 25, 2014 @ 09:17
    Dennis Aaen
    0

    Hi bparks,

    Umbraco uses id´s instead of guid´s perhaps you should have look on the UmbracoHelpers, http://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/#TypedContentAtRoot%28%29

    Maybe the .TypedContentAtRoot() can help you to get the nodes that you wanted, insted of using a hardcoded id. The .TypedContentAtRoot() Returns the root IPublishedContent from the Content tree

    An example could be:

    @foreach (var child in Umbraco.TypedContentAtRoot().Children) { 
        <a href="@child.Url">@child.Name</a>
    }

    Hope this helps,

    /Dennis

  • bparks 23 posts 79 karma points
    Nov 25, 2014 @ 17:45
    bparks
    0

    Dennis, thanks for your response!
    While your approach would work, it doesn't seem like that's best practice for finding a node.  Here's a better example:

    I have a node called News Articles which has a number of News Article children.  If I'm writing a partial to get a list of News Articles, so that the partial can be reused to display News Articles in multiple locations throughout the site, I typically just use the int ID to get the parent node (News Articles). 

    However, with a Courier deployment, the node ID is not going to be the same when published to the production server, so we need to get the node by another means.

    Using the Umbraco Helpers, you can get the root node, as you suggest, and iterate to find the node (by name) that you're looking for.  Or, you can use TypedContentSingleAtXPath to get the node by path. But, both of these approaches are dependent on the knowing the name of the node -- if a content editor changes the name of the node, neither of these approaches will work (in my example, a content editor might decide that News Articles should be called Press Releases).  So, we need to get the node using a fixed node ID, and according to Per Ploug's response in the link I attached, that should be done using the node's uniqueID property...only I don't see how to get a node by uniqueID in v7...

     

  • bparks 23 posts 79 karma points
    Nov 25, 2014 @ 18:13
    bparks
    104

    Update:

    I just discovered that this is possible through the ContentService, so this should meet my needs:

    var contentId = new Guid("5AF4EF20-EA14-4A08-97DB-0CD3AA38DBF1");   
    var service = ApplicationContext.Current.Services.ContentService;
    var rootNode = service.GetById(contentId);

  • Arnold 3 posts 76 karma points
    Dec 06, 2016 @ 13:55
    Arnold
    1

    Your solution got too may high fives and will result in a badly performing application. You neglect the cache and make unnecessary roundtrips to your database.

  • Harvey 28 posts 122 karma points
    Mar 17, 2016 @ 14:12
    Harvey
    4

    I see that your last post is getting quite a lot of high fives, but I don't think you're using quite the right approach.

    If I understand correctly, the ContentService gets node information from the database directly rather than accessing cached data. This is far less performant than using something like Umbraco.TypedContent(id) which goes through the cache first.

    What the ContentService does is allow the setting of values to a node and with this, you can programmatically save that node (instead of using the Umbraco backoffice). This is the reason why the ContentService queries the database directly - because it needs the latest content for that node.

    EDIT: This is wrong: Please checkout Arnold's answer below on getting a node ID from a GUID I believe that it might be a recent development (Umbraco 7.3.x ?) but you can now use Umbraco.TypedContent(guid) with a guid. It should use the overflow that requires the object id property.--

  • Arnold 3 posts 76 karma points
    Dec 02, 2016 @ 15:26
    Arnold
    0

    Did anyone confirm the answer that Harvey gave? I am struggling with the same challenge. The page ids are going across the www and I want to prevent that people will "guess" for content using the REST service i build. Therefore i want to be able to query the cache for Guids.

  • lpalou 5 posts 85 karma points
    Dec 06, 2016 @ 13:41
    lpalou
    0

    What about the case that you don't know the Guid?

    What I need is to find the previous version of a node. Any ideas?

    Thanks!

  • Arnold 3 posts 76 karma points
    Dec 06, 2016 @ 13:53
    Arnold
    2

    It turns out that the GUID is not stored in the same cache as where the UmbracoHelper is looking.

    Either way, my problem was solved using the EntityService. The reason I can not work with pageId's is that they are used to support a REST service and pageId's can be guessed for too easy.

    private Guid GetGuidForId(int pageId) { var keyFor = ApplicationContext.Current.Services.EntityService.Get(pageId); if (keyFor != null) { return keyFor.Key; } return Guid.Empty; }

Please Sign in or register to post replies

Write your reply to:

Draft