Saturday, February 18, 2012

PowerShell example to add Managed Metadata, people and group, Hyperlink and all types of OOB SPFields types to list


I have seen in many post and questions on how to add value of a perticular SPField type to a list. Most of them are quite straight forward but there are still some SPField Types which require a different approach.
I have put down mostly all the Field type avilable Out Of Box to a SharePoint list and writting a PowerShell to add a item. 
Fields which require some attention are : Person or Group , Hyperlink or Picture, Managed Matadata etc
Note : I have not added 'External Data' field in the script.
Overview :
The Metadata structure I have used for reference 


PowerShell
[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")  
   
$site    =     new-object Microsoft.SharePoint.SPSite("{Site URL}")                                                         
$web     =    $site.rootweb                                                                                                 
$list    =    $web.Lists["{List Name}"]                                                                               
$newitem=    $list.items.Add()                                                                                                   
 
#Single line of Text                                                                                                 
$newitem["Title"]=    "Title Value";  
       
#Multiple line of Text                                                                               
$newitem["MultiLine"]=  "Multiline Data";  
 
#Date and Time   
<# This will also work 
$newitem["DateTime"]= 2/15/2011 
#>                                                                                         
$newitem["DateTime"]=   [DateTime]::Now.AddDays(5) 
 
#Person or Group 
$newitem["User"] = $web.EnsureUser("{User Name}") 
 
#Choice(menu to choose from) : "B" is my Choice value 
$newitem["Choice"] = "B"; 
 
#Number 
$newitem["Number"] = 2; 
 
#Currency 
$newitem["Currency"]= 50; 
 
#Hyperlink or Picture 
$urlnew-object Microsoft.SharePoint.SPFieldUrlValue($newitem["Link"]); 
$url.URL = "http://www.mocrosoft.com"$url.Description = "Microsoft"$newitem["Link"] = $url; 
 
#Lookup - (1,"1") - First input is the index of the item and Second input is the value at that index.   
$newitem["Lookup"] = new-object Microsoft.SharePoint.SPFieldLookupValue(1,"1"); 
 
#Managed Metadata 
$taxonomySession = Get-SPTaxonomySession -Site $web.Site 
<#Accessing the TermStore, if the Term store name is not know 
You can get the term stole name by this command : 
    Write-Host $taxonomySession.TermStores[0].Name  
Also it can be directly accessed on basis of index 
    $termStore = $taxonomySession.TermStores[0]; 
Bus it’s always recommended to us directly by name 
#> 
$termStore = $taxonomySession.TermStores["Metadata Service Application Proxy"]; 
#Accessing the Group in the Term Store 
foreach($item in $termStore.Groups) 
{ 
  if($item.Name -eq "{Level 1}") 
   { 
    $TermGroup = $item 
   } 
} 
#Accessing the Term Set in the group 
$termSet = $TermGroup.TermSets["{Level 2}"$terms = $termSet.GetTerms(10) 
$term = $terms | ?{$_.Name -eq "{Level 3}"$terms2 = $term.GetTerms(10) 
$term2 = $terms2 | ?{$_.Name -eq "Operations"$spItem = [Microsoft.SharePoint.SPListItem]$newitem$taxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$newitem.Fields["Metadata"$taxField.SetFieldValue($spItem,$term2) 
 
$newitem.update()  
  
$web.Dispose() 
$site.Dispose()