2

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

2
  • Sounds like something is wrong with the code, can you post a snippet of it for us to look at? May 9, 2013 at 16:57
  • This code looks fine. Is there code that executes prior to this that may be failing?
    – Dave Wise
    May 9, 2013 at 17:40

2 Answers 2

3

You can create a custom permission level that allows adding but not editing.

See this MSDN article about how to do that. You will want to copy the contribute permission but remove the edit permissions.

2
  • 1
    Simple, effective and no managed code is the way to go here. May 9, 2013 at 18:06
  • In addition to my edit above, this will work as well so i marked it as the answer. Thanks!
    – heilch
    May 9, 2013 at 18:12
0

Please make sure that you are calling the RunWithElevatedPrivileges() outside the point where you are getting an SPSite reference otherwise it has no effect.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(siteUrl))
    {
    // do your stuff in here
    }
});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.