Second Life of a Hungarian SharePoint Geek

July 15, 2013

Approving all pending documents (and folders) of a specified library using PowerShell

Filed under: PowerShell, SP 2010 — Tags: , — Peter Holpar @ 21:56

Last week I wrote already about querying documents based on their moderation status. This time my goal was to approve all pending documents in a specified document library. I found a similar PowerShell script, but it does not handle approval of folders, so I created my own version displayed below:

$site = Get-SPSite("http://yourserver.com")
$web = $site.OpenWeb("/yourweb")

function approveItems($list) 
{
  Write-Host Processing $list
  $query = New-Object Microsoft.SharePoint.SPQuery
  $query.Query = "<Where><Eq><FieldRef Name=’_ModerationStatus’ /><Value Type=’ModStat’>2</Value></Eq></Where>"
  $query.ViewAttributes = "Scope = ‘RecursiveAll’"
  $items = $list.GetItems($query)

  $items | % {
    Write-Host Approving:  $_["Name"]
    $_["_ModerationStatus"] = 0
    $_.Update()
  }

  Write-Host —————————
}

approveItems $web.Lists["YourList"]

As you can see, the key is the usage of RecursiveAll scope in ViewAttributes that include recursively both folders and documents. If you would like to approve all documents on your site, you need something like this:

$web.Lists | % { approveItems $_ }

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: