The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
Adding MetaDescription tags for blogs, news, etc details view page has been a widely discussed topic in the Sitefinity community. With the following blog post we aim at demonstrating you how easy it is to achieve the desired functionality thanks to the extensibility of Sitefinity's ContentView.
For the purposes of the blog post we'll be looking at the Blogs module. As you know adding a new custom field to blog posts is as easy as making a couple of clicks in the backend, so we'll be using a simple custom field of type short text to store the desired MetaDescription.
Once you've added the custom field to blog posts you can move to the next step, which is overriding the default Telerik.Sitefinity.Modules.Blogs.Web.UI.Public.DetailPostView
inside the new custom view we're creating we can get a hold of the blog post item that will be displayed, by plugging in the InitializeControls() method and saying:
protected
override
void
InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container, Telerik.Sitefinity.Web.UI.ContentUI.Contracts.IContentViewDefinition definition)
{
base
.InitializeControls(container, definition);
var viewCtrl = (BlogPostView)
this
.Host;
var item = viewCtrl.DetailItem
as
BlogPost;
AddMetaKeywordsMarkup(item);
}
private
void
AddMetaKeywordsMarkup(BlogPost item)
{
try
{
if
(!
string
.IsNullOrEmpty(item.GetValue<String>(
"MetaDescription"
)))
{
HtmlMeta metaDescr =
new
HtmlMeta();
metaDescr.Name =
"Keywords"
;
metaDescr.Content = item.GetValue<String>(
"MetaDescription"
);
this
.Page.Header.Controls.Add(metaDescr);
}
}
catch
(Exception )
{
}
}
where MetaDescription
is the name of a custom field I've added to blog posts.
You can now tell Sitefinity to use the custom view by going to Administration->Settings->Advanced -> Blogs-> Controls->BlogsPostsFrontend->Views->DetailBlogPostsFrontend and changing the ViewType to the type of your custom view.
And there you go, you can start filling out the desired MetaDescription you want to see in your blog post's detail page.
For your convenience the custom view sample discussed in this blog post can be downloaded from here. Please do not hesitate to extend and modify it as per your requirements.
View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.