none
Powershell script to change the a column value of all documents in a site.

    Question

  • Hi,

    I need a powershell script to change the value of a column (a site column which has been added to all document libraries) in all documents in a site,

    For example: 

    -column 1 is a site column added to all libraries

    • the value of column 1 of all documents under this site: http://intranet.doman/ex1 should be equal to V1
    • the value of column 1 of all documents under this site: http://intranet.doman/ex2 should be equal to V2

    So, if I can write a powershell script to change the value of all documents in a site, I can modify it for different site that I have and run it for each of them individually,

    Wednesday, August 27, 2014 6:42 AM

Answers

  • cls
    # Is dev version?
    $dev = $false

    # Configuration
    $termStore = "Managed Metadata Service"
    $group = "G1"
    $subjectMatterTermSetName = "Subject Matter"

    # Check if SharePoint Snapin is loaded
    if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
         Add-PSSnapin Microsoft.SharePoint.PowerShell
    }
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Taxonomy") | Out-Null

    function GetTermStore($site, $termStore, $group, $termSet) {    
        $session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
        $termStore = $session.TermStores[$termStore]
        $group = $termStore.Groups[$group]
        $termSet = $group.TermSets[$termSet]
        return $termSet
    }

    if($dev) {
        Write-Host "Running DEV version..."
        $webUrl = "http://Site1"   
        $libraryName = "L1"
        $subjectMatter = "C1"
    } else {
        $webUrl = Read-Host "Enter Site URL" 
        $libraryName = Read-Host "Enter Document Library name"
    $subjectMatter = Read-Host "Enter Subject Matter"
    }

    try {
        $web = Get-SPWeb $webUrl
        $site = $web.Site
        $library = $web.Lists[$libraryName]
        $items = $library.GetItems()
        $subjectMatterTermSet = GetTermStore $site $termStore $group $subjectMatterTermSetName
        $subjectMatterTerm = $subjectMatterTermSet.GetTerms($subjectMatter,$true) | select -First 1

        foreach($item in $items) {
            if([string]::IsNullOrEmpty($item["Subject Matter"])) {     
                #Write-Host "Filename: $filename / Keywords: $keywords / Subject Matter: $subjectMatter / Document Type: $documentType"        
                Write-Host "Updating $($item["ows_FileLeafRef"])..."           
                
                # Set Subject Matter column
                
                $subjectMatterField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$item.Fields["Subject Matter"]
                $subjectMatterField.SetFieldValue($item,$subjectMatterTerm)
                
                # Update Item
                $item.SystemUpdate()
            }
        }

    catch
    {
        $ErrorMessage = $_.Exception.Message
        Write-Host "Something went wrong. Error: $ErrorMessage" -ForegroundColor Red
    }
    • Marked as answer by babaii Friday, September 12, 2014 5:10 AM
    Friday, September 12, 2014 5:10 AM

All replies

  • Yes, that's a pretty simple task. Have you tried writing a script to do so?
    Wednesday, August 27, 2014 7:32 AM
  • Hi Alex,

    Thanks for your reply,

    I'm new in powershell scripting, not sure where to start for this task and I need to get it done quickly,

    Any help will be greatly appreciated,


    Thursday, August 28, 2014 2:23 AM
  • No time like the present to learn.

    This script will show you how to get all the columns for a list item: http://get-spscripts.com/2010/09/get-all-column-values-from-sharepoint.html

    This one shows you a simpler selection of ways to get items and update them:

    http://sharepointrelated.com/2012/05/03/addedit-list-items-using-powershell-in-sharepoint-2010/

    This shows you how to loop through all sites and all webs.

    http://www.ilovesharepoint.com/2011/07/perfect-loop-looping-through-all-webs.html

    You'd just need to put that all together. Give it a shot and see how it goes, post up your code and I or others will help correct it.

    Thursday, August 28, 2014 8:44 AM
  • cls
    # Is dev version?
    $dev = $false

    # Configuration
    $termStore = "Managed Metadata Service"
    $group = "G1"
    $subjectMatterTermSetName = "Subject Matter"

    # Check if SharePoint Snapin is loaded
    if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
         Add-PSSnapin Microsoft.SharePoint.PowerShell
    }
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Taxonomy") | Out-Null

    function GetTermStore($site, $termStore, $group, $termSet) {    
        $session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
        $termStore = $session.TermStores[$termStore]
        $group = $termStore.Groups[$group]
        $termSet = $group.TermSets[$termSet]
        return $termSet
    }

    if($dev) {
        Write-Host "Running DEV version..."
        $webUrl = "http://Site1"   
        $libraryName = "L1"
        $subjectMatter = "C1"
    } else {
        $webUrl = Read-Host "Enter Site URL" 
        $libraryName = Read-Host "Enter Document Library name"
    $subjectMatter = Read-Host "Enter Subject Matter"
    }

    try {
        $web = Get-SPWeb $webUrl
        $site = $web.Site
        $library = $web.Lists[$libraryName]
        $items = $library.GetItems()
        $subjectMatterTermSet = GetTermStore $site $termStore $group $subjectMatterTermSetName
        $subjectMatterTerm = $subjectMatterTermSet.GetTerms($subjectMatter,$true) | select -First 1

        foreach($item in $items) {
            if([string]::IsNullOrEmpty($item["Subject Matter"])) {     
                #Write-Host "Filename: $filename / Keywords: $keywords / Subject Matter: $subjectMatter / Document Type: $documentType"        
                Write-Host "Updating $($item["ows_FileLeafRef"])..."           
                
                # Set Subject Matter column
                
                $subjectMatterField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$item.Fields["Subject Matter"]
                $subjectMatterField.SetFieldValue($item,$subjectMatterTerm)
                
                # Update Item
                $item.SystemUpdate()
            }
        }

    catch
    {
        $ErrorMessage = $_.Exception.Message
        Write-Host "Something went wrong. Error: $ErrorMessage" -ForegroundColor Red
    }
    • Marked as answer by babaii Friday, September 12, 2014 5:10 AM
    Friday, September 12, 2014 5:10 AM