I have a web part that, after insert an item in a list where the current user has the permission, need to insert an item in a list where the current user hasn't access. How can I do that?
1 Answer
You can use the RunWithElevatedPrivileges method to elevate your code and allow the user permission to the list.
SPUser currentUser = SPContext.Current.Web.CurrentUser;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// code to run here
item["Editor"] = currentUser;
item["Author"] = currentUser;
item.Update();
});
HTH
-
I read that is not a good way to accomplish this issue and I after the update I haven't the item in the list– GaetanuOct 16, 2012 at 13:56
-
I'm curious, where did you read this is not a good method to use? Run a debugger and ensure the code is firing. It should work without issue. On your new items, if you want them tagged from the current user, you need to declare a user object outside of the method, then set it to the Author and Editor fields. Oct 16, 2012 at 14:16
-
-
1Thanks for sharing that, I haven't come across that before. Asking my colleagues at the table, they also lean towards using SPSecurity.RunwithElevatedPrivilege. I'm going to do some more research to see what the real differences are. Oct 16, 2012 at 18:34