Click here to Skip to main content
13,352,298 members (43,037 online)
Rate this:
 
Please Sign up or sign in to vote.
Hi Guys!

How to display Latest and oldest news on site from SpList by date
try
           {
               using (SPSite site = new SPSite("http://wingtip:49074"))
               {

                   LabelShowTitle.Text = string.Empty;
                   LabelShowContents.Text = string.Empty;
                   LabelShowDatum.Text = string.Empty;
                   LabelAuthor.Text = string.Empty;
                   LabelLink.Text = string.Empty;

                   SPWeb web = SPContext.Current.Web;
                   SPList list = web.Lists.TryGetList(DropDownListSelectCategory.SelectedItem.Value);


                   DateTime dt = DateTime.Now;

                   SPListItemCollection items = list.GetItems();

                   foreach (SPListItem news in items)

                   {




                           TextBoxContents.Text = news["Contents"].ToString();

                           LabelShowTitle.Text = news["Title"].ToString();
                           //LabelShowContents.Text = news["Contents"].ToString();
                           LabelShowDatum.Text = news["Date"].ToString();
                           LabelAuthor.Text = news["Authors"].ToString();
                           LabelLink.Text = new SPFieldUrlValue(news["Links"].ToString()).Url;











                       }
                   site.Dispose();
                   }

           }
           catch(Exception x)
           {

               LabelError.Text = x.Message;
           }

           }
Posted 22-Jan-13 1:44am
Kurac1866
Comments
phil.o 22-Jan-13 9:10am
   
site.Dispose() is useless here since variable is already enclosed in a using block.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

try
            {
                using (SPSite site = new SPSite("http://wingtip:49074"))
                {
 
                    LabelShowTitle.Text = string.Empty;
                    LabelShowContents.Text = string.Empty;
                    LabelShowDatum.Text = string.Empty;
                    LabelAuthor.Text = string.Empty;
                    LabelLink.Text = string.Empty;
 
                    SPWeb web = SPContext.Current.Web;
                    SPList list = web.Lists.TryGetList(DropDownListSelectCategory.SelectedItem.Value);
 
                     
                    SPListItemCollection items = list.GetItems();
                    items= items.OrderByDescending(e=>e.Date).ToList();
 
                    foreach (SPListItem news in items)
                        
                    {
                            TextBoxContents.Text = news["Contents"].ToString();
                           
                            LabelShowTitle.Text = news["Title"].ToString();
                            //LabelShowContents.Text = news["Contents"].ToString();
                            LabelShowDatum.Text = news["Date"].ToString();
                            LabelAuthor.Text = news["Authors"].ToString();
                                LabelLink.Text = new SPFieldUrlValue(news["Links"].ToString()).Url;
                        }
                    site.Dispose();
                    }                  
                
            }
            catch(Exception x)
            {
 
                LabelError.Text = x.Message;
            }
 
            }
  Permalink  
v2
Comments
Kurac1 22-Jan-13 8:53am
   
hi! i get , on this line items= items.OrderByDescending(e=>e.Date).ToList(); // Red on .Date? not working?
Kishor Deshpande 22-Jan-13 8:55am
   
Is Date one of property in your list type SPListItem ??
Kurac1 22-Jan-13 8:56am
   
Cant find OrderByDescending
Kurac1 22-Jan-13 8:56am
   
its an Column
Kurac1 22-Jan-13 8:56am
   
With DateTime.Now on it
Kurac1 22-Jan-13 8:59am
   
here u se how it looks like http://sharepoint.stackexchange.com/questions/57667/display-items-from-splist-in-textboxes-only-takes-one-item-not-two
Kishor Deshpande 22-Jan-13 9:00am
   
var sortedItems= from element in items
orderby element.Date descending
select element;

can you try with this?

Then, first and last item display....

sortedList.Item[0]; // first item..
int count = sortedList.Count;
sortedList.Item[count-1]; //last item

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.180111.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100