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 http://dev
# create a new object called $SPSite

$SPSite = New-Object Microsoft.SharePoint.SPSite("http://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

 

Links

Clinks

  • 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!