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
- Open Windows PowerShell Modules as an Administrator.
- Paste the following code as .ps1 file and execute it.
- #Add the PowerShell snap in code
- Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
- #Provide a site collection to load the metadata properties from the SharePoint Central Admin where you metadata Database is connected
- $siteCollectionUrl = "http://devlink /"
- $site =new-object Microsoft.SharePoint.SPSite($siteCollectionUrl)
- #Get the Taxanomy
- $session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
- #Get the Termstore
- $termStore = $session.TermStores[0]
- #Provide the term store group you want to load and get connected
- $group = $termStore.Groups["Test"]
- #Provide the termset you want to load
- $termSet = $group.TermSets["Test1"]
- #Get all the desired terms.
- $terms = $termSet.GetAllTerms()
- Write-Host "SharePoint Database Connected"
- #Load the SharePoint Metadata in a Dataset
- #Create a table
- $tabName = “SampleTable”
- $table = New-Object system.Data.DataTable “$tabName”
- $col1 = New-Object system.Data.DataColumn Test1,([string])
- $col2 = New-Object system.Data.DataColumn Test2,([string])
- #Load the columns
- $table.columns.add($col1)
- $table.columns.add($col2)
- foreach ($term in $terms)
- {
- $lblid = $term.Labels[1].Value;
- $termname = $term.Name
- $row = $table.NewRow()
- $row.Test1row = $lblid
- $row. Test2row = $termname
- $table.Rows.Add($row)
- }
- #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.