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

Forums / Developing with Sitefinity / Extending the News module advice.

Extending the News module advice.

12 posts, 0 answered
  1. Meister
    Meister avatar
    262 posts
    Registered:
    09 Nov 2007
    20 Oct 2009
    Link to this post
    Hi

    I have a few questions on how i can make the news module fit to a clients needs.

    1. I need to change the meta tags on individual news items
    How would i implement such a feature?

    2. I would also like to control custom <HEAD> tags for robots etc

    3. I would like to be able to specify the page name instead of using the page title

    4. add a new field called short description that can be used in side panels to show latest news stories without lengthy titles

    5. i would like to be able to specify which social bookmarks to show per news item (how would i tackle this?)

    Sorry for a lot of questions that may not be easy to answer
    thanks
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    20 Oct 2009
    Link to this post
    Hi Quade,

    1. I need to change the meta tags on individual news items. How would i implement such a feature?

    It depends from where you want to change it - control, module etc. Generally you can create a list of content items for the desired provider - news, blogs, events and so on than get the item you want and call SetMetaData method. You can get all metakeys for a give provider using

    List<string> mFieldsList = new List<string>(this.manager.Provider.MetaKeys.Count);
    foreach (MetaInfo info in this.manager.Provider.MetaKeys.Values)
    {
        //do something with metadata
    }

    2. I would also like to control custom <HEAD> tags for robots etc.

    You can use HtmlMeta class or Sitefinity API to set the head and other tags. You need to access the ICms object and use its properties.

    3. I would like to be able to specify the page name instead of using the page title

    The page name will be empty string so you can use MenuName.

    4. add a new field called short description that can be used in side panels to show latest news stories without lengthy titles

    You can get the item you want to show by ID or Title or another identifier and then set it as a value of a key. you can get the title as a string and check its length using Length property. If necessary use String.Substring method.

    5. i would like to be able to specify which social bookmarks to show per news item (how would i tackle this?)

    The SocialBookmark class is private and its members cannot be accessed. You can control the bookmarks per control.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. James
    James avatar
    25 posts
    Registered:
    15 May 2009
    22 Nov 2010
    Link to this post
    Hi Ivan,

    I'm trying to override the Head tags for Description and keywords but think I have missed something in the code. I am editing the NewsItemNew external template with a couple of extra boxes and adding a couple of metafields in my web.config, this saves to the databse fine.. then adding a code behind to accomplish adding these to the Header. The addition to the Header does not seem to be taking..

    using System;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Telerik.News.WebControls;
     
    namespace Sitefinity.Admin.ControlTemplates.News
    {
        public partial class NewsItemNew : NewsView
        {
            protected void PageLoad(object sender, EventArgs e)
            {
                var desc = divWorkArea.Controls[0].FindControl("Description") as TextBox;
                var kwords = divWorkArea.Controls[0].FindControl("Keywords") as TextBox;
     
                var keywords = new HtmlMeta {Name = "keywords", Content = kwords.Text};
                var description = new HtmlMeta {Name = "description", Content = desc.Text};
     
                for (var i = 0; i < Page.Header.Controls.Count; i++)
                {
                    var myTag = Page.Header.Controls[i];
                    if ((myTag == keywords) || (myTag == description))
                    {
                        Page.Header.Controls.Remove(myTag);
                    }
                }
                Page.Header.Controls.Add(keywords);
                Page.Header.Controls.Add(description);
            }
        }
    }

    The code does not remove any existing controls from the page template so I assume the code is completely wrong at some point. Is there any thing I could add to this to make it work?

    Thanks in advance.
    (using Sitefinity 3.6)
  4. James
    James avatar
    25 posts
    Registered:
    15 May 2009
    22 Nov 2010
    Link to this post
    I tried ammending my code by creating a class MetaTags in App_Code with a couple of functions which should add head tags, then altering the NewsItemNew.ascx.cs slightly from previously:

    App_Code/MetaTags.cs
    using System.Web.UI.HtmlControls;
     
    namespace App_Code
    {
        public class MetaTags
        {
            public MetaTags()
            {
     
            }
            public static void Add(HtmlHead head, string name, string content)
            {
                // Prepare the meta tag
                var metaTag = new HtmlMeta
                {
                    Name = name,
                    Content = content
                };
     
                // Add the meta tag to the head parameter
                head.Controls.Add(metaTag);
            }
            public static void AddDescription(HtmlHead head, string content)
            {
                Add(head, "Description", content);
            }
            public static void AddKeywords(HtmlHead head, string content)
            {
                Add(head, "Keywords", content);
            }
        }
    }


    NewsItemNew.ascx.cs
    using System;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using App_Code;
     
    namespace Sitefinity.Admin.ControlTemplates.News
    {
        public partial class NewsItemNew : System.Web.UI.UserControl
        {
            protected void PageLoad(object sender, EventArgs e)
            {
                //Find the Description TextBox
                var desc = divWorkArea.Controls[0].FindControl("Description") as TextBox;
                //Get the text content
                var descContent = desc.ToString();
     
                //Find the Keywords TextBox
                var kwords = divWorkArea.Controls[0].FindControl("Keywords") as TextBox;
                //Get the text content
                var kwordsContent = kwords.ToString();
     
                //Iterate through the controls in the page header
                for (var i = 0; i < Page.Header.Controls.Count; i++)
                {
                    //read each item into myTag
                    var myTag = Page.Header.Controls[i];
                    if ((myTag is HtmlMeta && (((HtmlMeta)myTag).Name.ToLower()=="Keywords"))||(myTag is HtmlMeta && (((HtmlMeta)myTag).Name.ToLower()=="Description")))
                    {
                        Page.Header.Controls.Remove(myTag);
                    }
                }
                MetaTags.AddDescription(Page.Header, descContent);
                MetaTags.AddKeywords(Page.Header, kwordsContent);
            }
        }
    }

    It still is not working. It's a couple of weeks ahead of our purchase but I may get my boss to buy a license so I can open a ticket. If anyone else comes across this in the meantime who can see why it is not working please leave your comments.

    Thanks.
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    23 Nov 2010
    Link to this post
    Hello James,

    You can create a custom class that inherits from InternalPage and override AddHeaderControls method where you can modify the header.

    Another option is using OnPreRender

    protected override void OnPreRender(EventArgs e)
       {
           base.OnPreRender(e);
      
           for (int i = 0; i < Page.Header.Controls.Count; i++)
           {
               Control c = this.Page.Header.Controls[i] as Control;
     
     
                           ...
           }
       }

    You can cast the control the the type you are looking for and then match it by any criteria.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. James
    James avatar
    25 posts
    Registered:
    15 May 2009
    25 Nov 2010
    Link to this post
    Hi Ivan,

    I think I am 95% there. So I created a CustomNewsView class inheriting InternalPage

    using System.Web.UI.HtmlControls;
    using Telerik.Cms.Web;
     
    namespace App_Code
    {
        public class CustomNewsView : InternalPage
        {
            protected override void AddHeaderControls(Telerik.Cms.IPage page)
            {
                var metaDescription = new HtmlMeta
                {
                    Name = "Description",
                    Content = MetaDescription
                };
                var metaKeywords = new HtmlMeta
                {
                    Name = "Keywords",
                    Content = MetaKeywords
                };
                Header.Controls.AddAt(1, metaDescription);
                Header.Controls.AddAt(2, metaKeywords);
            }
     
            public static string MetaDescription, MetaKeywords;
        }
    }

    then Inherited my CMS entrypoint from the custom class:

    <%@ Page Inherits="App_Code.CustomNewsView" MasterPageFile="~/Sitefinity/Dummy.master" %>

    and set the two strings for MetaDescription and MetaKeywords from my Admin user control (I am using the External Template NewsItemNew.ascx) code behind:

    using System;
    using System.Web.UI.WebControls;
    using App_Code;
     
    namespace Sitefinity.Admin.ControlTemplates.News
    {
        public partial class NewsItemNew : System.Web.UI.UserControl
        {
            protected void PageLoad(object sender, EventArgs e)
            {
                var desc = MetaFields.Controls[0].FindControl("Description") as TextBox;
                var descContent = desc.ToString();
                var kwords = MetaFields.Controls[0].FindControl("Keywords") as TextBox;
                var kwordsContent = kwords.ToString();
                 
                CustomNewsView.MetaDescription = descContent;
                CustomNewsView.MetaKeywords = kwordsContent;
            }
        }
    }

    I get the rendered source as:

    <meta name="Description" /><meta name="Keywords" />

    So I don't think my FindControl is working so nothing is being assigned to my two strings descContent, and kwordsContent...

    How do I query these values at runtime?

    James.
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    26 Nov 2010
    Link to this post
    Hi James,

    Try using recursion or run the code in debug mode to see whether the controls you are looking for are found in the collection.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. James
    James avatar
    25 posts
    Registered:
    15 May 2009
    26 Nov 2010
    Link to this post
    Hi Ivan,

    Thanks for your help. I figured out I don't need NewsViewInsert.cs as my custom fields get saved to the database anyway, and assigning the variables to those strings does nothing! I need to retrieve them from the database by association when the page containing the newsview gets rendered.

    I am now trying to work out how to query whether a given page contains a news view, and if it is set to single display mode, and if so add the header tags. Back to the drawing board :) Thanks again

    James.
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    26 Nov 2010
    Link to this post
    Hello James,

    If there is the following key in the context - Context.Request[ContentItemKey] then there is a ContentView control in details mode on the page.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  10. James
    James avatar
    25 posts
    Registered:
    15 May 2009
    29 Nov 2010
    Link to this post
    Hi Ivan,

    I am at an impasse with this.. I am trying to access the current page's (NewsView in detail view) contols to compare to the current item from a list of news items. I get:

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
     
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
     
    Source Error:
     
    Line 34:            {
    Line 35:                // access the current items Id
    Line 36:                var news = mgr.Content.GetContent(view.SelectedItemId);
    Line 37:                // if newsItem from list is current news item
    Line 38:                if (newsItem.Content == news.Content)

    I only have code in MetaTags.cs in App_Class now:

    using System.Collections;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using Telerik.Cms;
    using Telerik.Cms.Engine;
    using Telerik.Cms.Web;
    using Telerik.News;
    using Telerik.News.WebControls;
     
    namespace App_Code
    {
        // Inherits Internal Page
        public class MetaTags : InternalPage
        {
            protected override void AddHeaderControls(IPage page)
            {
                // Get news using NewsManager
                var mgr = new NewsManager("News");
     
                // read it into IList
                IList newsList = mgr.Content.GetContent();
     
                // access the current page's controls
                // using System.Web.HttpContext.Current.Handler
                var pageAccess = (Page)HttpContext.Current.Handler;
     
                // cast the control to NewsView type where newsBody is
                // the content placeholder id on the master page
                var view = pageAccess.FindControl("newsBody") as NewsView;
     
                // iterate through the news items
                foreach (IContent newsItem in newsList)
                {
                    // access the current items Id
                    var news = mgr.Content.GetContent(view.SelectedItemId);
                    // if newsItem from list is current news item
                    if (newsItem.Content == news.Content)
                    {
                        // Get newsItem's metadata for Keywords & Description
                        Keywords = newsItem.GetMetaData("Keywords").ToString();
                        Description = newsItem.GetMetaData("Description").ToString();
     
                        // assign both to the
                        MetaDescription = Description;
                        MetaKeywords = Keywords;
                    }
                }
                // create metadata for Description
                var metaDescription = new HtmlMeta
                {
                    Name = "Description",
                    Content = MetaDescription
                };
     
                // create metadata for Keywords
                var metaKeywords = new HtmlMeta
                {
                    Name = "Keywords",
                    Content = MetaKeywords
                };
     
                // add controls to page
                Header.Controls.AddAt(1, metaDescription);
                Header.Controls.AddAt(2, metaKeywords);
                 
            }
             
            public static string Keywords, Description;
            public static string MetaDescription, MetaKeywords;
        }
    }

    Could you help me with this code? I have spent a few days going through the API and forums and can't get past this. It's a sticky situation when I'm asked why we can't specify keywords and descriptions for our news items using the CMS as they are rendered as pages where only the parent tags can be specified. We get most of our traffic from our news section.

    Thanks for any help you can give on this

    James.
  11. James
    James avatar
    25 posts
    Registered:
    15 May 2009
    29 Nov 2010
    Link to this post
    I also noticed if you open a Generic Content Item in Admin (can be blog posts or news items) you have a toolbar item for "Page Properties" which is not enabled out of the box. In the dialogue box you can set

    Page Attributes

    If you set any of these parameters they do not carry over when the item renders as a page. Is there a reason why this feature is not implemented? Would it be an easy thing to do?

    Thanks

    James.
  12. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    29 Nov 2010
    Link to this post
    Hello James,

    1. One of the objects in this line - var news = mgr.Content.GetContent(view.SelectedItemId); is null.

    2. Page Properties can be set only when you create or edit a page.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
12 posts, 0 answered