+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / MetaDetail Add for the newly created Blog Posts & News

MetaDetail Add for the newly created Blog Posts & News

2 posts, 0 answered
  1. Anand
    Anand avatar
    3 posts
    Registered:
    28 Mar 2016
    26 Jul
    Link to this post

    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
  2. Victor Leontyev
    Victor Leontyev avatar
    65 posts
    Registered:
    01 Jul 2014
    26 Jul in reply to Anand
    Link to this post

    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/

2 posts, 0 answered