1.查询列表的所有字段
SPSite site = new SPSite("http://carysun"); SPWeb web=site.OpenWeb(); SPList list = web.GetList("/IT Infrastructure"); foreach (SPField sf in list.Fields) { Console.WriteLine(sf.Title); }
2.使用对象模型创建列表,SPListTemplateType.Announcements指定使用通知内容类型作为模板来创建。
注意一定要调用Update()方法。
string listName="AnnouList"; foreach(SPList currList in web .Lists) { if(currList.Title.Equals(listName,StringComparison.InvariantCultureIgnoreCase)) { list=currList; break; } } if(list==null) { Guid listID=web.Lists.Add(listName,"New nnouncements",SPListTemplateType.Announcements); list=web.Lists[listID]; list.OnQuickLaunch=true; list.Update(); }
3.使用对象模型给列表添加item,注意一定要调用Update()方法。
SPListItem newItem = null; newItem = list.Items.Add(); newItem["Title"] = "AnnouItem1"; newItem["Body"] = "The first AnnouItem1 "; newItem["Expires"] = DateTime.Now + TimeSpan.FromDays(2); newItem.Update(); newItem = list.Items.Add(); newItem["Title"] = "AnnouItem2"; newItem["Body"] = "The second AnnouItem2."; newItem["Expires"] = DateTime.Now + TimeSpan.FromDays(5); newItem.Update();
4.查询item的相关信息
foreach (SPListItem listItem in list.Items) { foreach (SPField field in list.Fields) { if (!field.Hidden && !field.ReadOnlyField) Console.WriteLine("{0} = {1}", field.Title, newItem[field.Id]); } }
5. 如果你要想得到一个列表的item,你可以通过WebId, ListId, and ID来得到.
SPWeb parentWeb = web.Site.OpenWeb(new Guid(row["WebId"].ToString())); SPList list = parentWeb.Lists[ new Guid(row["ListId"].ToString()) ]; SPListItem item = list.GetItemById((int.Parse(row["ID"].ToString())));
6.使用SPQuery来查询列表中item信息:
SPQuery query = new SPQuery(); query.ViewFields = @"<FieldRef Name='Title'/><FieldRef Name='Created'/>"; query.Query = @"<Where> <Neq> <FieldRef Name='Created' /> <Value Type='DateTime'> <Today /></Value> </Neq> </Where>"; SPList list = web.Lists["AnnouList"]; SPListItemCollection items = list.GetItems(query); foreach (SPListItem expiredItem in items) { Console.WriteLine(expiredItem["Title"]); Console.WriteLine(expiredItem["Created"]); }
6.1. ViewFields 表示你查询后要返回的字段
6.2. Query表示查询过滤的表达式,使用CAML语言
7. 使用SPSiteDataQuery来查询列表中item信息
SPSiteDataQuery query = new SPSiteDataQuery(); query.Lists = @"<Lists ServerTemplate='104' />"; query.ViewFields = @"<FieldRef Name='Title'/><FieldRef Name='Created'/>"; query.Webs = "<Webs Scope='SiteCollection' />"; string queryText =@"<Where> <Neq> <FieldRef Name='Created' /> <Value Type='DateTime'> <Today /></Value> </Neq> </Where>"; query.Query = queryText; DataTable table = web.GetSiteData(query); foreach (DataRow row in table.Rows) { Console.WriteLine(row["Title"].ToString() + row["Created"].ToString()); }
7.1. query.Lists = @"<Lists ServerTemplate='104' />";中104代表通知列表类型
7.2 query.Webs = "<Webs Scope='SiteCollection' />";为查询的范围。
7.3. CAML的基本格式是这样的:“<Where><operator><operand /><operand /></operator> </Where>”.
7.4.使用SPQuery返回的是SPListItemCollection,而SPSiteDataQuery可以从不同的列表或是整个网站集查,实际上是返回了一个ADO.NET DataTable对象。