Try {spsecurity. runwithelevatedprivileges (delegate () // use this method to simulate the Administrator account to run this event handler {spweb web = spcontext. current. web; // at this time, the user is SharePoint \ system // response. write ("User:" + web. currentuser); web. allowunsafeupdates = true; // get the token spuser user = web of the user to be simulated. ensureuser ("veekee \ user1"); spusertoken token = user. usertoken; // use the User Token to construct a new site object spsite sitewithuser = new spsite (web. URL, token); spweb webwidthuser = sitewithuser. rootweb; // at this time, the user is veekee \ user1 // response. write ("User:" + webwidthuser. currentuser); webwidthuser. allowunsafeupdates = true; // use veekee \ user1 to add record splist list = webwidthuser. lists ["test1"]; splistitem item = List. additem (); item ["title"] = "11"; item. update (); webwidthuser. dispose (); sitewithuser. dispose (); web. dispose () ;});} catch (exception ex) {Throw ex ;}
Related information
Code used to simulate a specified user identity in SharePoint: String siteurl = "http: // ecpe-Dev: 82/sites/SJS "; // obtain the token of a user as an administrator. // spsecurity should be used. runwithelevatedprivilege to improve permissions. spsite site = new spsite (siteurl); // the user is SharePoint \ system response. write (site. rootweb. currentuser); // get the token spuser = site of the user to be simulated. rootweb. siteusers ["saicmotorlab \ user1"]; // use the User Token to construct a new site object spsite sitewithuser = new spsite (siteurl, user. use Rtoken); spweb webwithuser = sitewithuser. rootweb; // at this time, the user is saicmotorlab \ user1 response. write (webwithuser. currentuser); splist listwithuser = webwithuser. lists ["simelist"]; // The code above other code can be used when you need to use specified user permissions, such as writing a web service, return the listitem to be viewed based on the input user name.
How SharePoint simulates users