0

I need to update the CreatedBy field of a number of list items based on the criteria of Title and another user defined field. If I could run some powershell similar to the following it would be great.

Update List
Set CreatedBy = 'domain\new_username'
where title = 'xxx'
and userField = 'yyy'
and createdBy = 'domain\old_username'
2

Refer to this post:

Cannot update Created By (Author) field through powershell

Basically you need to get a reference to the item, then do something like this:

$SPListItem["Author"] = $userString;
$SPListItem.UpdateOverwriteVersion();
1

Below mentioned code will help you to update Author based on some conditions.

// 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)
    {
if($ListItem["title"] -eq "xxx" -And $ListItem["userField"] -eq "yyy" -And $ListItem["createdBy"] -eq "domain\old_username")
{
// 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.UpdateOverwriteVersion()
}
    }

    $SPWeb.Update()

// Still TODO Disposes, Error Checking, Change to take Parameters, etc
1

Ok, using @Mohamed and @Hardik's answer.. I scripted the following which allowed me to update the "Created By"

    Add-PSSnapin Microsoft.SharePoint.Powershell
    $SPWeb = Get-SPWeb "http://server/site"

    $userName = "i:0#.w|domain\username";
    $user = Get-SPUser -web $SPWeb -Identity $userName;
    $userString = "{0};#{1}" -f $user.ID, $user.UserLogin.Tostring()

    $SPList = $SPWeb.GetList("http://server/site/Lists/ListName");
    $SPListItemCollection = $SPList.Items;
    foreach ($ListItem in $SPListItemCollection)
    {
        if ($ListItem["Title"] -eq "xxx" -AND $ListItem["OtherField"] -eq "yyy" -AND $ListItem["Created By"].Contains("PartialName"))
        {
            $ListItem["Created By"] = New-Object Microsoft.SharePoint.SPFieldUserValue($SPWeb, $userString);        
            $ListItem.UpdateOverwriteVersion();
        }
}
  • BTW, this is the same solution which i provided in my answer. :) – Hardik May 19 '16 at 10:24
  • @hardik, it is not since you are using SSO objects, and the code above relies in great portion on PowerShell cmdlets – Robert Lindgren May 19 '16 at 10:30
  • It's the same as in the code I referred to you in the other post ;) – Mohamed Derhalli May 19 '16 at 15:47

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.