0

I am updating the SharePoint library item using custom code through elevate privilege, I am extracting current user from 'SPContext.Current.Web.CurrentUser' and passing it to 'Modified By' and 'Editor' field. I have setup daily alert on document library where I am getting Modified By as 'System Account' instead of current user but in library view it is showing current user name. Please help me in getting current user name as ‘Modified By’ in alert emails also, thanks in advance.

  • 1
    Can you please share the code which you are using to update the modified by? – users1100 Nov 21 '16 at 13:14
1

I presume you are doing this:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPWeb web = site.OpenWeb())
    {
        string user = web.CurrentUser.Name;
    } 
});

When you do that, the current user is System Account. But if you do this should work:

SPUserToken token = Web.AllUsers[LoginID].UserToken;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(Web.Site.ID, token))
    {
        using (SPWeb w = site.RootWeb)
        {
            string user = web.CurrentUser.Name;
        }
    }
});

Let me know if this work :)

  • I am using below code to get the current user: SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(ConfigValue(SITEURL))) { using (SPWeb web = site.RootWeb) { currentUser = web.AllUsers[SPContext.Current.Web.CurrentUser.LoginName]; } } } – Sheo Nov 21 '16 at 13:25
  • That is my first example, when you elevate privilages you code will run as the higher account and that is System Account, try the second script of my answer, get the current user token and elevate. – Marco Nov 21 '16 at 13:36
  • I am getting current username as logged in user. I am able to see that while debugging. The issue is that it shows "system account" in mails coming via SharePoint alert( in footer template "last modified" by column ). Also it is coming correct in SharePoint library view(as logged in user). – Sheo Nov 21 '16 at 13:41
  • That's odd when you elevate privilages with out token you should get the system account not the logged user. I will check what you say. – Marco Nov 21 '16 at 14:10
  • This the code which we have used: SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(ConfigValue(SITEURL))) { using (SPWeb web = site.RootWeb) loggedUser = web.AllUsers[SPContext.Current.Web.CurrentUser.LoginName]; }}} – Sheo Nov 22 '16 at 4:03
0

you can use the method SystemUpdate() instead of Update()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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