以匿名用户身份添加到 Sharepoint 列表
现在,我正在使用 SPSecurity.RunWithElevatedPrivileges 方法让匿名用户将列表项添加到列表中。我想做的是创建一个通用方法,它将站点、列表和列表项作为参数,并将该项添加到正在传递的列表中。现在我有:
public static void AddItemElevated(Guid siteID, SPListItem item, SPList list)
{
SPSite mySite = SPContext.Current.Site;
SPList myList = WPToolKit.GetSPList(mySite, listPath);
SPWeb myWeb = myList.ParentWeb;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite eleSite = new SPSite(mySite.ID))
{
using (SPWeb eleWeb = eleSite.OpenWeb(myWeb.ID))
{
eleWeb.AllowUnsafeUpdates = true;
SPList eleList = eleWeb.Lists[myList.Title];
SPListItem itemToAdd = list.Items.Add();
itemToAdd = item;
itemToAdd.Update();
eleWeb.AllowUnsafeUpdates = false;
}
}
});
}
问题是“item”在提升的权限之外初始化,因此当“itemToAdd”设置为“item”时,它会失去其提升的权限,导致代码在“item.update()”处中断,如果使用我的非特权用户。
有什么想法吗?
right now, I'm using the SPSecurity.RunWithElevatedPrivileges method to let anonymous users add list items to a list. What i would like to do is make a general method that takes a Site, List and List item as an argument and adds the item to the list being passed. Right now I have :
public static void AddItemElevated(Guid siteID, SPListItem item, SPList list)
{
SPSite mySite = SPContext.Current.Site;
SPList myList = WPToolKit.GetSPList(mySite, listPath);
SPWeb myWeb = myList.ParentWeb;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite eleSite = new SPSite(mySite.ID))
{
using (SPWeb eleWeb = eleSite.OpenWeb(myWeb.ID))
{
eleWeb.AllowUnsafeUpdates = true;
SPList eleList = eleWeb.Lists[myList.Title];
SPListItem itemToAdd = list.Items.Add();
itemToAdd = item;
itemToAdd.Update();
eleWeb.AllowUnsafeUpdates = false;
}
}
});
}
The problem is that 'item' gets initialized outside of the elevated privileges so when 'itemToAdd' is set to 'item' it loses its elevated privileges, causing the code to break at 'item.update()' if used my an non-privileged user.
Any Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题可能是因为您正在传递列表。尝试仅传递列表名称,然后从提升的网络中获取列表,如下所示:
The problem could be because you are passing in your list. Try just passing in the list name and then grabbing the list from the elevated web like this:
下面的行
itemToAdd = item;
做了一些奇怪的事情 - 您将项目添加到一个列表(使用list.Items.Add()
),但从另一个列表/位置更新项目(一个作为参数出现)。不确定您真正想要什么,但也许您想将所有文件从
item
复制到itemToAdd
。在这种情况下,请考虑将字段名称/值对作为参数传递,以明确您正在添加具有给定值的新项目。请注意,匿名用户可以将项目添加到明确允许的列表中。
Following line
itemToAdd = item;
does something strange - you adding item to one list (withlist.Items.Add()
) but updating item from another list/location (one that comes as argument).Not sure what you actually want, but maybe you want to co copy all fileds from
item
toitemToAdd
. Consider in this case to pass fieldName/value pairs as argument to make it clear that you are adding new item with given values.Note, that anonymous users are able to add items to lists that explicitly allow it.
我还没有尝试过,但这可能会有所帮助 - http ://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.copyto.aspx
问候,
尼廷·拉斯托吉
I haven't tried it but possibly this could help - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.copyto.aspx
Regards,
Nitin Rastogi
如果
item
来自 SPList.AddItem() 方法,则必须从提升的 Web 获取 splist 实例。否则这个代码对于匿名用户来说总是会被破坏。或者您可以允许匿名用户将项目添加到列表中,这样您就不需要以提升的权限运行代码。
顺便说一句,
itemToAdd = item;
不是将新添加的项目设置到旧实例的正确方法。If
item
is coming from an SPList.AddItem() method, the splist instance must be get from an elevated web. otherwise this code will always break for anonymous users.or you can allow anonymous user to add item to list, so you won't need running the code with elevated privileges.
by the way,
itemToAdd = item;
is not a correct way of setting the new added item to an old instance.