1
votes

I'm using forms authentication to log in into windows sharepoint servevices 3.0 service. I need to elevate during anonymous access, rights to add record to sharepoint portal list.

I found clue in msdn: http://msdn.microsoft.com/en-us/library/bb466220%28classic%29.aspx

But it doesn't work for me. :( It's still calling for user login and password.

Can anybody help me please?

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
2
Doesn't work how? Are you still getting Access Denied, or some other error? (if you're POSTing data, SharePoint will throw another exception, for example, and there are many other cases). Can you post the code that calls RunWithElevatedPrivileges, opens the site and adds the item?Kobi

2 Answers

2
votes

When using RunWithElevatedPrivileges you shouldn't use SPContext.Current - it still has the old permissions. You should reopen your SPWeb to give it the right permissions. On your linked code this is done by the lines:

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

Source:
RunWithElevatedPrivileges, watch out for the site context
Adding Items to a SharePoint List - from my blog, might help with your next problem.

Another note: you should not be writing Using oSite As SPWeb = SPContext.Current.Web. SPContext objects should not be disposed by you - they are shared between different components, so it may lead to other exceptions.
This is a common mistake - it could have been done better by the API in my opinion.

0
votes

The line

oUserAccStatusList.Items.Add()

Looks a little off. Once you have a reference to the SPList you create a new listItem like you have in the following code, by calling the Items.Add on the listItem, set your properties and then call the Update method.