views:

449

answers:

4

Hi,

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
A: 

What you linked to should do the trick. What kind of error message are you getting?

Chris Stewart
There is no error message, wss3.0 is asking me for login and password.
truthseeker
@Chris: Please make this a comment rather than posting it as an "answer." Thanks.
Jonathan Sampson
+2  A: 

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.

Kobi
The answer is on your blog here: http://kobikobi.wordpress.com/2009/05/04/adding-items-to-a-sharepoint-list/The key for the last error was setting up flag: AllowUnsafeUpdatesThanks!
truthseeker
A: 

I modified my code to:

Dim oSharedConfig As SharedConfig = SharedConfig.Instance
        Dim sListName As String = oSharedConfig.oWebPartsOpt.UserOpt.AccountVerificationList.Name

        Dim oSPSite As SPSite = SPContext.Current.Site
        Dim oSPWeb = SPContext.Current.Web
        Using oElevatedSPSite As SPSite = New SPSite(oSPSite.ID)
            Using oElevatedSPWeb As SPWeb = oElevatedSPSite.OpenWeb(oSPWeb.ID)
                Dim oUserAccStatusList As SPList = oElevatedSPWeb.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 Using

and right now I'm getting error like this:

Server Error in '/' Application.

Proces sprawdzania poprawności zabezpieczeń tej strony jest nieprawidłowy. Kliknij przycisk Wstecz w przeglądarce, odśwież stronę i ponów operację. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: Proces sprawdzania poprawności zabezpieczeń tej strony jest nieprawidłowy. Kliknij przycisk Wstecz w przeglądarce, odśwież stronę i ponów operację.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[COMException (0x8102006d): Proces sprawdzania poprawności zabezpieczeń tej strony jest nieprawidłowy. Kliknij przycisk Wstecz w przeglądarce, odśwież stronę i ponów operację.] Microsoft.SharePoint.Library.SPRequestInternalClass.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bMigration, Boolean bPublish) +0 Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bMigration, Boolean bPublish) +203

[SPException: Proces sprawdzania poprawności zabezpieczeń tej strony jest nieprawidłowy. Kliknij przycisk Wstecz w przeglądarce, odśwież stronę i ponów operację.] Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bMigration, Boolean bPublish) +251 Microsoft.SharePoint.SPListItem.AddOrUpdateItem(Boolean bAdd, Boolean bSystem, Boolean bPreserveItemVersion, Boolean bNoVersion, Boolean bMigration, Boolean bPublish, Boolean bCheckOut, Boolean bCheckin, Guid newGuidOnAdd, Int32& ulID, Object& objAttachmentNames, Object& objAttachmentContents, Boolean suppressAfterEvents) +1026 Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents) +182 Microsoft.SharePoint.SPListItem.Update() +94 OurHealthSharedLibrary.OurHealth.User.AddUserAccountDataToSPList() +774 Microsoft.SharePoint.SPSecurity.CodeToRunElevatedWrapper(Object state) +70 Microsoft.SharePoint.<>c_DisplayClass4.b_2() +389 Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) +152 Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) +344 Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) +86 OurHealthSharedLibrary.OurHealth.User.AddUserAccountData() +67 ActivateAccountWebPart.OurHealth.ActivateAccount.RegisterUserAccount(String sUserLogin) +75 CreateAccountWebPart.OurHealth.CreateAccountWebPart.SubmitButtonClick(Object sender, EventArgs e) +1780 CustomControls.OurHealth.Register._cSubmitButton_Click(Object sender, EventArgs e) +103 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


Version Information: Microsoft .NET Framework Version:2.0.50727.4200; ASP.NET Version:2.0.50727.4016

truthseeker
A: 

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.

John Ptacek