【问题标题】:How do I run Commands with Elevated Privileges in Windows SharePoint Services 3.0?如何在 Windows SharePoint Services 3.0 中运行具有提升权限的命令?
【发布时间】:2010-02-11 20:00:00
【问题描述】:

我正在使用表单身份验证登录到 windows sharepoint servicevices 3.0 服务。 我需要在匿名访问期间提升权限以将记录添加到共享点门户列表。

我在 msdn 中找到了线索: http://msdn.microsoft.com/en-us/library/bb466220%28classic%29.aspx

但这对我不起作用。 :( 它仍然要求用户登录和密码。

有人可以帮帮我吗?

Public Function AddUserAccountData() As String
        SPSecurity.RunWithElevatedPrivileges(AddressOf AddUserAccountDataToSPList)
        Return ""
    End Function

    Private Sub AddUserAccountDataToSPList()
        Dim oSharedConfig As SharedConfig = SharedConfig.Instance
        Dim sListName As String = oSharedConfig.oWebPartsOpt.UserOpt.AccountVerificationList.Name

        Using oSite As SPWeb = SPContext.Current.Web
            Dim oUserAccStatusList As SPList = oSite.Lists(sListName)

            oUserAccStatusList.Items.Add()
            Dim oSPListItem As SPListItem = oUserAccStatusList.Items.Add()

            oSPListItem("one") = _sUserLogin
            oSPListItem("two") = _sUserGuid
            oSPListItem("three") = False
            oSPListItem("four") = DateTime.Now

            oSPListItem.Update()
        End Using
    End Sub

【问题讨论】:

  • 不工作怎么办?您是否仍然收到 Access Denied 或其他错误? (例如,如果您正在发布数据,SharePoint 将引发另一个异常,并且还有许多其他情况)。能否贴出调用RunWithElevatedPrivileges的代码,打开站点并添加项目?

标签: .net sharepoint forms-authentication wss-3.0


【解决方案1】:

当使用RunWithElevatedPrivileges 时,您不应该使用SPContext.Current - 它仍然具有旧权限。您应该重新打开您的 SPWeb 以授予它正确的权限。在您的链接代码上,这是由以下行完成的:

using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))

来源:
RunWithElevatedPrivileges, watch out for the site context
Adding Items to a SharePoint List - 来自我的博客,可能对您的下一个问题有所帮助。

另一个注意事项:你不应该写Using oSite As SPWeb = SPContext.Current.WebSPContext 对象不应由您处置 - 它们在不同组件之间共享,因此可能会导致其他异常。
这是一个常见的错误 - 我认为 API 可以做得更好。

【讨论】:

【解决方案2】:

线

oUserAccStatusList.Items.Add()

看起来有点不对劲。引用 SPList 后,您将创建一个新的 listItem,就像在以下代码中一样,通过在 listItem 上调用 Items.Add,设置您的属性,然后调用 Update 方法。

【讨论】:

    猜你喜欢
    • 2019-10-05
    • 2018-04-03
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 2016-03-24
    • 2013-09-08
    相关资源
    最近更新 更多