More in this section

Forums / Developing with Sitefinity / News API: NewsManager.Content.GetContent Not Working

News API: NewsManager.Content.GetContent Not Working

14 posts, 0 answered
  1. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    02 Feb 2010
    Link to this post
    Hi,

    It's us and the Sitefinity News Module again!
    We're using Sitefinity 3.6 (Please don't tell us to upgrade, it's not an option right now - we can't just upgrade every other week)

    We've built a news ticker and bind the data as follows:

    filters[0] = new MetaSearchInfo(MetaValueTypes.DateTime, "Expiration_Date", DateTime.Now, SearchCondition.GreaterOrEqual);
    filters[1] = new MetaSearchInfo(MetaValueTypes.DateTime, "Publication_Date", new DateTime(DateTime.Now.Year - 2,1,1), SearchCondition.GreaterThen);
             
    IList list = _NewsManager.Content.GetContent(0, 10, "Publication_Date DESC", filters);
    RadRotator1.DataSource = list;
    RadRotator1.ItemDataBound += new RadRotatorEventHandler(RadRotator1_ItemDataBound);
    RadRotator1.DataBind();

    Question 1: In the GetContent parameter the "from" parameter (what is this for, what does it really do? )

    Question 2: With the filters and sort expression in place we still get news items from all over the place. Yes it seems to be within the filter expression scope, but we're not getting the 10 most recent items. We're getting the most recent item skip a few, then another, skip a few, then another item, and so on. How is this possible? Nowhere is there a parameter or setting that says: "Please don't skip articles I really just want the 10 MOST RECENT items".

    Can anyone help explain why this is happening?

    PS. The Sitefinity documentation for this API is majorly lacking as is the code documentation. This link doesn't describe at all how one can expect the GetContent method to behave or anything about it at all: http://www.sitefinity.com/help/developer-manual/news-api-overview.html

    PSS. Please do not tell us to upgrade.

    Jacques
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    02 Feb 2010
    Link to this post
    Hi J.Hoventer,

    There is "int from" parameter that indicates the starting position of the list you want to return. The GetContent overload you use gets filtered subset of content with specified starting position and maximum number, ordered by sorting expression.

    I created a simple code that adds to the list only the last 10 items and rotate them. I could not find ItemDataBound event of the RadRotator in the code you pasted.

    <telerik:RadRotator ID="RadRotator1" runat="server"
    RotatorType="AutomaticAdvance"
    Width="100px"
    Height="100px"
    ScrollDirection="Down" >
       <ItemTemplate>
     <asp:Label runat="server" ID="rotatorLabel"/>
       </ItemTemplate>
    </telerik:RadRotator>

           void RadRotator1_ItemDataBound(object sender, RadRotatorEventArgs e)
           {
               
              Label lbl =  e.Item.FindControl("rotatorLabel") as Label;
              IContent cnt = e.Item.DataItem as IContent;
              lbl.Text = (string)cnt.GetMetaData("Title");
           }
     
     
           protected void Page_Load(object sender, EventArgs e)
           {
               var CntManager = new ContentManager("News");
               IList list = CntManager.GetContent(0, 10, "Publication_Date DESC");
               RadRotator1.DataSource = list;
               RadRotator1.ItemDataBound += new RadRotatorEventHandler(RadRotator1_ItemDataBound);
               RadRotator1.DataBind();
           }

    There is a RadTicker control that you can use to achieve the same goal.

    You can also attach VS debugger the the filtered list and use Count in the immediate window to see how many items the list contains.
    Generally you have to look into Content items section, not in news, since all GenericContent modules use IContent objects, no matter of the provider. You can take a look at Finding Content Items

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  3. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    03 Feb 2010
    Link to this post
    Hi Ivan,

    I'm going to paste in the entire control, because to a degree I think our code is doing exactly what you're suggesting:

    001.using System;
    002.using System.Data;
    003.using System.Configuration;
    004.using System.Collections;
    005.using System.Web;
    006.using System.Web.Security;
    007.using System.Web.UI;
    008.using System.Web.UI.WebControls;
    009.using System.Web.UI.WebControls.WebParts;
    010.using System.Web.UI.HtmlControls;
    011.using Telerik.News;
    012.using Telerik.Cms.Engine;
    013.using Telerik.Web.UI;
    014.using Telerik.Cms.Engine.ContentViewFiltering;
    015.using Telerik.News.WebControls;
    016. 
    017.using System.ComponentModel;
    018. 
    019.public partial class UserControls_NewsRotator : System.Web.UI.UserControl
    020.{
    021.    #region FIELDS
    022.    private string _ProviderName;
    023.    private NewsManager _NewsManager;
    024.    private string _NewsDetailsPath = "";
    025.    private byte _Counter = 0;
    026.    private byte _MaxItems = 10;
    027.    #endregion
    028.    #region PROPERTY - ProviderName
    029.    public string ProviderName
    030.    {
    031.        get
    032.        {
    033.            return this._ProviderName;
    034.        }
    035.        set
    036.        {
    037.            this._ProviderName = value;
    038.        }
    039.    }
    040.    #endregion
    041.    #region PROPERTY - Rotator
    042.    [Browsable(true)]
    043.    public RadRotator Rotator
    044.    {
    045.        get
    046.        {
    047.            return this.RadRotator1;
    048.        }
    049.        set
    050.        {
    051.            this.RadRotator1 = value;
    052.        }
    053.    }
    054.    #endregion
    055.    #region PROPERTY - NewsDetailsPath
    056.    public string NewsDetailsPath
    057.    {
    058.        get
    059.        {
    060.            return _NewsDetailsPath;
    061.        }
    062.        set
    063.        {
    064.            _NewsDetailsPath = value;
    065.        }
    066.    }
    067.    #endregion
    068.    #region EVENTHANDLER - Page_Load
    069.    protected void Page_Load(object sender, EventArgs e)
    070.    {
    071.        //get the default content provider
    072.        if (String.IsNullOrEmpty(this._ProviderName))
    073.            this._ProviderName = NewsManager.DefaultContentProvider;
    074. 
    075.        //get the news manager
    076.        if (this._NewsManager == null)
    077.            this._NewsManager = new NewsManager(this._ProviderName);
    078. 
    079.        //get the max rows from the config file
    080.        _MaxItems = Convert.ToByte(ConfigurationManager.AppSettings["NewsTickerMaxItems"]);
    081. 
    082.        //create a filter build
    083.        //ContentFilterBuilder filter = new ContentFilterBuilder();
    084. 
    085.        //set the datasource of the rotator
    086.        //DateTime filterDate = new DateTime(DateTime.Now.Year - 1, 1, 1);
    087.        IMetaSearchInfo[] filters = new IMetaSearchInfo[2];
    088.        filters[0] = new MetaSearchInfo(MetaValueTypes.DateTime, "Expiration_Date", DateTime.Now, SearchCondition.GreaterOrEqual);
    089.        filters[1] = new MetaSearchInfo(MetaValueTypes.DateTime, "Publication_Date", new DateTime(DateTime.Now.Year - 2,1,1), SearchCondition.GreaterThen);
    090.         
    091.        //_NewsManager.Content.FilterExpression = "Publication_Date >= " + filterDate.ToShortDateString();
    092.        IList list = _NewsManager.Content.GetContent(0, _MaxItems, "Publication_Date DESC", filters);
    093.        RadRotator1.DataSource = list;
    094.        RadRotator1.ItemDataBound += new RadRotatorEventHandler(RadRotator1_ItemDataBound);
    095.        RadRotator1.DataBind();
    096.    }
    097.    #endregion
    098.    #region EVENTHANDLER - RadRotator1_ItemDataBound
    099.    void RadRotator1_ItemDataBound(object sender, RadRotatorEventArgs e)
    100.    {
    101.        _Counter++;
    102. 
    103.        Literal counter = (e.Item.FindControl("CounterText") as Literal);
    104. 
    105.        //if the max items is smaller than the total items found
    106.        if (_MaxItems < _NewsManager.Content.ContentCount())
    107.        {
    108.            //use max items instead
    109.            counter.Text = _Counter + " of " + _MaxItems.ToString();
    110.        }
    111.        else //if the Content Count is smaller than the max count
    112.        {
    113.            //use the content count instead
    114.            counter.Text = _Counter + " of " + _NewsManager.Content.ContentCount().ToString();
    115.        }
    116.         
    117. 
    118.        //get the icontent item
    119.        IContent contentItem = (IContent)e.Item.DataItem;
    120. 
    121.        //set the title of this article
    122.        HyperLink link = (e.Item.FindControl("NewsTitle") as HyperLink);
    123.        link.Text = contentItem.GetMetaData("Title").ToString();
    124.        link.NavigateUrl = _NewsDetailsPath + contentItem.UrlWithExtension;
    125. 
    126.        //extract the library reference for the thumnail
    127.        string thumbnailReference = (string)contentItem.GetMetaData("Thumbnail");
    128. 
    129.        //get a reference to the current image object
    130.        Image img = e.Item.FindControl("NewsThumbnail") as Image;
    131. 
    132.        //if the image object was found and the thumbnail reference is correct
    133.        if (img != null && thumbnailReference != "" && thumbnailReference != null)
    134.        {
    135.            //get the proper URL path for the image
    136.            img.ImageUrl = _GetUrl(thumbnailReference, true);
    137.        }
    138. 
    139.        //set publication date
    140.        Label publicationDateLabel = (e.Item.FindControl("PublicationDate") as Label);
    141.        DateTime publicationDate = Convert.ToDateTime(contentItem.GetMetaData("Publication_Date").ToString());
    142.        publicationDateLabel.Text = publicationDate.ToString("yyyy-MM-dd");
    143.    }
    144.    #endregion
    145.    #region METHOD - _GetUrl(thumbnailReference,resolveAsAbsoluteUrl
    146.    private string _GetUrl(string thumbnailReference, bool resolveAsAbsoluteUrl)
    147.    {
    148.        if (thumbnailReference != null && thumbnailReference.StartsWith("~/"))
    149.            return this.ResolveUrl(thumbnailReference);
    150.        if (thumbnailReference != null && thumbnailReference.StartsWith("["))
    151.        {
    152.            int idx = thumbnailReference.IndexOf("]");
    153.            string provider = thumbnailReference.Substring(1, idx - 1);
    154.            string strId = thumbnailReference.Substring(idx + 1);
    155.            Guid id = new Guid(strId);
    156.            if (ContentManager.Providers.ContainsKey(provider))
    157.            {
    158.                IContent cnt = ContentManager.Providers[provider].GetContent(id);
    159.                if (cnt != null)
    160.                    return VirtualPathUtility.ToAbsolute(cnt.UrlWithExtension, this.Context.Request.ApplicationPath);
    161.            }
    162.            else
    163.            {
    164.                SiteMapNode node = SiteMap.Provider.FindSiteMapNodeFromKey(id.ToString());
    165.                if (node != null)
    166.                    return this.ResolveUrl(node.Url);
    167.            }
    168.            return String.Concat("Item not found: [", thumbnailReference, "]", id);
    169.        }
    170.        return thumbnailReference.ToString();
    171.    }
    172.    #endregion
    173.}

    The _Maxitems variable you see is set to 10 in the web.config file.

    And here is the HTML:
    01.<%@ Control Language="C#" AutoEventWireup="true" CodeFile="NewsRotator.ascx.cs" Inherits="UserControls_NewsRotator" %>
    02.<script type="text/javascript">
    03.    function moveRight()
    04.    {
    05.        $find('<%=RadRotator1.ClientID%>').showNext(Telerik.Web.UI.RotatorScrollDirection.Left);
    06.        //$find('<%=RadRotator1.ClientID%>').showNext(Telerik.Web.UI.RotatorScrollDirection.Left);
    07.    }
    08.    function moveLeft()
    09.    {
    10.        $find('<%=RadRotator1.ClientID%>').showNext(Telerik.Web.UI.RotatorScrollDirection.Right);
    11.        //$find('<%=RadRotator1.ClientID%>').showNext(Telerik.Web.UI.RotatorScrollDirection.Right);
    12.    }
    13.</script>
    14.<table border="0" id="newsTicker" cellpadding="0" cellspacing="0">
    15.    <tr>
    16.        <td rowspan="2" style="width:393px">
    17.            <telerik:RadRotator ID="RadRotator1" runat="server" RotatorType="FromCode" EnableAjaxSkinRendering="false"
    18.                EnableEmbeddedBaseStylesheet="false" EnableEmbeddedSkins="false" Skin="Dominion"
    19.                Height="72" Width="395" ItemHeight="72" ItemWidth="186" WrapFrames="True" FrameDuration="1">
    20.                <itemtemplate>
    21.                    <div class="tickerItem">
    22.                        <asp:Image ID="NewsThumbnail" CssClass="tickerThumbnail" ImageAlign="Left" runat="server"
    23.                            Height="58px" Width="47px" />
    24.                        <div class="tickerItemText">
    25.                            <span class="tickerCounter">
    26.                                <asp:Literal ID="CounterText" runat="server" />
    27.                            </span>
    28.                            <asp:HyperLink ID="NewsTitle" CssClass="tickerTitle" runat="server" />
    29.                            <asp:Label ID="PublicationDate" runat="server" Style="display: none;" />
    30.                        </div>
    31.                    </div>
    32.                </itemtemplate>
    33.                <SlideShowAnimation Type="Pulse" />
    34.            </telerik:RadRotator>
    35.        </td>
    36.        <td>
    37.            <a onclick="moveRight();return false;" class="newsTickerButton newsTickerButtonRight"> </a>
    38.            <a onclick="moveLeft();return false;" class="newsTickerButton newsTickerButtonLeft"> </a>
    39.        </td>
    40.    </tr>
    41.    <tr>
    42.        <td>
    43.             
    44.        </td>
    45.    </tr>
    46.</table>

    We are on go live day so I don't think the RadTicker is an option, plus is it available in 3.6?

    Regards,
    Jacques
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    03 Feb 2010
    Link to this post
    Hello J.Hoventer,

    When you debug the code what is the value of _MaxItems when you get it from the web.config and how many items you have then in the list after applying the filter? If you hardcode 10 for the maximum number of items what is the result?

    The code below returns 10 items at my end. The IList has only 10 items.

    byte _MaxValue = Convert.ToByte(ConfigurationManager.AppSettings["_MaxValue"]);
    IList list = CntManager.GetContent(0, _MaxValue, "Publication_Date DESC");


    The ticker is not available in Sitefinity 3.6.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  5. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    03 Feb 2010
    Link to this post
    If you go to http://alt.thedominion.ca you'll see the news rotator on the first page and it always only shows 10.

    And the question is not if it returns 10 items or not, the 'major' question here is which 10 items. Here's an example of what we're getting, or rather, here's a list of articles in our site and then which ones actually do show in the ticker.

    Title


    Publication Date
    Expiration Date
    Status
    Currently Showing





    1
    Charlottetown and Cornwall to Welcome The Dominion Curling Club Championship

    14 Dec 2009, 09:26 31 Dec 2099, 05:00 Publ... No





    2
    The Dominion Q3 2009 Management’s Discussion and Analysis (MD&A)

    07 Dec 2009, 10:07 31 Dec 2099, 05:00 Publ... Yes (Position 1)





    3
    Ontario Providing Choice in Auto Insurance

    03 Nov 2009, 09:52 31 Dec 2099, 05:00 Publ... No





    4
    George Cooke Discusses Banks Selling Insurance Online on BNN

    19 Oct 2009, 03:49 31 Dec 2099, 05:00 Publ... No





    5
    Tips and Information to Reduce Basement Flooding

    26 Aug 2009, 03:55 01 Feb 2010, 05:00 Publ... No





    6
    The Dominion Discusses August 20 Ontario Tornadoes on CBC Radio

    25 Aug 2009, 04:04 31 Dec 2099, 05:00 Publ... No





    7
    The Dominion Licenses OneShield Dragon® for Modern Policy Management

    18 Aug 2009, 04:40 31 Dec 2099, 05:00 Publ...





    8
    The Dominion Q2 2009 Management’s Discussion and Analysis (MD&A)

    01 Aug 2009, 04:08 31 Dec 2099, 05:00 Publ...





    9
    George Cooke on BNN's Squeeze Play

    17 Jun 2009, 01:00 31 Dec 2099, 05:00 Publ... Yes (Position 2)





    10
    The Dominion Q1 2009 Management’s Discussion and Analysis (MD&A)


    01 May 2009, 11:50
    31 Dec 2099, 05:00
    Publ...
    Yes (Position 3)





    11
    The Dominion's 2008 Annual Report


    06 Apr 2009, 12:57
    31 Dec 2099, 05:00
    Publ...
    Yes (Position 4) Shouldn't show




    As you can see we'd expect to see these articles as the items that the rotator should should.

    When looking at the list of articles in the CMS I noticed that the site is actually showing the articles as follows (in terms of position within the CMS - skip 1, show 2, skip 3 to 8, show 9 to 17 (and they're in the wrong order anyway).

    We have a few hours to sort this out, your help would be greatly appreciated.

    Jacques
  6. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    03 Feb 2010
    Link to this post
    Anything?

    Is it possible that settings on the control itself could cause this behavior? The RotatorType is set to from Code so I assumed this only refers to the actual scrolling part done in javascript. But are there any properties that can cause this to happen.

    As you can see in the C# code the Rotator control itself is exposed as a public property.

    Regards,
    Jacques
  7. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    03 Feb 2010
    Link to this post
    We go live in 1 hour. Any help would be great.

    We've just been doing some tests locally....

    We put a number in the title of each of the articles that should display. Each number represented the position they should show up in.

    Then we changed this line of code:
    01.//set the datasource of the rotator
    02.//DateTime filterDate = new DateTime(DateTime.Now.Year - 1, 1, 1);
    03.IMetaSearchInfo[] filters = new IMetaSearchInfo[2];
    04.filters[0] = new MetaSearchInfo(MetaValueTypes.DateTime, "Expiration_Date", DateTime.Now, SearchCondition.GreaterOrEqual);
    05.filters[1] = new MetaSearchInfo(MetaValueTypes.DateTime, "Publication_Date", new DateTime(DateTime.Now.Year - 5,1,1), SearchCondition.GreaterThen);
    06. 
    07.//_NewsManager.Content.FilterExpression = "Publication_Date >= " + filterDate.ToShortDateString();
    08.IList list = _NewsManager.Content.GetContent(0, 11, "Publication_Date DESC", filters);
    09.RadRotator1.DataSource = list;
    10.RadRotator1.ItemDataBound += new RadRotatorEventHandler(RadRotator1_ItemDataBound);
    11.RadRotator1.DataBind();

    When we change line 8 to be 10 or 11 we get different results. When we make the value 10 article number 2 dissappears from the list, when we change it to 11 article 2 shows up in the right place again.

    Any answers? Any help?
  8. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    03 Feb 2010
    Link to this post
    Any help would be really useful. This looks like a bug to me.
  9. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    03 Feb 2010
    Link to this post
    We've just confirmed that changing the MaxItems in the GetContent method from 10 to 11 changes the articles returned and it does so incorrectly. Item 2 in our list gets removed but by publication date it should show up in position 2.

    Every item in our list is published and not expired.

    Jacques
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    03 Feb 2010
    Link to this post
    Hi J.Hoventer,

    The RadRotator list the items in the order they are added to the IList. Add the code below to see the actual content and its order that should be shown by RadRotator control.

    IList list = CntManager.GetContent(0, _MaxValue, "Publication_Date DESC");
     
         foreach (IContent contentItem in list)
         {
             Response.Write(contentItem.GetMetaData("Title").ToString() + "--" + contentItem.GetMetaData("Publication_Date").ToString() + "<br />");
         }


    Greetings,
    Ivan Dimitrov
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  11. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    03 Feb 2010
    Link to this post
    We're looking at this in Debug mode and the wrong items are being extracted. Please look two or three posts above where I explain in detail what's happening.

    You're saying the RadRotator adds.... but that doesn't matter. In the watch window or immediate window we're outputting the following:
    ((IContent)list[0]).GetMetaData("Title")

    We do that from index 0 - 9 and in there the list is wrong, so this eliminates the RadRotator as the problem.

    Jacques
  12. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    03 Feb 2010
    Link to this post
    Hello J.Hoventer,

    Try using Linq -SQL to get and sort the list.

    IQueryable<IContent> linqList = list.Cast<IContent>().AsQueryable<IContent>();
    linqList.OrderByDescending(c => c.GetMetaData("Publication_Date"));

    We cannot reproduce problems locally. All items are shown correctly sorted by Publication_Date DESC.
    Also check which items are returned by your filter, because these items are then filtered by Publication_Date DESC.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  13. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    03 Feb 2010
    Link to this post
    Ivan,

    How is Linq going to solve anything?

    As I've mentioned in my previous post, we're in debug mode, we can see that there are ONLY 10 items returned from the GetContent method and some of the articles does not show up. AT ALL. It's not in that list of 10 items. In this case article number 2

    IList list = _NewsManager.Content.GetContent(0, _MaxItems, "Publication_Date DESC", filters);


    In this line of code... if I change the value of _MaxValue from 10 to 5 then the article order changes yet again. We now have 1, 7, 8, 9, 10.... what happened to 1, 2, 3, 4, 5? This is all just from changing the value of _MaxItems!

    If we change the value back to 10 the order of articles is: 1,3,4,5,6,7,8,9,10,11. Article 2 is missing.
    If we change the value to 15 the order of articles is: 1,2,3,4,5,6,7,8,9,10,11,13,14,15,16. Article 12 is missing
    If we change the value to 3 the order of articles is: 7, 8, 10. Totally incorrect!!!!!!!!!

    When you say you're testing that locally. Are you testing Sitefinity 3.6 on Oracle 10g?
    (Please don't advise us to upgrade - it's not an option right now)

    Please look at this post carefully. The ONLY thing I'm changing when I do these tests is the value of the variable _MaxItems. How is it possible that GetContent can return such erratic results????

    Jacques
  14. snehal kadiya
    snehal kadiya avatar
    3 posts
    Registered:
    09 Mar 2010
    17 Mar 2010
    Link to this post
    I think LINQ have some inbulit bug by microsoft.
Register for webinar
14 posts, 0 answered