SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following method which gets the old news items and displays in the archive page. I am displaying the old news items by month wise. User will click the Month URL and then he can see the available old news items in an archive page. Unfortunately, this method is not displaying the news item which has created on 1st day of each month. Could someone help me to get this fixed please?

public List<NewsItem> GetArchivedNews(SPList list)
        SPQuery query = new SPQuery();
        SPListItemCollection qResult;
        // 1. Get olderst news item
        query.Query = string.Format(NewsQueryConstants.OldestItems_ByBaseType, SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now), ContentTypes.NewsBase_ID);
        query.RowLimit = 1;
        qResult = list.GetItems(query);
        if (qResult.Count <= 0)
            // No result, return empty list
            return new List<NewsItem>();
        List<NewsItem> resultList = new List<NewsItem>();
        // Get the results for the first entry
        NewsItem newsItem = new NewsItem(qResult[0]);            
        DateTime startDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1, 0, 0, 1); // First day of this month - 1 min after midnight
        DateTime endDate = DateTime.Now; // End date should be now - we don't future events from current month
        DateTime compareTo = new DateTime(newsItem.StartDate.Year, newsItem.StartDate.Month, 1, 0, 0, 1); // First day of month - 1 min after midnight

        // 2. Loop through the dates back till first date with news
        while (startDate >= compareTo)
            query = new SPQuery();
            query.RowLimit = 1;
            query.Query = string.Format(NewsQueryConstants.ItemsTimespan_ByBaseType, 

            qResult = list.GetItems(query);
            if (qResult.Count > 0)
                resultList.Add(new NewsItem(qResult[0]));
            // Get dates for previous month
            endDate = startDate.AddMinutes(-2); // End date is the last day of previous month
            startDate = startDate.AddMonths(-1); // Start date is the start date of previous month
        return resultList;
share|improve this question

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.