Showing posts with label Start-SPAssignment. Show all posts
Showing posts with label Start-SPAssignment. Show all posts

Tuesday, February 21, 2017

Get folder and file names from SharePoint SiteCollection using PowerShell

Get folder and file names from SharePoint SiteCollection using PowerShell
Step1: Copy below code into notepad and save as 'getFiles.ps1' and save.
--------------------------------------
Start-SPAssignment -Global | Out-Null
function Get-SPWebs($SiteCollection)
{
       $SiteCollection = Get-SPSite $SiteCollection
       $webs = @()
       $SiteCollection.allwebs | %{$webs += $_.url}
       return $webs
}
function Get-SPFolders($webs)
{
       foreach($web in $webs)
       {
              $web = Get-SPWeb $web
              Write-Host "`n$($web.url)" -ForegroundColor Green
              Add-Content C:\log01.txt $($web.url)
              $lists = $web.lists | ?{$_.Hidden -eq $false}
              #$lists = $web.lists | ?{$_.itemcount -ge "1" -And $_.Hidden -eq $false -And $_.BaseType -eq "DocumentLibrary"}
              #$lists = $web.lists | ?{$_.title -eq "Documents" -and $_.itemcount -ge "1" -And $_.BaseType -eq "DocumentLibrary"} #Change any identifier here
              foreach($list in $lists)
              {                   
                     Write-Host "- $($list.RootFolder.url)"
                  Add-Content C:\log01.txt "  - $($list.RootFolder.url)"
                     $rootfolder = $web.GetFolder($list.RootFolder.Url)
                     Get-SPFileNames($rootfolder)
                     foreach($folder in $list.folders)
                     {
                           $folder = $web.GetFolder($folder.url)
                           Get-SPFileNames($folder)
                     }
              }
              $web.dispose()
       }
}
function Get-SPFileNames($folder)
{
       foreach($file in $folder.Files)
       {            
              Add-Content C:\log01.txt "        - $($file.Url)"
       }
}
$Sitecollection = "http://SharePointURL:1234"
$webs = Get-SPWebs -SiteCollection $Sitecollection
Get-SPFolders -Webs $webs
Stop-SPAssignment -Global
--------------------------------------
Step2: Open 'SharePoint Management Shell' as 'Run as administrator' -> open 'getFiles.ps1' and click enter to execute script.
Step3: OutPut: we can similar out as below.
--------------------------------------
http://SharePointSiteURL:1234
   - DocLib1
        - DocLib1/Doc2.txt
        - DocLib1/Doc1.rtf
        - DocLib1/folder1/CustomList1.JPG
        - DocLib1/folder2/Bootstrap.JPG
  - Shared Documents
        - Shared Documents/ImageSlider/4.jpg
        - Shared Documents/ImageSlider/7.jpg
  - SitePages
        - SitePages/ImageSlider.aspx
  - Style Library
        - Style Library/Media Player/MediaWebPartPreview.png
  - Lists/test
        - Lists/test/DispForm.aspx
        - Lists/test/EditForm.aspx
        - Lists/test/AllItems.aspx
        - Lists/test/view1.aspx
        - Lists/test/NewForm.aspx
-------------------------------------- 

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

Featured Post

SharePoint Classic Blank-Fallback Redirect Page via CEWP

SharePoint Classic — Blank-Fallback Redirect Page via CEWP Query Parameter Routing · jQuery · Content Editor Web Part · Site Assets · wind...

Popular posts