[Web Part]显示新闻列表

在首页显示新闻的前10条,核心代码,
   // get the reference
   SPSite site = new SPSite("http://localhost"); // this will point to the portal URL
      SPWeb web = site.OpenWeb();
   
   // get the context
   PortalContext context = PortalApplication.GetContext(site.ID);  // context got using the site GUID
   Guid newsGuid = AreaManager.GetSystemAreaGuid(context, SystemArea.News);

   // SystemArea.News is a Sharepoint enumeration already available through the API
   Area newsarea = AreaManager.GetArea(context, newsGuid);
   AreaListingCollection newscoll = newsarea.Areas["内部新闻"].Listings;        // fetches all listings that are part of the current area
         
   //output.Write(newscoll.Count+"<br>");
   DataTable areaTable = new DataTable("IntranetList");
   DataColumn idColumn = new  DataColumn();
   idColumn.DataType = System.Type.GetType("System.DateTime");
   idColumn.ColumnName = "Date";
   areaTable.Columns.Add(idColumn);
   DataColumn fTitleColumn = new DataColumn();
   fTitleColumn.DataType = System.Type.GetType("System.String");
   fTitleColumn.ColumnName = "Title";
   areaTable.Columns.Add(fTitleColumn);
   DataColumn fURLColumn = new DataColumn();
   fURLColumn.DataType = System.Type.GetType("System.String");
   fURLColumn.ColumnName = "URL";
   areaTable.Columns.Add(fURLColumn);
 
   DataRow row = null;
   for(uint i=0;i<newscoll.Count;i++)
   {
    row = areaTable.NewRow();
    row["Date"] = newscoll[i].CreationDate ;
    row["Title"] = newscoll[i].Title;
    row["URL"] = newscoll[i].URL;
    areaTable.Rows.Add(row);
   } 
   DataRow[] areaRows = areaTable.Select(null,"Date DESC");

   //取十条最新的信息
   for(uint i=0;i<10;i++)
   {
                   //output.Write(areaRows[i][1].ToString()+"<br>");

        if (areaRows[i][2].ToString().Substring(areaRows[i][2].ToString().Length-3,3)=="pdf" )
        output.Write("<img src='http://localhost/_layouts/images/pdf16.gif' align=bottom>"+"<a href=''http://localhost"+areaRows[i][2] + "'>" + areaRows[i][1] +"</a>["+areaRows[i][0] +"]<br>");   //WriteToFile is a custom function used for debug logging to a file
        else
        output.Write("<img src=''http://localhost/Image%20Library/pic-07.gif' align=bottom>"+"<a href=''http://localhost"+areaRows[i][2] + "'>" + areaRows[i][1] +"</a>["+areaRows[i][0] +"]<br>");   //WriteToFile is a custom function used for debug logging to a file
   }
   output.Write("<br><a href=''http://localhost/CompanyNews/default.aspx'><font color='red'><b>[更多项目新闻]</b></font></a>");



参考资料:
排序方法来此 dudu  的 http://www.cnblogs.com/dudu/archive/2005/02/25/109317.html
新闻检索来此 harsh  的  http://blogs.msdn.com/harsh/archive/2005/07/26/443244.aspx

谢谢 dudu和harsh。

你可能感兴趣的:(part)