Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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:

http://stackoverflow.com/questions/5164626/impersonate-as-different-user-inside-the-webpart-code

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

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
1  
Simple, effective and no managed code is the way to go here. –  PirateEric May 9 '13 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 '13 at 18:12

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
    }
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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