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()   

Tuesday, January 17, 2012

Improve SharePoint Search Crawling depending upon machine RAM configuration


Introduction:
Improvement of SharePoint Search crawling by changing the default value of ‘Gathering Manger’ in registry depending upon machine RAM configuration.
Description:
There are few keys in Registry which are very useful to understand the Crawling setting for SharePoint and improve depending upon the RAM configuration of the machine.
Location in Registry:
SharePoint 2010: HKLM:\SOFTWARE\Microsoft\Office Server\14.0\Search\Global\Gathering Manager\
SharePoint 2007: HKLM:\SOFTWARE\Microsoft\Office Server\12.0\Search\Global\Gathering Manager\
       1. FolderHighPriority ( only MOSS 2007 not in SharePoint 2010):
Represents the number of high priority folders that can be processed at one time.  If this is too high then the cache in the daemons will constantly be running out of space.  If this is too low then the crawl will be throttled waiting for more items to process. This field is present in MOSS 2007 but not used any more in SharePoint 2010.
2.  FilterProcessMemoryQuota:
Represents how much memory can be consumed by the search daemon process before it gets killed by the crawler. The OOB default has been chosen based on 4 GB of memory on the indexer. If the customer has higher RAM, they can increase this value to cache more data during the crawl.
3. DedicatedFilterProcessMemoryQuota:
Same as for FilterProcessMemoryQuota except this is the size of the single-threaded daemons.
Usually by default as it is configured for 4 GB RAM and in Enterprise scenarios we usually go for higher RAM configuration (8 GB, 16 GB, 32 GB etc). So, for good configuration of we should modify the default value.
Default values:
FolderHighPriority (only in MOSS 2007): 125
FilterProcessMemoryQuota: 104857600
DedicatedFilterProcessMemoryQuota: 104857600

Now if we consider for a RAM of 16 GB then the values can be proportionally modified. 
i.e. FolderHighPriority 125/4 * 16 = 500 similarly for other RAM size.
So, we need to multiply all values by a proportion of “[RAM Allocated] / 4“.
I have written a PowerShell  2.0 script for SharePoint 2010 to do the same modification depending upon the RAM size.
Below script is for RAM as 8 GB, change the value in ‘param’ depending upon the RAM configuration of machine. For generic it can take the RAM size as input. Post script it will require a restart of machine.
For MOSS 2007 include also 'FolderHighPriority' in the script.
=========================================
PowerShell 2.0 script
=========================================
param   ([int] $RAM = 8)
$factor = $RAM/4
# DedicatedFilterProcessMemoryQuota
$DedicatedFilterProcessMemoryQuota = 104857600
$val = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Office Server\14.0\Search\Global\Gathering Manager\" -Name DedicatedFilterProcessMemoryQuota
Write-Host -ForeGroundColor Yellow "Current DedicatedFilterProcessMemoryQuota: " + $val.DedicatedFilterProcessMemoryQuota
$newVal = $DedicatedFilterProcessMemoryQuota * $factor
Write-Host -ForeGroundColor Green "New DedicatedFilterProcessMemoryQuota: " + $newVal
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Office Server\14.0\Search\Global\Gathering Manager\" -Name DedicatedFilterProcessMemoryQuota -Value $newVal

# FilterProcessMemoryQuota
$FilterProcessMemoryQuota = 104857600
$val = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Office Server\14.0\Search\Global\Gathering Manager\" -Name FilterProcessMemoryQuota
Write-Host -ForeGroundColor Yellow "Current FilterProcessMemoryQuota: " + $val.FilterProcessMemoryQuota
$newVal = $FilterProcessMemoryQuota * $factor
Write-Host -ForeGroundColor Green "New FilterProcessMemoryQuota: " + $newVal
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Office Server\14.0\Search\Global\Gathering Manager\" -Name FilterProcessMemoryQuota -Value $newVal
Write-Host -ForegroundColor Red "You need to reboot the server"
Restart-Computer -Confirm
=================================================