Search This Blog

Wednesday, 22 April 2015

Powershell - Create Terms from csv

# =========================== Central Admin Details =====================================
$centralAdminUrl= Read-Host "Enter Central Admin URL (Eg: http://<HostName>:8080 )"



$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
    Write-Host "Loading SharePoint Powershell Snapin" -ForegroundColor Green
    $snapin = Add-PSSnapin "Microsoft.Sharepoint.Powershell"
    Write-Host $snapin
}

$global:strTermFileName = Read-Host "Enter the location of Terms.csv"
$LogPath = Read-Host "Enter a location for log file  :"
$LogFileName = Read-Host "Enter name for log file  :"
$FilePath = $LogPath + "\" + $LogFileName

# =========================== Managed Metadata Service Details =====================================

$MMSApplicationName="Managed Metadata Service";
$MMSGroupName= "abc ";
$MMSTermSet="test1";
#===================================================================================================

function write-log([string]$label, [string]$logMsg)
{
    if($logFileCreated -eq $False)
    {
        write-host "Creating log file..."
        if((Test-Path -path $LogPath) -ne $True)
        {
            write-host "Provide proper values to LogPath folder" -ForegroundColor Red
        }
        else
        {
            Add-Content -Path $FilePath -Value $logHeader
            $script:logFileCreated  = $True
            write-host "Log file created..."
        }
    }
    else
    {
        [string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
        Add-Content -Path $FilePath -Value $info
    }
}


[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
                Write-Host "Term Creation Process Started...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Green
                write-log  "Term Creation Process Started...Time -" "$(get-date -format HH:mm:ss)"
                $StartTime=Get-Date
               
                Import-CSV -Path $global:strTermFileName   | ForEach-Object {
                                if($_.TERM -ne $null -and $_.TERM -ne "")
                                {
                                $global:libraryTerm=$_.TERM                              
                                Write-Host $global:libraryTerm
                                write-log $global:libraryTerm
                                #Connect to Central Admin
                                $taxonomySite = get-SPSite $centralAdminUrl

                                    #Connect to Term Store in the Managed Metadata Service Application
                                    $taxonomySession = Get-SPTaxonomySession -site $taxonomySite
                                    $termStore = $taxonomySession.TermStores[$MMSApplicationName]
                                    write-host "Connection made with term store -"$termStore.Name

                                    #Connect to the Group and Term Set
                                    $termStoreGroup = $termStore.Groups[$MMSGroupName]
                                    $termSet = $termStoreGroup.TermSets[$MMSTermSet]
                                    $termCreate = $termSet.CreateTerm($global:libraryTerm, 1033)
                                    $termCreate.SetDescription("Term Description", 1033)
                                    $termCreate.CreateLabel($global:libraryTerm +" Category", 1033, $false)
                                    #Update the Term Store
                                    $termStore.CommitAll()
                               
                                 }
                               
                       }
                         $StartTime=(Get-Date) - $StartTime
                write-host "Total execution time- " $StartTime
                write-log "Total execution time- " $StartTime
                Write-Host "Term Creation Process Completed...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Green
                write-log "Term Creation Process Completed...Time -" "$(get-date -format HH:mm:ss)"
                })


#Note:- im getting Terms from csv file

No comments:

Post a Comment