sharepoint2010获取最新发布文档并按照时间排序显示

首先,这个sp中内容查询webparts也是可以完成这样的功能,只需要自己配置就好了,但是如果你需要更炫的效果,可以自己手动获取

代码如下

 1   protected DataTable table = new DataTable(); 
 2         protected void Page_Load(object sender, EventArgs e)
 3         { 
 4             if (!IsPostBack)
 5             {
 6                 table.Columns.Add(new DataColumn("linkurl"));
 7                 table.Columns.Add(new DataColumn("title"));
 8                 table.Columns.Add(new DataColumn("imgurl"));
 9                 table.Columns.Add(new DataColumn("author"));
10                 table.Columns.Add(new DataColumn("authorID"));
11                 table.Columns.Add(new DataColumn("date", Type.GetType("System.DateTime")));
12                 table.Columns.Add(new DataColumn("folder"));
13                 table.Columns.Add(new DataColumn("folderurl"));
14                 using (SPWeb web = SPContext.Current.Site.OpenWeb())
15                 {  
16                     for (int i = 0; i < web.Lists.Count; i++)
17                     {
18                         try
19                         {
20                             SPDocumentLibrary list = (SPDocumentLibrary)web.Lists[i];
21                             SPFolder folder = list.RootFolder;
22                             if (folder.Name != "fpdatasources" && folder.Name != "masterpage" && folder.Name!="Documents")
23                             {
24                                 SPFileCollection files = folder.Files;
25                                 foreach (SPFile file in files)//获取第一级文件也就是docs中的文件名
26                                 {
27                                     DataRow row = table.NewRow();
28                                     row["linkurl"] = file.Url;
29                                     row["title"] = file.Title;
30                                     row["imgurl"] = file.IconUrl;
31                                     row["author"] = file.Author.Name;
32                                     row["authorID"] = file.Author.ID;
33                                     row["date"] = file.TimeCreated;
34                                     row["folder"] = folder.Name;
35                                     row["folderurl"] = folder.ServerRelativeUrl;
36                                     table.Rows.Add(row);
37                                 }
38                                 EnumerateFolders(folder.SubFolders);
39                             }
40                         }
41                         catch (Exception ex)
42                         {
43                         }
44                     }
45                 }
46                 DataView Docs = new DataView(table);
47                 Docs.Sort = "date Desc";
48                 string filter = string.Empty;
49                 string delimiter = string.Empty;
50                 int numDocs =10;
51                 numDocs = Math.Min(numDocs, Docs.Count);
52                 for (int i = 0; i < numDocs; i++)
53                 {
54                     filter += delimiter + "'" + Docs[i]["linkurl"].ToString() + "'"; delimiter = ",";
55                 }
56                 if (filter.Length > 0)
57                 {
58                     Docs.RowFilter = "linkurl in (" + filter + ")";
59                 }
60                 rptList.DataSource = Docs;
61                 rptList.DataBind();
62             }
63         }
64 
65         //下面的方法是迭代获取文档库中的所有文件夹及其文件夹内的文件
66         private void EnumerateFolders(SPFolderCollection folders)
67         {
68             foreach (SPFolder subFolder in folders)//获取文件夹中的文件
69             {
70                 if ( subFolder.Name != "Forms" && subFolder.Name != "masterpage" && subFolder.Name!="fpdatasources")
71                 {
72                     SPFileCollection subFiles = subFolder.Files;
73                     foreach (SPFile file in subFiles)
74                     {
75                         DataRow row = table.NewRow();
76                         row["linkurl"] = file.Url;
77                         row["title"] = file.Title;
78                         row["imgurl"] = file.IconUrl;
79                         row["author"] = file.Author.Name;
80                         row["authorID"] = file.Author.ID;
81                         row["date"] = file.TimeCreated;
82                         row["folder"] = subFolder.Name;
83                         row["folderurl"] = subFolder.Url;
84                         table.Rows.Add(row);
85                     }
86                     SPFolderCollection subFolders = subFolder.SubFolders;//获取文件夹中的子文件夹
87                     EnumerateFolders(subFolders);
88                 }
89             }
90         }

 

 

posted @ 2012-07-12 10:55  Leochen  阅读(379)  评论(0编辑  收藏  举报