Posted by: msqr | November 1, 2010

Sharepoint tips – Update Created By/Modified by user with logged in user

Normally, we run code under elevated previleges that takes System User account in created by modified by. If we want to reflect Created By/Modifed by with logged in user, you have to specify UserToken while opening site.

SPUserToken loggedInUserToken = SPContext.Current.Web.CurrentUser.UserToken;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite objSite = new SPSite(SPContext.Current.Web.Url,loggedInUserToken))
{
using (SPWeb objWeb = objSite.OpenWeb())
{
objWeb.AllowUnsafeUpdates = true;
SPListItem lstItem= listItemCollection.Add();
lstItem[“Foo”] = “Bar”
lstItem.Update();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: