Hamaz.S
Hamaz.S

Reputation: 25

Access is denied Exception when updating List items using SPUserToken

I try to do some actions on Sharepoint 2010 list using an unlowed user( having only read right) and i got Access denied exception. Is it possible to do it whithout changing the user rights. please find below my code:

public void StartWorkFlow(int itemID, int wfTemplateID, String entityName, String userShortname)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            SPUser spUser;
            using (site = new SPSite(siteUrl))
            {
                using (web = site.RootWeb)
                {
                    spUser = web.EnsureUser(userShortname);
                }
            }
            using (site = new SPSite(siteUrl, spUser.UserToken))
            {
                using (web = site.AllWebs["WPrs"])
                {
                    list = web.Lists["Workflow Template"];
                    SPListItem item = list.Items.GetItemById(wfTemplateID);
                    SPList processList = web.Lists["List Process " + item["Template_x0020_Name"]];
                    SPListItem newInstance = processList.Items.Add();
                    newInstance["Template_x0020_Name"] = item["Template_x0020_Name"];
                    newInstance["Template_x0020_Description"] = item["Template_x0020_Description"];
                    newInstance["Date_x0020_De_x0020_Publication"] = item["Date_x0020_De_x0020_Publication"];
                    newInstance["Item_x0020_Id"] = itemID;
                    newInstance["Item_x0020_Extradata"] = entityName.ToLower() + "||Parameter||" + "";
                    newInstance["CTName"] = Utility.GetInstance().GetSuperItemNameBySubItemName(entityName);
                    web.AllowUnsafeUpdates = true;
                    newInstance.Update();
                    web.AllowUnsafeUpdates = false;
                }
            }
        });

    }

Thank you in advance.

Upvotes: 0

Views: 1119

Answers (2)

Marek Kembrowski
Marek Kembrowski

Reputation: 1262

If the only requirement is to set up the Created By/Modified By field - you should be able to overwrite default value by using SystemUpdate instead of Update. But still you need to run this RunWithElevatedPrivileges mode instead of UserToken

Although I'm not sure what will happen if have any other dependent actions after this update (event receivers or workflows) i'm not sure what context it will use (i.e. if SharePoint internally will use current SPWeb.CurrentUser object.

Upvotes: 0

Aarti
Aarti

Reputation: 765

To be able to use site.AllWebs the user must have Full Control to the site. Instead of using AllWebs try to get the sub sites using the following code -

SPWebCollection subwebs = CurrentSite.OpenWeb().GetSubwebsForCurrentUser();

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getsubwebsforcurrentuser%28v=office.12%29.aspx

http://social.msdn.microsoft.com/Forums/sharepoint/en-US/e7ff284f-fe4b-42fb-8b21-a475e331a18c/access-denied-for-user-with-contribute-rights?forum=sharepointdevelopmentlegacy

Also you would not be able to update a listitem with read only rights.

Upvotes: 2

Related Questions