How to “Pin” blog posts in Sitefinity without touching code

January 27, 2015 Digital Experience, Sitefinity

File this as a simple post to constructively while away a bit of time during the “Snowpocalypse” that is currently happening on the east-coast of the United States. Maybe winters in Seattle aren’t so bad? Comparatively…  

I was recently asked a question about how you would go about “pinning” a blog post to the top of a list of posts. My initial response was to suggest extending the Blogs Posts widget via code, but then it occurred to me that there might be an easier, more direct way.

Out of the box Sitefinity allows you to sort the List Settings of your Posts by “Last published”, “Last modified”, “By Title” and “As set in Advanced mode”. It’s the last one that we’re going to take a look at now.

 


First, let’s open up the Blogs module. If you do not already have a Blog setup, then you will need to create one now. Click on your Blog’s Title to go into its Posts section. On bottom right of the page click on the “Custom Fields for posts” link.



Add a new Yes/No type field and give it a name. I called mine “Featured”, but call it whatever you’d like.



Whenever you create a new Post or edit an existing one you will now see a checkbox field labeled “Featured”.



If you haven’t already, then add a Blog Posts widget to a Page. Once the widget is on the page click its “Edit” button. Now select the “List Settings” tab, set “Sort blog posts” to “As set in Advanced mode” and then click the “Advanced” button.



From within Advanced menu click the “Control Definition” button --> “Views” button --> “MasterBlogPostsFrontend” button and then set the “SortExpression” field to “Featured DESC, PublicationDate DESC”.

 

Please note that if you called your new custom field that we added earlier anything other than “Featured”, then you will need to update your “SortExpression” accordingly. You can find more information about SortExpression in the “Filtering and sorting blog posts” section of the Blogs documentation.

Previously our blog posts were being sorted by publication date only, so our newest published posts were being added to the top of our lists as so:

I’ve now gone back into my existing blog posts and set the posts titled “What is Eco Friendly Technology” and “Examples of Eco Friendly Technology” as “featured” by checking their corresponding “Featured” checkbox. Now we should see these “featured” posts being “pinned” to the top of our list, regardless of their publish date, and subsequently our “newest published” posts added beneath them:



That’s it, simple solution is simple. Do note that this new “SortExpression” will only affect this particular Blog Posts widget and not any others.

Now there is one piece of confusion that came upon while researching this topic that I wanted to clear up. The item of confusion is the ability of the “SortExpression” field to support multiple columns. In our case this would be the custom “Featured” field and the “PublicationDate”. In the past “SortExpression” only supported one column at a time and I came across a number of Sitefinity forum posts that mentioned this fact. However, I noticed that many of these posts were fairly old and only pertained to Sitefinity 3.x and 4.x. After reading the current documentation and searching for newer forum posts I couldn’t find any information that said whether “SortExpression” could or couldn’t use multiple columns now, so I basically added my “SortExpression” and fortunately it worked. I tested out a few different scenarios to make sure that everything looked good and they all came back as expected, so I feel pretty certain that the “SortExpression” does in fact support multiple columns now. Otherwise I would have expected the Sitefinity Blog Posts widget to default to sorting by the “PublicationDate”.

One last thing before I go. This post should not be taken as an endorsement that this is the best way to go. It is only meant to show how one might sort their blog posts using a custom sorting scenario without touching code. That is not to say that this isn’t a perfectly valid solution for some, but you might also extend the Blogs Posts widget template via code and come up with a more feature rich sorting method and ultimately a more powerful solution.  

 

David Cowart