I am trying to programmatically add a list item to a SharePoint list. The code works fine when executed by a user with contribute permissions, but does not work when a user has view only. Funny thing is, i can debug through the same code with both users and it does not seem to fail on the user with read only.
I only want users to be able to add items to the list (which has a workflow to approve before they are visible) so giving the users contribute permissions will not work.
I tried running with elevated privileges but that did not work.
Is there a permissions setting to only allow add item ot list, or is there a way to programmatically bypass this issue?
This is the code i am working on right now:
var siteId = SPContext.Current.Site.ID;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteId))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
// Fetch the List
SPList list = web.Lists["List Name"];
//Add a new item in the List
SPListItem itemToAdd = list.Items.Add();
itemToAdd["Title"] = titlename;
itemToAdd.Update();
web.AllowUnsafeUpdates = false;
}
}
});
Solved, run with elevated privileges grants you system account access, but you still cannot add an item with it. The full answer can be found on the second reply here:
https://stackoverflow.com/questions/5164626/impersonate-as-different-user-inside-the-webpart-code