SharePoint 2013 Check folder exists in a document library using powershell

Following is a powershell function to check for a existence of folder in a document library.

Function CheckFolder($listParm) { TRY { $ErrActionPref = "CONTINUE"; $spQry = New-Object Microsoft.SharePoint.SPQuery $camlQry = "<Where> <And> <Contains> <FieldRef Name='ContentType' /> <Value Type='Computed'>Folder</Value> </Contains> </And> <And> <Eq> <FieldRef Name='Title' /> <Value Type='Text'>Name of Folder-To-Check</Value> </Eq> </And> </Where>" $spQry.ViewAttributes = "Scope = 'Recursive'" $spQry.Query = $camlQry $spLstItms = $listParm.GetItems($spQry) if ($spLstItms.Items.Count -gt 0)

{

foreach ($item in $spLstItms) { #items found return $true }

} } CATCH { #Error handling return $false } return $false } #I am calling this CheckFolder function as follows if ((CheckFolder $list) -eq $true) { #Matching folder found }


When I run the checkFolder function, it is not finding the folder within the document library.

Any ideas on why it is not working.

Thanks

Nate

July 27th, 2015 12:02am

Hi Nate,

I have written this for you, I have tested and it seems to be working,

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$web = Get-SPWeb "<<web url>>"
$list = $web.lists["<<list name>>"];

$spQuery = New-Object Microsoft.SharePoint.SPQuery;
$spQuery.ViewAttributes = "Scope='RecursiveAll'";
$spQuery.Query = "<Where><BeginsWith><FieldRef Name='ContentTypeId'/><Value Type='ContentTypeId'>0x0120</Value></BeginsWith></Where>";
$folders = $list.GetItems($spQuery);

foreach ($folder in $folders) {
	Write-Host "Current folder name: " $folder.Name ;
	#write your filter logic here
}


Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 2:16am

Thanks Rupesh, I am working on using CAML and recursively finding the folder. I will try your query and let you know.
August 2nd, 2015 7:08am

Sure, I am always there :)
Free Windows Admin Tool Kit Click here and download it now
August 2nd, 2015 11:36am

Hi Nate,

Any update?

Best Regards

August 6th, 2015 3:18am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics