概要
在sharepoint中的自定义web part会涉及到对list里面的item进行操作,这篇文单将会讲解如何修改list里的item。
我们先看一个示例
/// <summary>
///
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
public SPListItemCollection GetSPListItemCollection(stringID)
{
SPListItemCollection itemCollection =newSPListItemCollection();
SPSecurity.RunWithElevatedPrivileges(
delegate
{
using(SPSite spsite =newSPSite(this.CurrentUrl))
{
using(SPWeb spWeb = spsite.OpenWeb())
{
SPList listInstance = spWeb.Lists[this.ListName];
SPQuery query =newSPQuery();
query.Query ="<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>"+ ID +"</Value></Eq></Where>";
itemCollection = listInstance.GetItems(query);
}
}
}
);
returnitemCollection;
}
SPSecurity.RunWithElevatedPrivileges()是用来提高操作权限级别来操作站点!
用法:
SPSecurity.RunWithElevatedPrivileges(delegate() {代码});提高权限将是让代码以sharepoint\system所具有的权限来执行!
更改指定的item代码
public string AddNewItem()
{
stringretVal =string.Empty;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site =newSPSite(SPContext.Current.Web.Url))
{
using(SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["TEST"];
SPListItem item = list.Items.Add();
item["Title"] =string.Format("Test at {0}", DateTime.Now.ToString());
item.Update();
}
}
retVal = "operation success!";
});
}
catch(Exception ex)
{
retVal += ex.Message;
}
returnretVal;
}
如果修改的不留痕迹,要使用SystemUpdate;
示例如下:
在很多时候我们需要设置允许不安全的修改。使用这个AllowUnsafeUpdates.
示例如下:
public string AddNewItem()
{
stringretVal =string.Empty;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site =newSPSite(SPContext.Current.Web.Url))
{
using(SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates =true;
SPList list = web.Lists["TEST"];
SPListItem item = list.Items.Add();
item["Title"] =string.Format("Test at {0}", DateTime.Now.ToString());
item.SystemUpdate();
web.AllowUnsafeUpdates =false;
}
}
retVal = "operation success!";
});
}
catch(Exception ex)
{
retVal += ex.Message;
}
returnretVal;
}
在sharepoint里面删除一条item的话可以进行如下操作.
public void DelState(string listID, string itemID)
{
string siteCol = System.Configuration.ConfigurationManager.AppSettings["siteCol"];
SPSecurity.RunWithElevatedPrivileges(delegate() //虚拟管理员,否则其他用户访问没有权限
{
using (SPSite site = new SPSite(siteCol))
{
using (SPWeb web = site.OpenWeb("expense"))
{
SPList list = web.Lists["EBRL"];
//允许修改list
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
//填充list表单
SPQuery query = new SPQuery();
query.Query = string.Format(@"<Where><And><Eq><FieldRef Name='ListID' />
<Value Type='Text'>{0}</Value>
</Eq><Eq><FieldRef Name='ItemID' />
<Value Type='Text'>{1}</Value></Eq></And></Where>", listID, itemID);
SPListItemCollection items = list.GetItems(query);
//拒绝后删除记录
items[0].Delete();
}
}
}
);
}
如果只取item的发布过的版本的数据,可以做如下处理.
总结
sharepoint 对list的操作,主要有增加、删除和修改list里面的item数据,要注意的是这里打开的是当前操作的site。SPContext.Current.Web.Url取的是当前打开页面的值
在SharePoint2010中,列表在API对象中称为SPList,如果代码运行在SharePoint环境内部,则可以通过上下文对象SPContext来获取列表对象SPList。跟获取当前网站集对象SPSite和网站对象SPWeb的方法一样。
下面我们这段代码表示获取当前网站集当前网站下的当前列表对象SPList
由于列表是依赖于网站存在的,所以获取列表对象必须在获取网站对象之后才可以获取。
获取到SPList对象之后就可以操作列表及列表项中的内容了。
//获取当前网站集对象
using(SPSite site=SPContext.Current.Site)
{
//获取当前网站对象
using(SPWeb web=SPContext.Current.Web)
{
//获取当前列表对象
SPList list=SPContext.Current.List;
}