How To Get SharePoint Managed Metadata In A Data Set Using PowerShell Script

Welcome to an article on how to get SharePoint Managed Data in a Data set using a PowerShell script. In our projects we sometimes need to fetch the data from SharePoint Managed Data and use it in our projects.

We will see a very simple executable script which will allow you to connect to the database very quickly and efficiently and store it in a data set so that you don’t have to connect the database every time to get the data as that will make the script really slow if you have thousands of bits of data.

Let’s see how can we do it.

Steps

  1. Open Windows PowerShell Modules as an Administrator.



  2. Paste the following code as .ps1 file and execute it.
    1. #Add the PowerShell snap in code  
    2. Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null  
    3. #Provide a site collection to load the metadata properties from the SharePoint Central Admin where you metadata Database is connected  
    4. $siteCollectionUrl = "http://devlink /"  
    5. $site =new-object Microsoft.SharePoint.SPSite($siteCollectionUrl)  
    6. #Get the Taxanomy  
    7. $session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)  
    8. #Get the Termstore  
    9. $termStore = $session.TermStores[0]  
    10. #Provide the term store group you want to load and get connected  
    11. $group = $termStore.Groups["Test"]  
    12. #Provide the termset you want to load  
    13. $termSet = $group.TermSets["Test1"]  
    14. #Get all the desired terms.  
    15. $terms = $termSet.GetAllTerms()  
    16. Write-Host "SharePoint Database Connected"  
    17. #Load the SharePoint Metadata in a Dataset  
    18. #Create a table  
    19. $tabName = “SampleTable”  
    20. $table = New-Object system.Data.DataTable “$tabName”  
    21. $col1 = New-Object system.Data.DataColumn Test1,([string])  
    22. $col2 = New-Object system.Data.DataColumn Test2,([string])  
    23. #Load the columns  
    24. $table.columns.add($col1)  
    25. $table.columns.add($col2)  
    26. foreach ($term in $terms)  
    27. {  
    28.    $lblid = $term.Labels[1].Value;  
    29.    $termname = $term.Name  
    30.    $row = $table.NewRow()  
    31.    $row.Test1row = $lblid  
    32.    $row. Test2row = $termname  
    33.    $table.Rows.Add($row)  
    34. }  
    35. #You will get all the content in the table 

Pre-requisites

  • Site Collection URL
  • Name of the Term Store Group
  • Name of the Term set
  • Column Names

The above parameters are required from your end while connecting to the SharePoint database. Once you get the correct parameters and execute the script , you will get a message “SharePoint Database Connected”.

This data set will help you to have the completed Metadata database on your table and you can read the data through the table thereby saving your instance and time duration while every time reading the SharePoint web services. Here in this article we saw how to get SharePoint Metadata content on a data set using PowerShell Script.