Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Created October 7, 2019 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joerodgers/32978257c5b2380db771e8e72aff019c to your computer and use it in GitHub Desktop.
Save joerodgers/32978257c5b2380db771e8e72aff019c to your computer and use it in GitHub Desktop.
Get/Set files with missing author and editor properties on files in document libraries
Add-PSSnapin Microsoft.SharePoint.PowerShell
function Set-EditorAuthorProperty
{
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true)][string]$FileUrl,
[Parameter(Mandatory=$true)][string]$DefaultAuthor,
[Parameter(Mandatory=$true)][string]$DefaultEditor
)
begin
{
$itemsNeedsUpdate = $false
# convert to claims format
if( -not [Microsoft.SharePoint.Administration.Claims.SPClaimProviderManager]::IsEncodedClaim( $DefaultAuthor ) )
{
$DefaultAuthor = (New-SPClaimsPrincipal -Identity $DefaultAuthor -IdentityType WindowsSamAccountName -Verbose:$false).ToEncodedString()
}
# convert to claims format
if( -not [Microsoft.SharePoint.Administration.Claims.SPClaimProviderManager]::IsEncodedClaim( $DefaultEditor ) )
{
$DefaultEditor = (New-SPClaimsPrincipal -Identity $DefaultEditor -IdentityType WindowsSamAccountName -Verbose:$false).ToEncodedString()
}
}
process
{
try
{
# get the site
$site = New-Object Microsoft.SharePoint.SPSite($FileUrl)
# get the web of the file
$web = $site.OpenWeb()
# get the file
$file = $web.GetFile($FileUrl)
# get the file item
$item = $file.Item
# add the default users to the site (no perms)
$defaultAuthorUser = $web.EnsureUser($DefaultAuthor)
$defaultEditorUser = $web.EnsureUser($DefaultEditor)
if( $true -or -not $item["Author"] )
{
if( $file.Author )
{
$item["Author"] = $file.Author # pull the author from the file
}
else
{
$item["Author"] = $defaultAuthorUser # default author value
}
Write-Verbose "Setting item author to: $defaultAuthorUser"
}
else
{
# seem to have to set both values at once for either to save
$authorField = New-Object Microsoft.SharePoint.SPFieldUserValue($web, $item["Author"].ToString())
$item["Author"] = $authorField.User
}
if( -not $item["Editor"] )
{
if( $file.ModifiedBy )
{
$item["Editor"] = $file.ModifiedBy # pull the editor from the file
}
else
{
$item["Editor"] = $defaultEditorUser # default editor value
}
}
else
{
# seem to have to set both values at once for either to save
$editorField = New-Object Microsoft.SharePoint.SPFieldUserValue($web, $item["Editor"].ToString())
$item["Editor"] = $editorField.User
}
$item.Update()
}
catch
{
Write-Error "Error processing file: $($FileUrl). Exception $($_)"
}
finally
{
if( $web )
{
$web.Dispose()
}
if( $site )
{
$site.Dispose()
}
}
}
end
{
}
}
function Get-FilesWithMissingEditorAndAuthor
{
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true)][string]$SiteUrl
)
begin
{
}
process
{
$site = Get-SPSite -Identity $SiteUrl
foreach( $web in $site.AllWebs )
{
foreach( $list in $web.GetListsOfType([Microsoft.SharePoint.SPBaseType]::DocumentLibrary ) )
{
if( -not $list.Hidden )
{
foreach( $item in $list.Items )
{
$authorField = $editorField = $null
if( $item["Author"] )
{
$authorField = New-Object Microsoft.SharePoint.SPFieldUserValue($web, $item["Author"].ToString())
}
if( $item["Editor"] )
{
$editorField = New-Object Microsoft.SharePoint.SPFieldUserValue($web, $item["Editor"].ToString())
}
if( -not $file.Author -or
-not $file.ModifiedBy -or
-not $authorField.User -or
-not $editorField.User )
{
[PSCustomObject] @{
Site = $site.Url
Web = $web.Url
List = $list.Title
FileAuthor = $item.File.Author.UserLogin
FileEditor = $item.File.ModifiedBy.UserLogin
ItemAuthor = $authorField.User.UserLogin
ItemEditor = $editorField.User.UserLogin
FileUrl = $site.MakeFullUrl($item.File.Url)
}
}
}
}
}
}
}
end
{
}
}
# site url
$siteUrl = "https://sharepoint.2016.contoso.com/sites/teamsite"
# new default users
$defaultAuthorLogin = "contoso\aaronp"
$defaultEditorLogin = "contoso\aaronp"
# query the site for missing author and editor values
$results = @(Get-FilesWithMissingEditorAndAuthor -SiteUrl $siteUrl)
$results | Export-Csv -Path "MissingAuthorsEditors.csv" -NoTypeInformation
# enumerate each of the results
foreach( $result in $results )
{
Write-Host "Updating Principals on: $($result.FileUrl)"
Set-EditorAuthorProperty -FileUrl $result.FileUrl -DefaultAuthor $defaultAuthorLogin -DefaultEditor $defaultEditorLogin -Verbose
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment