wss3 sdk学习笔记三:一些常用代码
返回一个网站集下的全部网站以及网站下面的列表名称的代码:
SPSite mySite = SPContext.Current.Site;
SPWebCollection subSites = mySite.AllWebs;
for (int i=0;i<subSites.Count;i++)
{
SPListCollection lists = subSites[i].Lists;
for (int j=0;j<lists.Count;j++)
{
Response.Write(SPEncode.HtmlEncode(subSites[i].Title)
+ " :: " + SPEncode.HtmlEncode(lists[j].Title) + "<BR>");
}
}
SPWebCollection subSites = mySite.AllWebs;
for (int i=0;i<subSites.Count;i++)
{
SPListCollection lists = subSites[i].Lists;
for (int j=0;j<lists.Count;j++)
{
Response.Write(SPEncode.HtmlEncode(subSites[i].Title)
+ " :: " + SPEncode.HtmlEncode(lists[j].Title) + "<BR>");
}
}
新建立一个子网站的代码:
SPWeb mySite = SPContext.Current.Web;
SPWebCollection subSites = mySite.Webs;
string currentTemplate = mySite.WebTemplate;
string siteUrl = TextBox1.Text.ToString();
string siteTitle = TextBox2.Text.ToString();
string siteDescription = TextBox3.Text.ToString();
subSites.Add(siteUrl, siteTitle, siteDescription, 1033,
currentTemplate, true, false);
SPWebCollection subSites = mySite.Webs;
string currentTemplate = mySite.WebTemplate;
string siteUrl = TextBox1.Text.ToString();
string siteTitle = TextBox2.Text.ToString();
string siteDescription = TextBox3.Text.ToString();
subSites.Add(siteUrl, siteTitle, siteDescription, 1033,
currentTemplate, true, false);
删除一个子网站的代码:
string deleteSite = TextBox1.Text.ToString();
SPSite mySite = SPContext.Current.Site;
SPWebCollection sites = mySite.AllWebs;
sites.Delete(deleteSite);
SPSite mySite = SPContext.Current.Site;
SPWebCollection sites = mySite.AllWebs;
sites.Delete(deleteSite);
新建立一个网站集的代码:
SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
SPSiteCollection siteCollections = webApplication.Sites;
siteCollections.Add("http://Server_Name/sites/Site_Collection_Name",
"User_Name","User_Email ");
SPSiteCollection siteCollections = webApplication.Sites;
siteCollections.Add("http://Server_Name/sites/Site_Collection_Name",
"User_Name","User_Email ");
新建立网站的一些元素:
SPWeb mySite = SPContext.Current.Web;
SPListCollection lists = mySite.Lists;
string listTitle = TextBox1.Text;
string listDescription = TextBox2.Text;
string listType = ListBox1.SelectedItem.Text;
SPListTemplateType listTemplateType = new SPListTemplateType();
switch(listType)
{
case "Generic List":
{
listTemplateType = SPListTemplateType.GenericList;
break;
}
case "Events":
{
listTemplateType = SPListTemplateType.Events;
break;
}
case "Announcements":
{
listTemplateType = SPListTemplateType.Announcements;
break;
}
}
lists.Add(listTitle, listDescription, listTemplateType);
SPListCollection lists = mySite.Lists;
string listTitle = TextBox1.Text;
string listDescription = TextBox2.Text;
string listType = ListBox1.SelectedItem.Text;
SPListTemplateType listTemplateType = new SPListTemplateType();
switch(listType)
{
case "Generic List":
{
listTemplateType = SPListTemplateType.GenericList;
break;
}
case "Events":
{
listTemplateType = SPListTemplateType.Events;
break;
}
case "Announcements":
{
listTemplateType = SPListTemplateType.Announcements;
break;
}
}
lists.Add(listTitle, listDescription, listTemplateType);
应用模版来建立列表:
SPWeb mySite = SPContext.Current.Web;
SPListTemplate template = mySite.ListTemplates["Decisions"];
mySite.Lists.Add("My Decisions", "This is a list of decisions",
template);
SPListTemplate template = mySite.ListTemplates["Decisions"];
mySite.Lists.Add("My Decisions", "This is a list of decisions",
template);
删除一个列表(需提供列表的GUID ):
SPWeb mySite = SPContext.Current.Web;
SPListCollection lists = mySite.Lists;
SPList list = lists[TextBox1.Text];
System.Guid listGuid = list.ID;
lists.Delete(listGuid);
SPListCollection lists = mySite.Lists;
SPList list = lists[TextBox1.Text];
System.Guid listGuid = list.ID;
lists.Delete(listGuid);
返回一个列表的全部项目数据:
SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
for (int i=0;i<listItems.Count;i++)
{
SPListItem item = listItems[i];
Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + " : " +
SPEncode.HtmlEncode(item["Location"].ToString()) + " : " +
SPEncode.HtmlEncode(item["Begin"].ToString()) + " : " +
SPEncode.HtmlEncode(item["End"].ToString()) + "<BR>");
}
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
for (int i=0;i<listItems.Count;i++)
{
SPListItem item = listItems[i];
Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + " : " +
SPEncode.HtmlEncode(item["Location"].ToString()) + " : " +
SPEncode.HtmlEncode(item["Begin"].ToString()) + " : " +
SPEncode.HtmlEncode(item["End"].ToString()) + "<BR>");
}
返回符合条件的列表的项目的数据:
SPWeb mySite = SPContext.Current.Web;
SPList list = mySite.Lists["Books"];
SPQuery query = new SPQuery();
query.Query = "<Where><Gt><FieldRef Name='Stock'/><Value Type='Number'>100</Value></Gt></Where>";
SPListItemCollection myItems = list.GetItems(query);
foreach (SPListItem item in myItems)
{
Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + "<BR>");
}
SPList list = mySite.Lists["Books"];
SPQuery query = new SPQuery();
query.Query = "<Where><Gt><FieldRef Name='Stock'/><Value Type='Number'>100</Value></Gt></Where>";
SPListItemCollection myItems = list.GetItems(query);
foreach (SPListItem item in myItems)
{
Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + "<BR>");
}
在列表中新增加一个项目
SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
SPListItem item = listItems.Add();
item["Title"] = TextBox2.Text;
item["Stock"] = Convert.ToInt32(TextBox3.Text);
item["Return Date"] = Convert.ToDateTime(TextBox4.Text);
item["Employee"] = TextBox5.Text;
item.Update();
}
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
SPListItem item = listItems.Add();
item["Title"] = TextBox2.Text;
item["Stock"] = Convert.ToInt32(TextBox3.Text);
item["Return Date"] = Convert.ToDateTime(TextBox4.Text);
item["Employee"] = TextBox5.Text;
item.Update();
}
删除列表中的一个项目:
SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
int itemCount = listItems.Count;
for (int k=0; k<itemCount; k++)
{
SPListItem item = listItems[k];
if (TextBox2.Text==item["Employee"].ToString())
{
listItems.Delete(k);
}
}
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
int itemCount = listItems.Count;
for (int k=0; k<itemCount; k++)
{
SPListItem item = listItems[k];
if (TextBox2.Text==item["Employee"].ToString())
{
listItems.Delete(k);
}
}