Updating a List's "Modified By/Created By" Properties
Here is a piece of code that can be used in Powershell to update the Modified By and Created By properities on a list. This is helpful if you move content using Sushi or other tool and they don't get updated. We tried to use Sushi to update this information, but found that you couldn't do it.
[Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
# Change the URL to your site
$SPSite = New-Object Microsoft.SharePoint.SPSite("http://source/presidentscorner/")
$SPWeb = $SPSite.OpenWeb()
$SPList = $SPWeb.Lists["Comments"]
$SPListItemCollection = $SPList.Items
foreach ($ListItem in $SPListItemCollection)
{
# Make sure you are updating the correct item in the list
if ($ListItem.ID -eq 4)
{
# Change the User ID and Name to who you want shown.
# If the user doesn't exist in the Web collection then added them as a visitor and get their ID. You can then delete them
# from the group and everything will still work
$SPFieldUserValue = New-Object Microsoft.SharePoint.SPFieldUserValue($SPWeb,131, "Shields, Lance")
$ListItem["Author"] = $SPFieldUserValue
$ListItem["Editor"] = $SPFieldUserValue
$ListItem.Update()
}
if ($ListItem.ID -eq 5)
{
$SPFieldUserValue = New-Object Microsoft.SharePoint.SPFieldUserValue($SPWeb,179, "Martin, Ryan")
$ListItem["Author"] = $SPFieldUserValue
$ListItem["Editor"] = $SPFieldUserValue
$ListItem.Update()
}
}
$SPWeb.Update()
[Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
# Change the URL to your site
$SPSite = New-Object Microsoft.SharePoint.SPSite("http://source/presidentscorner/")
$SPWeb = $SPSite.OpenWeb()
$SPList = $SPWeb.Lists["Comments"]
$SPListItemCollection = $SPList.Items
foreach ($ListItem in $SPListItemCollection)
{
# Make sure you are updating the correct item in the list
if ($ListItem.ID -eq 4)
{
# Change the User ID and Name to who you want shown.
# If the user doesn't exist in the Web collection then added them as a visitor and get their ID. You can then delete them
# from the group and everything will still work
$SPFieldUserValue = New-Object Microsoft.SharePoint.SPFieldUserValue($SPWeb,131, "Shields, Lance")
$ListItem["Author"] = $SPFieldUserValue
$ListItem["Editor"] = $SPFieldUserValue
$ListItem.Update()
}
if ($ListItem.ID -eq 5)
{
$SPFieldUserValue = New-Object Microsoft.SharePoint.SPFieldUserValue($SPWeb,179, "Martin, Ryan")
$ListItem["Author"] = $SPFieldUserValue
$ListItem["Editor"] = $SPFieldUserValue
$ListItem.Update()
}
}
$SPWeb.Update()
Comments
But the user id is not the same for each site.
So one can use the
SPUser user=web.Ensureuser(loginName or aliasName);
and use
spfielduservalue value=new spuserfieldvalue(wed,user.ID,user.LoginName);
Hope it Helps!!
Bharath