none
Using Powershell script how to get all the pages(Pages Library) based on a condition if custom property matches the given value? RRS feed

  • Question

  • Using Power shell script(Share point 2013) how to get all the pages(Pages Library) based on a condition if custom property matches the given value?

    I have a managed metadata column in the pages library properties.

    I have to get all the pages if the custom property "Category" equal to "Business".I have to read category of the each page in pages library and update it to the "Business Update".

    I changed this value in the manages metadata.But its not reflected in the pages.I need to update this through out the site collection wherever the pages library having property Category equal to Business to Business Update.

    Please provide the power shell code.

    Friday, June 7, 2019 3:42 AM

All replies

  • site = get-SPSite http://sp2013vm:5555/
    $web = $site.RootWeb
    $pweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
    
    $pPages = $pWeb.GetPublishingPages();
    
    $pLayouts ="HomePage.aspx" #$pweb.GetAvailablePageLayouts()
    
    foreach ($pPage in $pPages)
    {
        if ($pPage.Layout.Name -eq $pLayouts)
        {
            write-host $pPage.Title
        }
    } 

    you can try the changing the property to category from layout and execute the script.


    Please remember to click Mark as Answer on the answer if it helps you

    Friday, June 7, 2019 10:31 PM
  • Hi, 

    Sample script for your reference.

    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
        Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    }
    
    $siteURL="http://sp"
    $site = Get-SPSite $siteURL
    $web = $site.RootWeb
    $splList = $web.Lists.TryGetList("Pages")
    $spqQuery = New-Object Microsoft.SharePoint.SPQuery 
                $spqQuery.Query =  
                    " <Where>
    			        <Contains>
    			        <FieldRef Name='Meta' />
    			        <Value Type='Text'>B</Value>
    			        </Contains>
    			     </Where>" 
    
    $splListItems = $splList.GetItems($spqQuery) 
    			
    			
    $session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
    $termStore = $session.TermStores["Managed Metadata Service"]
    $group = $termStore.Groups["MyGroup"]
    $termSet = $group.TermSets["MyTerms"]
    $terms = $termSet.GetTerms(100)
    $term = $terms | ?{$_.Name -eq "C"}
    
    Write-Host $splListItems.Count 
    
    foreach($item in $splListItems)
        {
           #Cast to SPListItem to avoid ambiguous overload error
            $spItem = [Microsoft.SharePoint.SPListItem]$item;
            
            #Get the taxonomy field for the list item
            $taxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spItem.Fields["Meta"]
            
            #Set the field's value using the term
            $taxField.SetFieldValue($spItem,$term)
            $spItem.Update()
        }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, June 10, 2019 3:30 AM
  • Hi Sudhir,

    There is a hidden list in every site collection "TaxonomyHiddenList" can be found at:

    http://<sitecollection>/Lists/TaxonomyHiddenList

    SharePoint uses this list to cache the term sets into site collection. And there is a hourly "Taxonomy Update Scheduler" timer job that updates the terms in this list from the Managed Metadata service. And all the columns of Managed metadata type refer this hidden list for displaying values. 

    So, first check if the values are updated in this list. If not then check if the timer job is running. Try to run the timer job and check the list again if changes are cached in this list. 

    If nothing works then run the below powershell script to force sync every term:

    $url = Read-Host "Enter Url of site collection" 
    
    $site=Get-SPSite $url
    [Microsoft.SharePoint.Taxonomy.TaxonomySession]::SyncHiddenList($site) 
    $site.dispose()


    Best Regards,
    Brij K
    http://bloggerbrij.blogspot.co.uk/

    Monday, June 10, 2019 12:57 PM
  • I got the issue while execute the below script.please help where i have to change.

    I have a term which is modified in managed metadata.SO using power shell i need to update through out the Pages library wherever this pages Property  Category equal to "Concept Review" to "Concept"

    code:

    Add-PSSnapin 'Microsoft.SharePoint.PowerShell'

    $spSite = Get-SPSite "https://aaa.com/sites/news"
     
    if($spSite -ne $null)
    {
       foreach($subWeb in $spSite.AllWebs)
       {
          if($subWeb -ne $null)
          {
          #if($subWeb -eq "https://aaa.com/sites/news/aa"){
            #Print each Subsite
            Write-Host -ForegroundColor Red "Subsite : "$subWeb.Title - $subWeb.Url 
            
            #Get the Term from Term store
            $TaxonomySession = Get-SPTaxonomySession -site $subWeb.Site
            $TermStore = $TaxonomySession.TermStores["ManagedMetadataServiceApplication"]
            $TermGroup = $TermStore.Groups["News"]
            $TermSet = $TermGroup.TermSets["Categories"]
            $Term = $Termset.Terms["Concept"]
            Write-Host "term set: " $Term.Name and $Term.Id
                           
            $spSourceList = $subweb.Lists["Pages"]
              $spqQuery = New-Object Microsoft.SharePoint.SPQuery 
                        $spqQuery.Query =  
                            " <Where>
                    <Contains>
                    <FieldRef Name='News_x0020_Category' />
                    <Value Type='Text'>Concept reviews</Value>
                    </Contains>
                 </Where>" 

            $pages = $spSourceList.GetItems($spqQuery)        
              Write-Host $pages.Count 

            foreach ($page in $pages){       
                if($page.Name -eq "news_from_Concept_reviews.aspx"){
                    Write-Host "Page Title: " $page.Name                  
                    #Write-Host -ForegroundColor Green "Page Topics: " $page["News_x0020_Category"]                
                                     
                    #Get the taxonomy field for the list item
                    $taxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$page.Fields["News_x0020_Category"]
                                    
                    #Set the field's value using the term
                    $taxField.SetFieldValue($page, $Term)
                    $spItem.Update() 
                }                                       
            }
            #}      
          }
          else
          {
             Echo $subWeb "does not exist"
          }
          $subWeb.Dispose()
       }
       $spSite.Dispose()
       
    }
    else
    {
       Echo $siteURL "does not exist, check the site collection url"
    }

    Issue details given below:

    You cannot call a method on a null-valued expression.
    At F:\PS\Update_Page Properties.ps1:45 char:17
    +                 $taxField.SetFieldValue($page, $Term)
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    Exception calling "Update" with "0" argument(s): "The file "https://news..com/sites/news/aaa/Pages/Inspirational_news_from_Concept_reviews.aspx" is checked out 
    for editing by i:0ǵ.t|/sss.aaa.com|s."
    At F:\PS\Update_Page Properties.ps1:46 char:17
    +                 $spItem.Update()
    +                 ~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : SPException

    Monday, June 10, 2019 1:26 PM
  • Hi, 

    The error should be get the taxField failed.

    $taxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$page.Fields["News_x0020_Category"]

    Try to install PowerGUI in your server so you could debug the script easily.


    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, June 11, 2019 3:10 AM
  • I have removed old term and added the new term.So both Guid are not equal.

    i was run the above script after the above step then i got this issue.

    SO first i have to clear the old value from metadata property and replaced with the new one.

    Both New and Old values gud are not equal.

    SO please help in this case.


    Tuesday, June 11, 2019 6:47 AM
  • Hi,

    Could you debug it so you could know detailed error?

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, June 13, 2019 3:18 AM