Showing posts with label SPQuery. Show all posts
Showing posts with label SPQuery. Show all posts

Friday, June 24, 2016

Get ListItem Attachments using Iterating Large SharePoint Lists with PowerShell

Get ListItem Attachments using Iterating Large SharePoint Lists with PowerShell.

$SPAssignment = Start-SPAssignment
$webUrl = "http://sharepointurl:1234/"   
$listname = "ListDisplayName"  
$destinationfolder = "c:\backupfiles"    
$site = new-object Microsoft.SharePoint.SPSite($webUrl)   
$web = $site.OpenWeb()        
$list = $web.Lists[$listname] 
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$query = '<Where><IsNotNull><FieldRef Name="ID" /></IsNotNull></Where>'
$spQuery.ViewAttributes = "Scope = 'Recursive'"
$spQuery.Query = $query
$spQuery.RowLimit = 2000
Write-Host "    ItemCount: " $list.ItemCount
do
{
    $spListItemCol = $list.GetItems($spQuery)
    $spQuery.ListItemCollectionPosition = $spListItemCol.ListItemCollectionPosition        
       foreach ($listItem in $spListItemCol)
       {
              Write-Host "    Item ID: " $listItem.ID      
              if (!(Test-Path -path $destinationfolder))       
              {           
                     $dest = New-Item $destinationfolder -type directory         
              }
              foreach ($attachment in $listItem.Attachments)   
              {       
                     $file = $web.GetFile($listItem.Attachments.UrlPrefix + $attachment)       
                     $bytes = $file.OpenBinary()               
                     $path = $destinationfolder + "\" + $listItem.ID + "_" + $attachment
                     Write "Saving $path"
                     $fs = new-object System.IO.FileStream($path, "OpenOrCreate")
                     $fs.Write($bytes, 0 , $bytes.Length)   
                     $fs.Close()   
              }
       }
}
while ($spQuery.ListItemCollectionPosition -ne $null)
Stop-SPAssignment $SPAssignment

Wednesday, June 3, 2015

CAML Query filter between two dates

CAML Query filter between two dates

Method 1:-
</And><Geq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'><Today OffsetDays='-91' /></Value></Geq></And>

Method 2: Without using OffsetDays :-
DateTime dtStart = DateTime.Now.AddDays(-2);
                string dtStartDate = dtStart.ToString("yyyy-MM-ddTHH:mm:ssZ");
                string dtEndDate = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ");

                Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<Where><And><Geq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'>" + dtStartDate + "</Value></Geq>" + "<Leq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'>" + dtEndDate + "</Value></Leq></And></Where>";

Method 3: With using OffsetDays :-
Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<Where><And><Geq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'><Today OffsetDays='-91' /></Value></Geq>" + "<Leq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'><Today OffsetDays='91' /></Value></Leq></And></Where>";

Method 4:-
SPQuery camlQuery = new SPQuery();
camlQuery.Query = "<Where><And><Geq><FieldRef Name="Created" /><Value Type="DateTime" IncludeTimeValue='FALSE'>" +SPUtility.CreateISO8601DateTimeFromSystemDateTime(ddtStartDate) + "</Value></Geq>" +
"<Leq><FieldRef Name="Created" /><Value Type="DateTime" IncludeTimeValue='FALSE'>" +
SPUtility.CreateISO8601DateTimeFromSystemDateTime(ddtEndDate) + "</Value></Leq></And></Where>";
SPListItemCollection collection = list.GetItems(query);

Featured Post

How to Restore Deleted Emails in Exchange Online (Microsoft 365): The Complete Admin Guide

From the user's own "Recover deleted items" button to PowerShell bulk restores, and how to find out who deleted what, and whe...

Popular posts