Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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, 
                    SPUtility.CreateISO8601DateTimeFromSystemDateTime(startDate), 
                    SPUtility.CreateISO8601DateTimeFromSystemDateTime(endDate),
                    ContentTypes.NewsBase_ID);

            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
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.