Wednesday, April 22, 2015

PowerShell to delete the SPFolder from the SharePoint Document Library using input CSV file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
$snapinName = "Microsoft.SharePoint.PowerShell"  
 if ((Get-PSSnapin | Where-Object {$_.Name -eq $snapinName }) -eq $NULL) {  
  write-host "SharePoint SnapIn not loaded. Loading..."  
  Add-PSSnapin $snapinName -ErrorAction SilentlyContinue  
 }  
 #Change the URL for different environment.  
 #$webUrl = "http://usdttdevbq005:1000/sites/Test/"  
 #$listUrl = "http://usdttdevbq005:1000/sites/Test/Resumes/"  
 $webUrl = "http://sp2010:23934/"  
 $listUrl = "http://sp2010:23934/TEST/"  
 $destination=".\"  
 $Datetime= Get-Date -Format dd_MM_yyyy_HH.mm.ss   
 $InputCSVFile=".\Input_GUID.csv"  
 #Log file name   
 $LogFileName=([string]::Concat("Output_GRR_Bulk_DeleteFolder_Log" , $Datetime ,".txt"))  
 $ListOfGuidNotFound=""  
  Try  
     { # Start Try  
    New-Item .\$LogFileName -ItemType file   
     "variable initialization completed.."  
     Add-Content .\$LogFileName -Value "--->  variable initialization completed.."  
     $web = Get-SPWeb -Identity $webUrl  
     $list = $web.GetList($listUrl)  
     "Document Library access.."  
     Add-Content .\$LogFileName -Value "--->  Document Library access.."  
     $userGUIDCollection=import-Csv $InputCSVFile | where {$_.key -eq $keyword} |select UserGUID   
     "Read the CSV.."  
     Add-Content .\$LogFileName -Value "--->  Read the CSV.."  
 #Main start  
 #Download files in folders  
  foreach($VaruserGUID in $userGUIDCollection)  
  {  
     "working for CSV GUID : " + $VaruserGUID.UserGUID.Trim().ToString()  
      Add-Content .\$LogFileName -Value ([string]::Concat("`r`n--->  working for CSV GUID : ",$VaruserGUID.UserGUID.Trim().ToString()))  
    $varTemp = 0  
   foreach ($folder in $list.Folders)   
   {# Start of For  
       If($VaruserGUID.UserGUID.Trim().ToString() -eq $folder.DisplayName.Trim().ToString())  
     {  
       "GUID Matches : " + $VaruserGUID.UserGUID.Trim().ToString()  
       Add-Content .\$LogFileName -Value ([string]::Concat("--->  GUID Matches with Resume folder name : "+ $VaruserGUID.UserGUID.Trim().ToString() ))  
       $list.Folders.DeleteItemById($folder.ID)   
                  Add-Content .\$LogFileName -Value ([string]::Concat("--->  Folder has been deleted for : "+ $VaruserGUID.UserGUID.Trim().ToString() ))  
        $varTemp = 1  
     }  
   }#End of For  
       If($varTemp -eq 0) # Checking that the GUID is found in Resume folder, Yes or No  
     {  
       $ListOfGuidNotFound = $ListOfGuidNotFound.Trim().ToString() +"`r`n"+ $VaruserGUID.UserGUID.Trim().ToString()  
     }    
 }  
   Add-Content .\$LogFileName -Value ([string]::Concat("`r`n*******START*** GUID Not Matches with Resume folder for GUID Start***********`r`n"+ $ListOfGuidNotFound +"*******END*** GUID Not Matches with Resume folder for GUID END***********`r`n"))  
     } # End Try  
   catch   
   {  
           $ErrorMessage = $_.Exception.Message  
           Add-Content .\$LogFileName -Value ([string]::Concat("************************************"))  
           Add-Content .\$LogFileName -Value ([string]::Concat("**********Error with writing files to folder*******************"))  
           Add-Content .\$LogFileName -Value ([string]::Concat("System Message: ",$ErrorMessage))  
           Add-Content .\$LogFileName -Value ([string]::Concat("************************************"))  
           continue  
   }   

No comments:

Post a Comment

HTML

Script:

JS