Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Guys!

How to display Latest and oldest news on site from SpList by date
C#
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
Comments
phil.o 22-Jan-13 9:10am    
site.Dispose() is useless here since variable is already enclosed in a using block.

1 solution

C#
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;
            }
 
            }
 
Share this answer
 
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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900