1

I am using this code for it but I am unable to get the hierarchy which I got from the file....

$taxonomySite = Get-SPSite http:url
$taxonomySession = Get-SPTaxonomySession -site $taxonomySite
$termStore = $taxonomySession.TermStores["Managed Metadata Service"]
write-host “Connection made with term store -”$termStore.Name
$termStoreGroup = $termStore.CreateGroup(“General Business Taxonomy”)
$termStoreGroup.Description = “General Business Terms” 
$termStoreGroup.AddGroupManager(“x\y”)
$termStoreGroup.AddContributor(“x\y”)

$termSet = $termStoreGroup.CreateTermSet("General Business Taxonomy")

$data = Import-Csv "C:\terms.csv"

foreach ($line in $data) 
{ 
    $TermName = $line."Level 1 Term"
    $Termvalue =$line."Level 2 Term"
    $Termvalue =$line."Level 3 Term"
    $Termvalue =$line."Level 4 Term"
    $Termvalue =$line."Level 5 Term"

    if ($termSet.Terms[$TermName]  -eq $Null)
    {
        if($TermName -ne "")
        {
            $term =  $termSet.CreateTerm($TermName, 1033)
            Write-Host -ForegroundColor Cyan "Added term $TermName"
            $termStore.CommitAll()
        }
     }

     if ($termSet.Terms[$TermName] -ne $Null)
     { 
         $mainterm = $termSet.Terms[$TermName]
         if($TermValue -ne "")
         {
             $subterm = $mainterm.CreateTerm($TermValue, 1033)
             Write-Host -ForegroundColor Cyan "Added sub term $TermValue"
             $termStore.CommitAll()
         }
     }
}

$termStore.CommitAll()
4
  • what is your question ? what did you tried ? what are your results ?
    – Steve B
    Commented Sep 22, 2011 at 5:34
  • Did you try importing this via SharePoint Central Administration to see if it works? Commented Sep 22, 2011 at 6:36
  • Hi Steve my question is how to import huge csv file using powershell?
    – vikky22
    Commented Sep 22, 2011 at 11:49
  • Chandran I can do that through out of box but I want it in this manner ....can you help me out..?
    – vikky22
    Commented Sep 22, 2011 at 11:50

4 Answers 4

2

Try using this code, it worked for me, this is for a 2 level, first i check if the term exist if it doesn't I create it, then check if the termvalue has a value if it does I also check if exists and create it

$termName = $line."Level 1 Term"
$termValue =$line."Level 2 Term"

            try {
                [string]$normalizedTermName = [Microsoft.SharePoint.Taxonomy.TermSet]::NormalizeName($termName.trim());                 
                $term = $termSet.Terms[$normalizedTermName];                    
                }
            catch { }    
            if ($term -eq $null) 
            {

                if (![string]::IsNullOrEmpty($termName.trim()))
                {
                    $t = $termSet.CreateTerm($termName.trim(), 1033);
                    $termSet.TermStore.CommitAll();
                    write-host -foregroundcolor Darkyellow "Term $termName has been created"            
                }
            }
            else
            {
                write-host "Term $termName already exists"
            }

            if($termSet.Terms[$termName.trim()] -ne $null)
            {
                [string]$normalizedTermName = [Microsoft.SharePoint.Taxonomy.TermSet]::NormalizeName($termName.trim());
                $mainTerm = $termSet.Terms[$normalizedTermName]

                if (![string]::IsNullOrEmpty($termValue.trim()))
                {   
                    [string]$normalizedTermName = [Microsoft.SharePoint.Taxonomy.TermSet]::NormalizeName($termValue.trim());
                    $subTerm = $mainTerm.Terms[$normalizedTermName]
                    if($subTerm -eq $null)
                    {
                        $subTerm = $mainTerm.CreateTerm($termValue.trim(), 1033)
                        write-host -foregroundcolor Magenta "SubTerm $termValue has been created"
                        $termSet.TermStore.CommitAll();
                    }
                    else
                    {
                        write-host -foregroundcolor DarkMagenta "SubTerm $termValue Already Exists"
                    }
                }
            }
1

Wictor Wilen created a macro that allows you to import a csv into the MMS straight from Excel, maybe an option?

http://www.wictorwilen.se/Post/Create-SharePoint-2010-Managed-Metadata-with-Excel-2010.aspx

0

I suggest to avoid powershell (especially for more complex taxonomy) or importing from code.

Try "SharePoint 2010 Taxonomy Import Utility " and you'll see that this is the most elegant way for this task.

(Taxonomy data you define in .xml and you have ability to properly and easy define all possible attributes)

2
  • Is there any method to convert csv file to xml??
    – vikky22
    Commented Sep 22, 2011 at 11:51
  • As I know there isn't, but you can (if you are developer or you can contact one) relatively easy (at least in .net) make parser for csv, fill objects and than serialize them to xml.
    – trip
    Commented Sep 22, 2011 at 13:43
0

You can try using the script I wrote which exports taxonomy to XML and then imports it to a new Metadata Service.

1
  • Welcome to SharePoint StackExchange. Although your answer may answer the question, links to blog posts often break, where as we won't be going anywhere for a long while. It's always a good idea to extract the useful parts for research if not all of it incase that happens (If it's not too long!) Please read the FAQ[1] for more information [1]:sharepoint.stackexchange.com/faq
    – Hugh Wood
    Commented Dec 2, 2012 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.