We recently received a query for DocRead to see if it was possible to set the 'Required Audience' and 'Acknowledgement Days' to about 100 policies on mass (via a script). Hence, being one who is always up for a challenge, I loaded up PowerGUI and set to work!
Pre-Requisities
This script can easily be modified to set any column value, but, obviously my example was specifically to update 'Required Audience' and / or 'Recommended Audience' columns that DocRead relies upon to distribute the documents to correct groups. To follow my example, you will need to :
- Have enabled DocRead on the site containing the Document library.
- Attached DocRead to the Document Library.
- Populated the Document Library with a few important policies.
Instructions
- Copy the script below (into notepad) and save as 'DocRead-Set-DocProps.ps1' on one of your WFE's.
- Login to the WFE as a user who is an admin and who also has contribute permissions on the Document Library containing the policies.
- Load a SharePoint PowerShell session.
- Within the PowerShell session, 'CD' into the directory where you saved the script. (e.g. CD c:\Powershell).
- Now copy this line, changing the values in orange to match your url, document library name, comment, and group names.
-
.\DocRead-Set-DocProps.ps1 -prmUrl "http://localhost" -prmFolder "Documents" -prmCommentField "DocRead Comment" -prmComment "Your comment" -prmRequiredGroupName "DocRead test group" -prmRecommendedGroupName "Approvers" –prmRequiredAckDays 4 -prmRecommendedAckDays 5 -prmOverrideCheckOut 0
- Run it!
- Once it has run, the script will checkout each document, amend the audience and comment columns, check in and optionally approve and publish the document.
- For DocRead users, the next step is to process site readerships via the DocRead menu.
The PowerShell Script
param(
[string]$prmUrl,
[string]$prmFolder,
[string]$prmCommentField,
[string]$prmComment,
[string]$prmRequiredGroupName,
[string]$prmRecommendedGroupName,
[int]$prmRequiredAckDays,
[int]$prmRecommendedAckDays,
[boolean]$prmOverrideCheckOut
)
# Load up SP required snap in.
if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
$web = Get-SPWeb $prmUrl
# Get the folder
$folder = $web.GetFolder($prmFolder);
# Only do this if the folder is in the web.
if ($folder.Exists) {
# Loop through files and check out if file is not already checked out
$folder.Files | ForEach-Object {
if ($_.CheckOutType -ne "None" -and $prmOverrideCheckOut) {
$_.CheckIn("Override Checkout")
}
# Check if file is not checked in if ($_.CheckOutType -eq "None")
{
Write-Host Checking out : $_.Name
$_.CheckOut()
# Set Comment field
if ($prmCommentField) {
$_.Item[$prmCommentField] = $prmComment
}
# Set Required Audience
if ($prmRequiredGroupName) {
if (!$prmRequiredAckDays) {
$reqAckDays = 7
} else {
$reqAckDays = $prmRequiredAckDays
}
$reqUserGroup = $web.Groups[$prmRequiredGroupName]
Write-Host Setting Required Audience : $prmRequiredGroupName
$_.Item["Required Audiences"] = ";#;;;;;;;;" +
$reqUserGroup.Name + ";#" +
$reqAckDays + ";#"
}
# Set Recommended Audience
if ($prmRecommendedGroupName) {
if (!$prmRecommendedAckDays) {
$recAckDays = 7
} else {
$recAckDays = $prmRecommendedAckDays
}
$recUserGroup = $web.Groups[$prmRecommendedGroupName]
Write-Host Setting Recommended Audience : $prmRecommendedGroupName
$_.Item["Recommended Audiences"] = ";#;;;;;;;;" +
$recUserGroup.Name + ";#" +
$recAckDays + ";#"
}
$_.Item.Update()
$_.CheckIn("Checked in.")
# Check for a publish
if (($_.Level -eq [Microsoft.SharePoint.SPFileLevel]::Draft)) {
Write-Host Publishing a major version
$_.Publish("Published.")
$_.Update();
}
# Check for an approve
if ($_.Item.ModerationInformation){
if ($_.Item.ModerationInformation.Status -eq
[Microsoft.SharePoint.SPModerationStatusType]::Pending) {
Write-Host Approving
$_.Approve("Approved")
$_.Update();
}
}
}
else
{
Write-Host $_.Name already checked out;
}
}
}
$web.Dispose()