Eric Di Bari
Eric Di Bari

Reputation: 3867

SharePoint Web Part List Write Permission

I've created a quiz web part in SharePoint 2007, but am stuck on one permission. It needs to write the quiz score to a list, which it's throwing an error now when trying. I'm under the assumption that if the web part has the appropriate permission level, the user's permissions (the quiz taker) don't matter.

Is there a particular permission that should allow the webpart to write to a list? Specifically:

 SPListItem item = listItems.Add();

Upvotes: 3

Views: 551

Answers (1)

Kyle Trauberman
Kyle Trauberman

Reputation: 25694

The webpart can only write to a list if the current user has write (Contribute) access to the list. If you don't want the user to have access to the list, you can elevate permissions for the write to the list. This will cause the list item to be created by the System Account.

SPSecurity.RunWithElevatedPrivileges(WriteToList);

private void WriteToList()
{
    // create new SPSite and SPWeb objects.  This is important, if you 
    // don't then the write won't use the elevated privileges.
    using(SPSite site = new SPSite(SPContext.Current.Site.ID))
    {
        using(SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
        {
            // Code to Write to the list.
        }
    }
}

For more information see http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx

Upvotes: 2

Related Questions