2

We have a Sitecore component that displays a list of blog posts. Part of this component is a placeholder where a "Featured Blog Post" component may be placed by editors manually. The "Featured Blog Post" component is a view rendering (not controller rendering) and uses a "blog post" as datasource.

We would like to be able to dynamically place the "Featured Blog Post" component dynamically based on a querystring in the URL.

So far, all I know how to do is get the querystring and check to see if it's a valid Sitecore ID and then if it is a valid blog post.

How would I then place the "Featured Blog Post" component on the page with the specified blog post as the datasource?

Here is an over-simplified version of what I have so far:

@{
    var featBlogPost = Request.QueryString["featpost"];
    var isFeatBlogPost = Sitecore.Data.ID.IsID(featBlogPost);
    if(isFeatBlogPost){
        var featBlogPostID = Sitecore.Data.ID.Parse(featBlogPost);
        var featBlogPostItem = Sitecore.Context.Database.GetItem(featBlogPostID);
    }
}

Sitecore Ver: 8.2

  • 2
    Wouldn't it be much easier as your featured blog post rendering would read the querystring and use that data to get the blogpost to render? – Gatogordo Jul 1 at 18:58
  • @Gatogordo There's not always a featured blog post on the page, so that won't work. – Michael Jul 2 at 19:04
2

You may need to statically add your rendering inside the container rendering, something like this would work inside the container rendering

@{
    var featBlogPost = Request.QueryString["featpost"];
    var isFeatBlogPost = Sitecore.Data.ID.IsID(featBlogPost);
    if(isFeatBlogPost){
        var featBlogPostID = Sitecore.Data.ID.Parse(featBlogPost);
        var featBlogPostItem = Sitecore.Context.Database.GetItem(featBlogPostID);
    }
}

@if(featBlogPostItem!=null){
  @Html.Sitecore().Rendering("/sitecore/layout/renderings/{YOUR RENDERING PATH}", new 
  {DataSource = "@featBlogPostItem.Paths.FullPath"})
}
else
{
 //You may need to default to the datasource item for the featured blog post
 }
  • Thank you! This worked almost perfectly. I just had to use DataSource = featBlogPostID instead of DataSource = "@featBlogPostItem.Paths.FullPath". – Michael Jul 2 at 17:45
3

I think your title is a little different than what you want to really achieve. But you could build a Controller Rendering, that looks at the current pages Querystring, to get it's Id, and use that to return data to your view.

public ActionResult Render() {

    string featBlogPost = Request.QueryString["featpost"];
    if (ID.TryParse(featBlogPost, ref ID postId)) {
        Item blogPostItem = Sitecore.Context.Database.GetItem(postId);

        return View(blogPostItem);
    }

    return new EmptyViewResult();
}

Now technically your Featured Post component wouldn't display if the querystring isn't present, and you could insert this on any given page as a dynamic rendering, and you should be all set and ready to go. I'd typically say you should worry about the experience editor and still display, when the user is in EE, but with the fact that the content varies based on the ID, I'm not sure it should be something you enable in this case.

Another option would be to hide the component using personalization. You would have to create a new rule condition to look at the page url and it's Querystring, but then you could define other rules for your component, so maybe it could still read from a datasource in addition to using the querystring value.

  • 1
    What about the HTML cache?.. (cache by querystring) personalization, with the rule engine, in my opinion, is the best solution, generic, no unclear code needed, much better to manage and cache. but for this a little bit unworkable if there are many Featured Blogs – Jan Bluemink Jul 2 at 8:26
  • What might you suggest for the title? I apologize for poor wording. – Michael Jul 2 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.