The standard behaviour of the Blogs module that ships with Sitefinity (versions 3.6, 3.7) when it comes to blog authors is 1-to-1, the author being an optional attribute of the blog definition. While this is fine for a personal blog, where one expects to have a single author for all blog posts, there are cases when a blog is topic-oriented, with multiple authors. In such cases, the author cannot be specified on a blog post level, and the only way to persist the name of the author is to create a dedicated blog for that author with its own "blog page". As a result, if there are 20 contributors / authors we'll end up creating 20 blogs in the administration area, and 20 blog pages for the public side that act as presentation layer for all blog posts by the related author.
Of course there's the option to have a single blog page for all blogs (each having its own author) in order to avoid changing the site structure / map for every new author. In this case, however, the author link will point to the same blog page URL for all authors and there's no filtering based on the author name.
This is the first part of a 2-part set of blog posts where we'll describe a possible implementation for "single blog - multiple authors" functionality:
- Part 1 - Admin view: enable author maintenance on the blog post level;
- Part 2 - Public view: new controls for Blog Posts, and Bloggers List that allow filtering by author.
Part 1 - Admin View: Enable Author Maintenance on the Post Level
All files and source code discussed in this post are available for
download.
The author name will be persisted as a mandatory meta-field with a "ShortText" type. The Blogs module already has an Author meta-field defined in the configuration file (web.config), however it's not maintained through the built-in add / edit post templates:
<metaFields>
...
<add key="Blogs.Author" valueType="ShortText" visible="True" searchable="True" sortable="True" defaultValue=""/>
...
</metaFields>
In order to allow for the Author meta-field maintenance we'll enable the external templates for blog post creation / update, and modify these templates by adding a new section for Author maintenance.
1.1 Enable Add / Edit External Templates for Blog Post
The external templates for blog post creation and update are available in a separate download (that contains all external templates and controls configuration file) from your client area downloads on sitefinity.com. We'll need the following external templates, and their resource files:
- /Sitefinity/Admin/ControlTemplates/Blogs/PostNew.ascx
- /Sitefinity/Admin/ControlTemplates/Blogs/PostEdit.ascx
- /Sitefinity/Admin/ControlTemplates/Blogs/App_LocalResources/PostNew.ascx.resx
- /Sitefinity/Admin/ControlTemplates/Blogs/App_LocalResources/PostEdit.ascx.resx
Copy these files in the corresponding location on your website, e.g.:
- ~/Sitefinity/Admin/ControlTemplates/Blogs/
- ~/Sitefinity/Admin/ControlTemplates/Blogs/App_LocalResources/
The external templates are enabled through the controls configuration file (full template also available in the external templates download):
- ~/App_Data/Configuration/Telerik.Sitefinity.Configuration.ControlsConfig.xml
The following view settings are needed in the controls configuration file:
<!--Provides user interface for creating a new blog post in the blogs module.-->
<viewSettings hostType="Telerik.Blogs.WebControls.Admin.PostNewView" layoutTemplatePath="~/Sitefinity/Admin/ControlTemplates/Blogs/PostNew.ascx" />
<!--Provides user interface for editing a blog post in the blogs module.-->
<viewSettings hostType="Telerik.Blogs.WebControls.Admin.PostEditView" layoutTemplatePath="~/Sitefinity/Admin/ControlTemplates/Blogs/PostEdit.ascx" />
Save the configuration file and restart the website. The external templates for blog post creation / update are now in use.
1.2 Modify the External Templates to Allow for Author Maintenance
We'll edit the external templates using Visual Studio. If you don't have a licensed version of Visual Studio, a free version of Visual Web Developer (Express) is available from Microsoft.
Apply the following changes to both external templates we enabled at step (1.1):
- ~/Sitefinity/Admin/ControlTemplates/Blogs/PostNew.ascx
- ~/Sitefinity/Admin/ControlTemplates/Blogs/PostEdit.ascx
Locate:
<sf:ContentMetaFields ID="MetaFields" runat="server">
and just below the <ItemTemplate> node add the following:
<!-- Author: START -->
<h3>
<asp:Literal ID="LiteralAuthorSet" runat="server" Text="<%$Resources:AuthorSet %>"></asp:Literal> *
</h3>
<fieldset class="set">
<ol class="setIn clearfix">
<li class="catSel">
<asp:Label ID="Label_Author" AssociatedControlID="Author" runat="server">
<asp:Literal ID="Literal_Author" runat="server" Text="<%$Resources:Author %>"></asp:Literal>
</asp:Label>
<asp:TextBox ID="Author" runat="server" MaxLength="250"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator_Author" runat="server" ControlToValidate="Author" Display="Dynamic" EnableViewState="False" CssClass="validMessage" SetFocusOnError="True">
<strong><asp:Literal ID="Literal_AuthorEmpty" runat="server" Text="<%$Resources:AuthorEmpty %>" /></strong>
</asp:RequiredFieldValidator>
</li>
</ol>
</fieldset>
<div class="bottom"><div><!-- --></div></div>
<!-- Author: END -->
Save and close the external templates. Add the following (name, value) pairs to each of the resource files (PostNew.ascx.resx, PostEdit.ascx.resx):
- Author, Author Name
- AuthorSet, Post Author
- AuthorEmpty, Author Name cannot be empty!
Save and close the resource files.
The external templates are ready to be used. We can now maintain the Author on the blog post level:

The post author name can be seen on the right-hand sidebar of the blog post preview:

1.3 Populate the Author Name Using the Current User Profile
To ensure Author Name consistency and, at the same time, improve the user experience we can set a default value for the Author Name in case there isn't a value already set, and disable the text box if the user is not part of the "administrators" role. We'll implement the following functionality:
- Create New Post: set default value for Author Name to First Name / Last Name as set in the profile of the current user. These profile properties are not mandatory, as a result we'll only disable the field if the profile First Name / Last Name are non-empty. The author name will always be available for editing (enabled) if the current user is part of the "administrators" role.
- Edit Existing Post: if a value is already set for Author Name, we'll just disable / enable the field if the user is not / is part of the "administrators" role. If the Author Name is not set (this would be the case with blog posts created before implementing the functionality described in this article), the behaviour will be identical with new post creation described above.
1.3.1 Enable Author Default Value for New Post Creation
Open the "~/Sitefinity/Admin/ControlTemplates/Blogs/PostNew.ascx" file using you're favourite editor, and replace:
<%@ Control Language="C#" %>
with:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PostNew.ascx.cs" Inherits="Sitefinity_Admin_ControlTemplates_Blogs_PostNew" %>
Create a file named "PostNew.ascx.cs" in the same directory as the user control template "~/Sitefinity/Admin/ControlTemplates/Blogs/". Copy the following code in the newly created file, save it and close it:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using Telerik.Security;
public partial class Sitefinity_Admin_ControlTemplates_Blogs_PostNew : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TextBox textboxAuthor = MetaFields.Controls[0].FindControl("Author") as TextBox;
if (textboxAuthor != null)
{
ProfileBase userProfile = new ProfileBase();
userProfile.Initialize(UserManager.GetCurrentUserName(), true);
textboxAuthor.Text = (userProfile.GetPropertyValue("FirstName").ToString().Trim() + " " + userProfile.GetPropertyValue("LastName").ToString().Trim()).Trim();
if(!String.IsNullOrEmpty(textboxAuthor.Text))
textboxAuthor.Enabled = UserManager.IsCurrentUserInRole("administrators");
}
}
}
}
1.3.2 Enable Author Default Value for Existing Blog Post
Open the "~/Sitefinity/Admin/ControlTemplates/Blogs/PostEdit.ascx" file using you're favourite editor, and replace:
<%@ Control Language="C#" %>
with:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PostEdit.ascx.cs" Inherits="Sitefinity_Admin_ControlTemplates_Blogs_PostEdit" %>
Create
a file named "PostEdit.ascx.cs" in the same directory as the user
control template "~/Sitefinity/Admin/ControlTemplates/Blogs/". Copy the
following code in the newly created file, save it and close it:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using Telerik.Cms.Engine;
using Telerik.Blogs;
using Telerik.Security;
public partial class Sitefinity_Admin_ControlTemplates_Blogs_PostEdit : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TextBox textboxAuthor = MetaFields.Controls[0].FindControl("Author") as TextBox;
if (textboxAuthor != null)
{
object oMetaAuthor;
Guid blogId = new Guid(Request.QueryString["ParentId"]);
Guid postId = new Guid(Request.QueryString["Param"]);
BlogManager blogManager = new BlogManager();
IBlog theBlog = blogManager.GetBlog(blogId);
foreach (IContent blogPost in theBlog.Posts)
{
if (blogPost.ID == postId)
{
oMetaAuthor = blogPost.GetMetaData("Author");
if (oMetaAuthor == null || String.IsNullOrEmpty(oMetaAuthor.ToString()))
{
ProfileBase userProfile = new ProfileBase();
userProfile.Initialize(UserManager.GetCurrentUserName(), true);
textboxAuthor.Text = (userProfile.GetPropertyValue("FirstName").ToString().Trim() + " " + userProfile.GetPropertyValue("LastName").ToString().Trim()).Trim();
}
if (!String.IsNullOrEmpty(textboxAuthor.Text))
textboxAuthor.Enabled = UserManager.IsCurrentUserInRole("administrators");
break;
}
}
}
}
}
}
All done. Please review all user profiles and maintain First Name / Last Name as appropriate to ensure the correct default value for a blog post author.
All files and source code discussed in this post are available for
download.