需要在AssemblyInfo.cs中加入[assembly:AllowPartiallyTrustedCallers] (经常会忘了加)
2、用普通用户执行webpart时可能权限不够,需要提升权限
当将webpart部署到站点后,如果用的是管理员的帐户来运行,则成功;如果用一般用户,则跳转到用户无权访问的页面。通过查找资料,终于找出了问题所在:如果用户没有“管理列表”的权限,则无法更新List、View、ListItem等。给一般用户“管理列表”的权限显然是不可行的,那如何提升用户的权限呢?
在wss 3.0中提供了一种更快捷的方法,采用RunWithElevatedPrivileges ,如下:
Code
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb thisWeb = site.OpenWeb(SPContext.Current.Web.ID))
{
thisWeb.AllowUnsafeUpdates = true;
SPList list = thisWeb.Lists["任务"];
spView = list.DefaultView;
string query = "<Where><Eq><FieldRef Name=\"Status\"/><Value Type=\"Text\">" + dropDownList.SelectedValue + "</Value></Eq></Where>";
spView.Query = query;
//一般用户没有Update()的权限,所以在这里提升用户权限
spView.Update();
thisWeb.Dispose();
}
site.Dispose();
}
}
);
3、在webpart中取得当前登录用户
SPWeb web = SPControl.GetContextWeb(Context);
Label1.Text = web.CurrentUser.Name;