MetaDetail Add for the newly created Blog Posts & News

Posted by Community Admin on 03-Aug-2018 10:29

MetaDetail Add for the newly created Blog Posts & News

All Replies

Posted by Community Admin on 26-Jul-2016 00:00

As per the requirement, Need to create Meta Title, Meta Description & Meta Keywords for all of the pages which will be available after creating of an every individual blog posts & news.

One of the solution which I have tried was as mentioned (This was taken from here ):

Step 1 : Create custom fields inside the blog post

Step 2 : Use of an ActionFilter

namespace SitefinityWebApp
    /// <summary>
    /// Blog Attribute
    /// </summary>
    public class BlogMetaAttribute : ActionFilterAttribute
    
        /// <summary>
        /// MetaTitle variable
        /// </summary>
        public string MetaTitle get; set;
 
        /// <summary>
        /// Override the on Action Executing
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        
            base.OnActionExecuting(filterContext);
 
            if (filterContext.Controller.GetType() == typeof(NewsController) && filterContext.ActionDescriptor.ActionName == "Details")
            
                var actionParameters = filterContext.ActionParameters as IDictionary<string, object>;
                var newsItem = actionParameters["newsItem"] as NewsItem;
                if (newsItem != null)
                
                    this.MetaTitle = newsItem.Title;
 
                    this.Register();
                
            
        
 
        /// <summary>
        /// Register Events
        /// </summary>
        private void Register()
        
            EventHub.Unsubscribe<IPagePreRenderCompleteEvent>(OnPagePreRenderCompleteEventHandler);
            EventHub.Subscribe<IPagePreRenderCompleteEvent>(OnPagePreRenderCompleteEventHandler);
        
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="event"></param>
        private void OnPagePreRenderCompleteEventHandler(IPagePreRenderCompleteEvent @event)
        
            if (!string.IsNullOrEmpty(this.MetaTitle))
            
                HtmlMeta tag = new HtmlMeta();
                tag.Attributes.Add("property", "title");
                tag.Content = this.MetaTitle;
                @event.Page.Header.Controls.Add(tag);
            
        
    

Step 3  : Filter simply register in the Global.asax

protected void Application_Start(object sender, EventArgs e)
    Bootstrapper.Initialized += this.Bootstrapper_Initialized;
   
private void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
    if (e.CommandName == "Bootstrapped")
    
        GlobalFilters.Filters.Add(new FacebookMetaAttribute());
    

Still unable to load meta detail inside the page

Posted by Community Admin on 26-Jul-2016 00:00

I met the same situation with meta title and meta description. 

I just copied source code of existing News widget. Renamed it to Custom News widget. Added new fields (title, description and keywords) to model.

And in controller Details action added

ViewBag.Title = "something here";

ViewBag.Title automatically overrides old titile

You can find source code of News widget here: https://github.com/Sitefinity/feather-widgets/tree/master/Telerik.Sitefinity.Frontend.News/

This thread is closed