sharepoint常见操作
1.获取左右应用程序
SPWebApplicationCollection webs = SPWebService.ContentService.WebApplications;
foreach (SPWebApplication web in webs)
{
listApplication.Items.Add(web.DisplayName);//获取所有应用程序
}
2. 获取列表各列表项的标题字段
SPSite site = new SPSite("http://sharepoint:81/");
SPWeb spweb = site.OpenWeb();
SPList list = spweb.Lists["DepartmentList"];
SPListItemCollection listitems = list.Items;
foreach (SPListItem item in listitems)
{
txtList.Text += item["标题"].ToString() + Environment.NewLine;//获取部门列表标题字段
}
3.删除列表
SPSite site = new SPSite("http://sharepoint:81/");
SPWeb spweb = site.OpenWeb();
SPListCollection lists= spweb.Lists;
SPList list = lists["DepartmentList"];
System.Guid listguid = list.ID;
lists.Delete(listguid);
4.操作视图
SPSite site = new SPSite("http://sharepoint:81/");
SPWeb spweb = site.OpenWeb();
SPList list = spweb.Lists["DepartmentList"];
SPViewCollection listitems = list.Views;
foreach (SPView item in listitems)
{
this.txtList.Text += item.Title + Environment.NewLine;
}
Guid viwGuid = list.Views["我的日常工作"].ID;
SPView sv = list.GetView(viwGuid);
SPListItemCollection collItem = list.GetItems(sv);
SPViewFieldCollection collViewFields = sv.ViewFields;
foreach (SPListItem oitem in collItem)
{
for (int index = 0; index < collViewFields.Count; index++)
{
this.txtList.Text += collViewFields[index].ToString() + ":" + oitem[collViewFields[index]].ToString() + Environment.NewLine;
}
}
5.列表转换为DataTable
SPSite site = new SPSite("http://sharepoint:81/");
SPWeb spweb = site.OpenWeb();
SPList list = spweb.Lists["DepartmentList"];
SPListItemCollection listitems = list.Items;
return listitems.GetDataTable();
6.操作文档库
SPSite site = new SPSite("http://sharepoint:81/");
SPWeb spweb = site.OpenWeb();
SPFolder myFolder = spweb.GetFolder("Doc");
SPFileCollection myfiles = myFolder.Files;
FileStream fstream = File.OpenRead(@"C:\aaa.txt");
byte[] contens = new byte[fstream.Length];
fstream.Read(contens, 0, (int)fstream.Length);
fstream.Close();
string path = site.Url + "/kb/kb/";
string filename = DateTime.Now.ToString().Replace(" ", "").Replace(":", "") + ".txt";
SPFile file = myfiles.Add(path + filename, contens);
file.Item["知识库标题"] = "测试知识库";
file.Item["内容"] = "计算机是以非常复杂的工作";
file.Item["分类"] = "4";
file.Item.Update();
7.操作用户用户组
SPSite site = new SPSite("http://sharepoint:81/");
SPWeb spweb = site.OpenWeb();
foreach (SPUser user in spweb.SiteUsers)
{
this.txtList.Text += user.Name + Environment.NewLine;
}
foreach (SPGroup group in spweb.Groups)
{
this.txtList.Text += group.Name + Environment.NewLine;
}
8.获取MOss内置名称 在CAML语法中使用的字段名并不是创建的字段名而是Moss系统内部名称
SPSite site = new SPSite("http://sharepoint:81/");
SPWeb spweb = site.OpenWeb();
SPList list = spweb.Lists["DepartmentList"];
SPListItemCollection listitems = list.Items;
SPListItem item = listitems[0];
for (int i = 0; i < item.Fields.Count; i++)
{
this.txtList.Text += item.Fields[i].Title + ":" + item.Fields[i].InternalName + Environment.NewLine;
}
9.CAML查询
SPSite site = new SPSite("http://sharepoint:81/");
SPWeb spweb = site.OpenWeb();
SPList list = spweb.Lists["DepartmentList"];
SPQuery query = new SPQuery();
query.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='_x95ee_x9898_x51b3_x65'/><FieldRef Name='Created'/>";
query.Query = "<Where><Contains><FieldRef Name='Title'/><value Type='Text'>计算机</value></Contains></Where>";
query.RowLimit = 5;
SPListItemCollection myiten = list.GetItems(query);
10. 得到网站集
SPSite site=new SPSite(“http://yang:9000”)
使用AllWebs得到子站点kb
SPWeb web1=site.AllWebs[“kb”];
MessageBox.Show(web1.Title)
使用OpenWeb获取当前网站集下的知识库子站点
SPWeb web2=site.OpenWeb(“kb”)
OpenWeb方法中的参数是一个相对地址
利用SPControl控制的上下文
SPWeb web=SPControl.GetContextWeb(Context)
利用WSS对象的上下文SPContent
SPWeb web=SPContext.Current.Web
SPWeb web=SPControl.GetContextWeb(Context).webs[“kb”]
SPList treelist=web.Lists[listname]
SPWeb rootweb=SPControl.GetContextSite(Context).RootWeb
列表对象
SPListCollection SPListItem SPListItemCollection
SPViewCollection
SPViewFiledCollection
SPView
SPDocumentLibrary
SPFiled
SPFiledCollection