首先让我们看看MOSS2007整个对象模型架构
从最内层开始出发
SPListItem 创建Item,这里需要注意的一点是这里的name参数需要用internal names
Code 1 SPListItem item = list.Items.Add(); 2 item["Title"] = "Ann Simms"; 3 item["First_x0020_Name"] = "Ann"; 4 item["Last_x0020_Name"] = "Simms"; 5 item["EmailAddress"] ="
[email protected]"; 6 item.Update();
SPListItemCollection 获取List的列表项的集合,具体方法为
Code list.Items.GetDataTalbe();
SpQuery 结合CAML 查询制定列表或者站点下某一类列表项
1.制定列表下
Code 1 SPWeb web = SPContext.Current.Web; 2 SPList list = web.Lists[“Announcements”]; 3 SPQuery query = new SPQuery(); 4 string date = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today); 5 qry.Query = "<OrderBy><FieldRef Name='Title' /></OrderBy>" + 6 "<Where><Geq><FieldRef Name='Created' />" + 7 "<Value Type='DateTime'>" + date + "</Value></Geq></Where>"; 8 SPListItemCollection results = list.GetItems(query); 9
2.站点下某一类列表
Code 1 SPWeb web = SPContext.Current.Web; 2 SPSiteDataQuery qry = new SPSiteDataQuery(); 3 string date = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today); 4 qry.Query = "<OrderBy><FieldRef Name='Title' /></OrderBy>" + 5 "<Where><Geq><FieldRef Name='Created' />" + 6 "<Value Type='DateTime'>" + date + "</Value></Geq></Where>"; 7 qry.Lists = "<Lists ServerTemplate='101' />"; 8 qry.ViewFields = "<FieldRef Name='Title' />"; 9 qry.Webs = "<Webs Scope='SiteCollection' />"; 10 DataTable tbl = web.GetSiteData(qry); 11
这里的ServerTemplate="101" 101表示查找的为普通list列表
对文档库的部分操作
文档库之间文件的拷贝,做了个简单的Demo
Code 1 using (SPSite site = new SPSite("http://cpu/")) 2 { 3 try 4 { 5 6 //拷贝 7 SPWeb web = site.RootWeb; 8 //SPDocumentLibrary doclib = (SPDocumentLibrary)web.Lists["共享文档"]; 9 10 SPFile document = web.GetFile("DOC1/role.txt"); 11 document.CopyTo("http://cpu/Execel/role.txt", true); 12 13 Console.WriteLine("It's OK"); 14 } 15 16 catch (Exception ex) 17 { 18 Console.WriteLine(ex.Message); 19 } 20 21 finally 22 { 23 Console.ReadLine(); 24 } 25 }
文档库新增文件以及文档库文件的转移
Code 1 using (SPSite site = new SPSite("http://cpu/")) 2 { 3 try 4 { 5 //新增 6 SPWeb web = site.RootWeb; 7 SPList list = web.Lists["共享文档"]; 8 9 SPFile document = web.GetFile("Execel/5.gif"); 10 SPFile newdoc = list.RootFolder.Files.Add 11 (list.RootFolder.Url + "/" + document.Name, 12 document.OpenBinary(), document.Author, 13 document.ModifiedBy, document.TimeCreated, 14 document.TimeLastModified); 15 list.Update(); 16 17 //移动到 18 document.MoveTo("http://cpu/DOC1/5.gif", true); 19 20 21 } 22 23 catch (Exception ex) 24 { 25 Console.WriteLine(ex.Message); 26 } 27 finally 28 { 29 Console.ReadLine(); 30 } 31 }
在网上看到个遍历文档库下所有文件的方法,感觉比较不错,这里收下共享出来
Code 1 //调用 2 SPWeb web = site.RootWeb; 3 SPDocumentLibrary doclib = (SPDocumentLibrary)web.Lists["共享文档"]; 4 doclib.Lists.IncludeRootFolder = true; 5 GoThroughDocLib(doclib, doclib.RootFolder, 0); 6 //方法 7 static void GoThroughDocLib(SPDocumentLibrary doclib, SPFolder folder, int level) 8 { 9 SPListItemCollection items = doclib.GetItemsInFolder(doclib.DefaultView, folder); 10 if (items.Count == 0) 11 return; 12 13 for (int i = 0; i < level; i++) 14 Console.Write(" "); 15 foreach (SPListItem item in items) 16 { 17 if (item.FileSystemObjectType == SPFileSystemObjectType.Folder) 18 { 19 Console.WriteLine("[{0}]", item.Name); 20 GoThroughDocLib(doclib, item.Folder, level + 1); 21 } 22 else 23 Console.WriteLine(item.Name); 24 } 25 }
关于获取文档库信息的一些对象,这个帖子感觉比较全面Item