Copied to clipboard

Flag this post as spam?

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


  • Malthe Petersen 6 posts 86 karma points
    Oct 26, 2016 @ 11:57
    Malthe Petersen
    0

    Get the Author of the blogpost

    Hello Umbraco Forum.

    I have this problem, where I need to get some properties from another documenttype.

    I have this blog, where I have seperatet subsites for the authors of the blog. When one of the authors write a blogpost, I need this author to be shown on the blogpost-promo.

    Image of the promo. enter image description here

    My problem is, how can I do this? I have tried with a partial, where i have a foreach for the blogpost and then for the authors and then tried to make an if-statement, that should only take that author that is equal to the author of the blogpost, but this does not work.

    Any suggestions?

    Regards Malthe

  • Casper 70 posts 308 karma points
    Oct 26, 2016 @ 12:51
    Casper
    0

    Hi,

    Is the author you are referring to a member("front end member") or a user("backend user")?

    If it is a backend user, take a look at this:

    int writerId = Model.Content.WriterId;
    string writerName = Model.Content.WriterName;
    
    Umbraco.Core.Services.IUserService  userService = ApplicationContext.Current.Services.UserService;
    
    Umbraco.Core.Models.Membership.IUser user = userService.GetUserById(writerId);
    

    If it is a member you propable have a member picker on the post, and then you need to go something like this to retrieve the member:

    int memberId = Model.Content.GetPropertyValue<int>("PROPERTY_ALIAS");
    
    IPublishedContent member = Umbraco.TypedMember(memberId);
    

    So lets say you already have your "featured posts" in a var, you would go for it like this:

    foreach (var featuredPost in featuredPosts)
    {
        int memberId = featuredPost.GetPropertyValue<int>("PROPERTY_ALIAS");
    
        IPublishedContent member = Umbraco.TypedMember(memberId);
    
        // use your member props...
    
    }
    

    Not sure this is what you are looking for, but hope it helps.

  • Malthe Petersen 6 posts 86 karma points
    Oct 26, 2016 @ 13:56
    Malthe Petersen
    0

    Thank you so much for answering!

    My authors are nor members or users, they are a documenttype. enter image description here

    I need a way to select one of these authors in the blogpost, and then display their image, name and position from the documenttype showed in the picture.

    Is there a way to do that?

    My blogpost partial looks like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    

    @{var node = Umbraco.TypedContent(1131); var blogpost = node.Descendants().Where(x => x.DocumentTypeAlias == "blogpost").Take(10);

    foreach(var blog in blogpost.OrderBy("CreateDate desc")){

    @blog.GetPropertyValue("promotekst")

    @Html.Partial("blogCreateForfatter") @* AUTHOR HERE IN A PARTIAL VIEW*@

               <div class="col-sm-4 col-xs-12">
               <a href="@blog.Url" class="btn btn-primary">Læs Mere   <span class=" fa fa-arrow-circle-right"></span></a>
               </div>
           </section>
           <img class="img-responsive" style="width:100%;" src="@blog.GetCropUrl("promoBilleder", "Blog Image")" alt="">
           </div>
           <div class="blogMainInfo">
              <section class="col-md-8 col-xs-12">
                 <h2>@blog.GetPropertyValue("blogTitle")</h2>
              </section>
              <section class="col-md-4 col-xs-12">
                 <div class="col-md-6 col-sm-6 col-xs-6">
    
                @blog.Parent.Name
    
    
                 </div>
                 <div class="col-md-6 col-sm-6 col-xs-6 text-center">
                    <p>
                       @(blog.GetPropertyValue
                       <DateTime>
                       ("createDateOld").ToString("dd/MM"))<br>
                       @(blog.GetPropertyValue
                       <DateTime>
                       ("createDateOld").ToString("yyyy"))
                    </p>
                 </div>
              </section>
           </div>
        </article>
    }                             
    

    }

    *Sorry that the code is not wrapped correctly.

    Regards Malthe

  • Casper 70 posts 308 karma points
    Oct 26, 2016 @ 15:11
    Casper
    100

    Hi,

    That should be easy. You can do it in 2 (or more) ways:

    First way - Create a property using Umbraco.ContentPicker datatype and give it an alias. Ex. "authorPicker" - now you can select any content you want on your post.

    That way kinda suck, because you will actually be able to pick any kind of content, and that could give errs if not handled in code.

    Second way - Create property using Umbraco.MultiNodeTreePicker datatype and give it an alias. Ex. "authorPicker". Then configure it to only allow documenttype "Forfatter/author or whatever you named that type". Then pick a starting node for the picker and set set minimum and maximum items to 1 - Now you have a picker that will start at given node and contrain pickable items to be of your "author type" multinodetreepicker

    Now loop your featured posts and fetch the author for each post.

    foreach(var featuredPost in featuredPosts)
    {
        var authorId = featuredPost.GetPropertyValue<int>("authorPicker");
        var author = Umbraco.TypedContent(authorId);
        if(!author.Equals(null)) 
        {
            // use author...
            @Html.Partial("blogCreateForfatter", author)
        }
    }
    

    Note im not really sure that retrieving the featuredPost "authorPicker" will work(it stores csv, but we only have one item, so maybe it will work). If not change it to string and all is good.

  • Casper 70 posts 308 karma points
    Oct 26, 2016 @ 15:31
    Casper
    0

    Your blogCreateForfatter partial could look like this:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    @{
        if (!Model.ContentType.Alias.InvariantEquals("forfatter"))
        {
            return;
        }
    
        // assuming authorImage is a media picer
        var imageId = Model.GetPropertyValue<int>("authorImage");
        var image = Umbraco.TypedMedia(imageId);
    
        if (image.Equals(null))
        {
            return;
        }
    
        <img src="@image.Url" alt="@image.Name" />
    
    }
    
  • Malthe Petersen 6 posts 86 karma points
    Oct 26, 2016 @ 20:31
    Malthe Petersen
    0

    Thank you so much! With a little bit of modification I made it work!

    Great help!

    Regards Malthe.

Please Sign in or register to post replies

Write your reply to:

Draft