1. query all fields in the list
Spsite site = new spsite ("http: // carysun"); spweb = site. openweb (); splist list = web. getlist ("/IT Infrastructure"); foreach (spfield SF in list. fields) {console. writeline (SF. title );}
2. Create a list using the Object Model, Splisttemplatetype. Announcements specifies to use the notification content type as the template to create.
Be sure to call the update () method.
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. Use the object model to add an item to the listYou must call the update () method.
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. query item-related information
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. If you want to get a list item, you can get it through 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. Use spquery to query the item information in the list:
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 indicates the field to be returned after your query
6.2. query indicates the expression used for query filtering. It uses the caml language.
7. Use spsitedataquery to query item information in the list
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 = '000000'/>"; "104" indicates the notification list type.
7.2 query. webs = "<webs scope = 'sitecollect'/>"; indicates the query range.
7.3. the basic format of caml is as follows: "<where> <operator> <operand/> </operator> </where> ".
. Splistitemcollection is returned using spquery, and spsitedataquery can be queried from different lists or the entire website set. In fact, An ADO. Net datatable object is returned.
7.5 The following table describes some simple descriptions of caml queries:
Element |
Description |
And |
And |
Beginswith |
Starting with a string |
Contains |
Contains a string |
EQ |
Equal |
Fieldref |
Reference of a field (used in groupby) |
Geq |
Greater than or equal |
Groupby |
Group |
GT |
Greater |
Isnotnull |
Not empty |
Isnull |
Null |
Leq |
Less than or equal |
Lt |
Less |
NEQ |
Not equal |
Now |
Current Time |
Or |
Or |
Orderby |
Sort |
Today |
Today's date |
Todayiso |
Today's date (ISO format) |
Where |
Where clause |