none
How to display user account for Modified By when the listitem is updated by timer job RRS feed

  • Question


  • Hi,

    I am having a custom Timer Job in SharePoint 2010, which updates the listitem properties.
    After timer job execution, Modified By and Created By fields of the listitem is displayed as "System Account".
    But I need user account to be displayed for Modified By and Created By fields when the listitem is updated by timer job.

    How to achieve this

    Thanks in advance,
    dhijit

    Thursday, August 8, 2013 11:35 AM

Answers

  • Hi Dhijit,

    There is a method SPListItem.SystemUpdate(), you can use it in the timer job. This method will update the item but will not change values of "Modified" or "Modified By" fields. Those values remain intact.

    I hope this is what you are looking for.

    But you have metioned "Created By" field also. So if you are creating a new item in the timer job, then you have to create the item by impersonating identity of the user (using SPUser.UserToken property) whose name should appear in these field.

    Reference for Impersonation: http://www.codeproject.com/Articles/433494/Impersonation-in-SharePoint-2010


    Pradip T. ------------- MCTS(SharePoint 2010/Web)|MCPD(Web Development) https://www.mcpvirtualbusinesscard.com/VBCServer/paddytakate/profile

    • Marked as answer by dhijit Monday, August 12, 2013 10:14 AM
    Friday, August 9, 2013 10:38 AM

All replies

  • Hello,

    Are you using "runwithelevatedprivileges" in your code? If yes then use "SPUserToken" class instead of runwith...

    http://anujpant.wordpress.com/2012/11/05/sharepoint-user-impersonation-with-spusertoken/

    Hope it could help


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, August 8, 2013 12:11 PM
    Moderator
  • Hello Hemendra,

    I am not using "runwithelevatedprivileges".  My timer job creates and updates an item in the list, hence the Created By and Modified By field is updated to System Account.

    Thanks,

    dhijit

    Friday, August 9, 2013 4:03 AM
  • @dhijit,

    Always timer jobs run under the account which runs the OWSTimer job. Which will be always Farm service account so your created by and modified by are showing system account.

    http://social.technet.microsoft.com/Forums/en-US/d6a9500d-09ee-4e03-981c-40ef4ece5218/timer-job-account


    My Blog- http://www.sharepoint-journey.com| Twitter
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful

    Friday, August 9, 2013 4:16 AM
  • Hi Dhijit,

    There is a method SPListItem.SystemUpdate(), you can use it in the timer job. This method will update the item but will not change values of "Modified" or "Modified By" fields. Those values remain intact.

    I hope this is what you are looking for.

    But you have metioned "Created By" field also. So if you are creating a new item in the timer job, then you have to create the item by impersonating identity of the user (using SPUser.UserToken property) whose name should appear in these field.

    Reference for Impersonation: http://www.codeproject.com/Articles/433494/Impersonation-in-SharePoint-2010


    Pradip T. ------------- MCTS(SharePoint 2010/Web)|MCPD(Web Development) https://www.mcpvirtualbusinesscard.com/VBCServer/paddytakate/profile

    • Marked as answer by dhijit Monday, August 12, 2013 10:14 AM
    Friday, August 9, 2013 10:38 AM
  • Hello Pradip,

    The user is not having access to the site. When I update the item using SPUser.UserToken it throws UnauthorizedException.

    Thanks,

    dhijit

    Friday, August 9, 2013 11:10 AM
  • Dhijit,

    #1, Without having access to the site, user will not be able to create the list item and hence cannot be shown in "Created By " field value.

    Try to use UserToken of the user who has Contiribute permission to the list.

    #2, And if you are not creating item and just updating the items, then use SPListItem.SystemUpdate method for exisiting items. It will preserve the old values of the columns.


    Pradip T. ------------- MCTS(SharePoint 2010/Web)|MCPD(Web Development) https://www.mcpvirtualbusinesscard.com/VBCServer/paddytakate/profile

    • Marked as answer by dhijit Monday, August 12, 2013 10:14 AM
    • Unmarked as answer by dhijit Monday, August 12, 2013 10:14 AM
    Friday, August 9, 2013 11:17 AM