Thursday, October 30, 2014

Caml Query with Managed Metadata, Date Time and Content Type in PowerShell

Context: I had to fetch list items from a list based on a CAML query. The caml query should fetch records based on two different column types(Managed metadata, datetime) and a contenttype.
The following is the PS script that I wrote to accomplish this task.

$listName = "List Name"
$webURL = "http://sharepoint/sites/sharepoint";
$para = "Managed metadata value";
$today = [Microsoft.SharePoint.Utilities.SPUtility]
         ::CreateISO8601DateTimeFromSystemDateTime([DateTime]::Today)
$ContentType = "custom contenttype"; 

$query = New-Object Microsoft.SharePoint.SPQuery
$camlQuery = [string]:: Format('
            
                
                    
                    {0}
                
                
                    
                        
                        {1}
                    
                     
                        
                        {2}
                        
                    
                           
            
        ',$ContentType , $para, $today);

$query.Query = $camlQuery
$listItem = $list.GetItems($query);

foreach($item in $listItem){ 
    Write-Host 'ID: ' $item['ID'] 
    Write-Host 'Title: ' $item['Title'] 
} 

No comments:

Post a Comment