Click here to Skip to main content
Rate this: bad
good
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 0:44am
Kurac1602
Comments
phil.o at 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 at 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 at 22-Jan-13 8:55am
   
Is Date one of property in your list type SPListItem ??
Kurac1 at 22-Jan-13 8:56am
   
Cant find OrderByDescending
Kurac1 at 22-Jan-13 8:56am
   
its an Column
Kurac1 at 22-Jan-13 8:56am
   
With DateTime.Now on it
Kurac1 at 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 at 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
0 Maciej Los 225
1 OriginalGriff 145
2 manoj kumar choubey 115
3 _Amy 115
4 Sergey Alexandrovich Kryukov 105
0 OriginalGriff 7,320
1 Sergey Alexandrovich Kryukov 6,043
2 Maciej Los 3,749
3 Peter Leow 3,408
4 DamithSL 2,555


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2014
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