Udostępnij za pośrednictwem


Updating the “Created By” and “Modified By” Columns in SharePoint lists using POWERSHELL

Thinks

Ever had the good old System Account show up as your author or editor, oops I mean “Created By” and “Modified By” column in SharePoint lists? Come on, you know that you have. OK, if you have and wanted to modify these, it can be done through the good old SharePoint Object Model. Sowmyan has a post on doing this in C# from a quick little app and I wanted to take the time to play with my new favorite tool (PowerShell!)

Sample Code for your PowerShell file – hint user PowerGUI Script editor for good old code completion goodness (www.PowerGui.org). This is sample code that you should add the appropriate dispose and error checking etc to. The # is the PowerShell line comment code for those learning PowerShell.

The fun thing is that this could probably be done as a ONE LINE code thing as well.

 

# Get the SharePoint Assembly
# You will need to run this on the SharePoint Server

[Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

# The site my list was in was at https://dev
# create a new object called $SPSite

$SPSite = New-Object Microsoft.SharePoint.SPSite("https://dev/")

# Make sure you have the last “/” in the url on the site
# allow $SPWeb to be the OpenWeb from the site

$SPWeb = $SPSite.OpenWeb()

# Get the list ModifyCreatedBy

$SPList = $SPWeb.Lists["ModifyCreatedBy"]

# Get the Collection of the List Items

$SPListItemCollection = $SPList.Items

# iterate the Collection

foreach ($ListItem in $SPListItemCollection)
    {

# The user in this case was Test R. Test and had a user id of 8 from the SPWeb users
#

        $SPFieldUserValue = New-Object Microsoft.SharePoint.SPFieldUserValue($SPWeb,8, "#Test R. Test")

        $ListItem["Author"] = $SPFieldUserValue                   

# Note: Editor will be the account that you are running the Powershell under unless you update # Editor as well

        $ListItem.Update()
    }

    $SPWeb.Update()

# Still TODO Disposes, Error Checking, Change to take Parameters, etc

 

  • GREAT! Thank you Sowmyan for sharing your post with the EcoSystem and providing me the motivation to do this in PowerShell
  • NOT SO GREAT! Unrelated, but my VPC had a disk error, argh.
  • GREAT! Was very straight forward in PowerShell!

Comments

  • Anonymous
    May 27, 2008
    Entwicklung What you need to know about AllowUnsafeUpdates (Part 1) What you need to know about AllowUnsafeUpdates

  • Anonymous
    June 06, 2008
    Direkter Download: SPPD-099-2008-06-06 Aktuell SharePointCommunityCamp September 2008 (15.9./16.9.2008)

  • Anonymous
    June 06, 2008
    Direkter Download: SPPD-099-2008-06-06 Aktuell SharePointCommunityCamp September 2008 (15.9./16.9.2008

  • Anonymous
    June 25, 2008
    Direkter Download: SPPD-100-2008-06-25 Behind the Scene - QIK Video Aktuell SharePointCommunityCamp September

  • Anonymous
    June 25, 2008
    Direkter Download: SPPD-100-2008-06-25 Behind the Scene - QIK Video Aktuell SharePointCommunityCamp September

  • Anonymous
    June 25, 2008
    Direkter Download: SPPD-100-2008-06-25 Behind the Scene - QIK Video Aktuell SharePointCommunityCamp September

  • Anonymous
    October 24, 2008
    Thank you very much, now I can get my self-updating inventory list done

  • Anonymous
    August 12, 2010
    You can also edit the created by value using a custom form and a Sharepoint:FormField pointed to Author. Only people with full control of the list will be able to edit though, for some reason.

  • Anonymous
    June 18, 2012
    Note that this can be done on document libraries too as long as you have rights. This powershell code assumes you have a valid list item in $itm, and a SPFieldUserValue in $oUser. Note you have to change the editor as well as author to get it to take- $itm["Author"].tostring() #show current author $itm["Author"] = $oUser #list item author $itm.Properties["vti_author"] = $oUser.User.LoginName #file properties author $itm["Editor"] = $oUser #this needs to be changed as well to update the item. $itm.UpdateOverwriteVersion() $itm["Author"].tostring() #print the changed author

  • Anonymous
    June 07, 2013
    Can I do this in Workflow (sharepoint 2007) ?