如何通过 C# 代码更新 Sharepoint 2007 用户信息列表 (UIL)?我的代码在以管理员身份登录时工作正常,但是当以简单用户身份登录时,代码会抛出“拒绝访问”错误,因为简单用户无法访问 UIL。我需要一些方法让简单的用户在 UIL 中编辑他自己的信息(比如 EMAIL)。请帮忙。这很重要。我的代码是这样的:
string var_UserName = myUsername;
string var_newemail = mynewemail@abc.com;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = SPContext.Current.Site)
{
SPWeb curWeb = site.OpenWeb();
SPList userList = SPContext.Current.Web.SiteUserInfoList;
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.Append(@"<Query>");
queryBuilder.Append(@"<OrderBy>");
queryBuilder.Append(@"<FieldRef Name='Title' />");
queryBuilder.Append(@"</OrderBy>");
queryBuilder.Append(@"<Where>");
queryBuilder.Append(@"<Eq>");
queryBuilder.Append(@"<FieldRef Name='Title' />");
queryBuilder.Append(@"<Value Type='Text'>" + var_UserName + "</Value>");
queryBuilder.Append(@"</Eq>");
queryBuilder.Append(@"</Where>");
queryBuilder.Append(@"</Query>");
SPQuery query = new SPQuery();
query.Query = queryBuilder.ToString();
SPListItemCollection oSPListItemCollection = userList.GetItems(query);
SPField field = userList.Fields.GetField("E-Mail");
foreach (SPItem item in oSPListItemCollection)
{
if (item["Title"].ToString().ToLower() == Login1.UserName.ToLower())
{
item[field.InternalName] = var_newemail;
curWeb.AllowUnsafeUpdates = true;
item.Update(); //throws error when not logged in as administrator
curWeb.AllowUnsafeUpdates = false;
break;
}
}
}
});