Copied to clipboard

Flag this post as spam?

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


  • Anwarubed 2 posts 22 karma points
    Jun 10, 2020 @ 12:14
    Anwarubed
    0

    Get nodes created by specific user by query

    Hi Team,

    How can i get the nodes created by the specific user to show in a particular page on the website.

    I've different pages for every author in my website

  • Steve Morgan 1278 posts 4216 karma points c-trib
    Jun 11, 2020 @ 07:57
    Steve Morgan
    0

    Hi,

    I don't know how far you got with this but this razor example should work.

    v7:

    @{
        var homeNode = Model.Content.AncestorOrSelf(1);
    
        // replace the id with the user's ID
        var nodesByCreator = homeNode.DescendantsOrSelf().Where(x => x.CreatorId == 0);
    
        foreach(var curNode in nodesByCreator)
        {
            <h2>@curNode.Name - @curNode.CreatorId - @curNode.CreatorName</h2>
        }
    }
    

    v8

    @{
        var homeNode = Model.AncestorOrSelf(1);
        <h1>@homeNode.Name @homeNode.CreatorId</h1>
    
        var nodesByCreator = homeNode.DescendantsOrSelf().Where(x => x.CreatorId == -1);
    
        foreach (var curNode in nodesByCreator)
        {
            <h2>@curNode.Name - @curNode.CreatorId - @curNode.CreatorName</h2>
        }
    }
    

    Or using the Creator Name works however - though this is obviously isn't ideal, it will probably work for you but it will need an exact string match.

    @{
    var homeNode = Model.AncestorOrSelf(1);
    <h1>@homeNode.Name @homeNode.CreatorId</h1>
    
    var nodesByCreator = homeNode.DescendantsOrSelf().Where(x => x.CreatorName == "Steve Morgan");
    
    foreach (var curNode in nodesByCreator)
    {
        <h2>@curNode.Name - @curNode.CreatorId - @curNode.CreatorName</h2>
    }
    

    }

    UPDATED: to remove my reference to a non-existent bug in Umbraco.

    HTH

    Steve

  • Steve Morgan 1278 posts 4216 karma points c-trib
    Jun 11, 2020 @ 08:05
    Steve Morgan
    0

    Created bug report https://github.com/umbraco/Umbraco-CMS/issues/8275

    OK - more sleep required here. I didn't notice my user ID in my v8 instance was actually "-1" not "1".

    So my query returned nothing... I thought I'd hit a bug - no just an idiot!

    Thanks to Sebastiaan for spotting my stupid error.

  • 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