0

I have a powershell sharepoint script that give me all items in a document library of a site. Problem is, I get Documents I don't want. Like home.aspx and some other regular documents from sharepont itself My first idea was to exclude this document librarys which have this files inside. But I hobe there is a better solution to do this job. I use the sharepoint client component sdk. Maybe you can help me here.

Small Code Snippet:

       ForEach($List in $Lists)
   {
   if(($List.BaseType -eq "DocumentLibrary") -and ($List.Hidden -eq $false) -and ($list.EnableVersioning) -and ($list.Name -ne "Forms") -and ( $List.Name -ne "Site Assets") ){

   $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
   $Query.ViewXml="@<View Scope='RecursiveAll'>
                           <Query>
                               <Where>
                                   <Eq>
                                       <FieldRef Name='FSObjType'/><Value Type='Integer'>0</Value>
                                  </Eq>
                              </Where>
                            </Query>
                          </View>"
   $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
   $Ctx.Load($ListItems)
   $Ctx.ExecuteQuery()

1 Answer 1

0

While reading the document library you can exclude the system-related document library. Below is the list of system libraries in SharePoint, you can exclude the below list/library.

$systemlibs =@("Converted Forms", "Customized Reports", "Documents", "Form Templates", "Images", "List Template Gallery", "Master Page Gallery", "Pages", "Reporting Templates", "Site Assets", "Site Collection Documents", "Site Collection Images", "Site Pages", "Solution Gallery", "Style Library", "Theme Gallery", "Web Part Gallery", "wfpub") 

Refer to the below example:

Powershell - get a list of all the document libraries for a web application including content types - output to csv

How exclude the system related files while reading the document library in SharePoint?

You can refer to the below example to exclude system related documents, this example is in c# coding, however you can get the equivalent PowerShell object.

How to exclude hidden/system sharepoint files when enumerating a document library

2
  • thanks, I will check this
    – patchfox
    Commented Jun 26, 2020 at 9:46
  • I tested it. I think it works but I get an $null error in some items of the loop. "Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null. "File"" Without this array there is no error. I guess it's another nested array in front of the code of the snippet which has references they are in the loop but have no objects. Do you have an idea how I can catch this with an if condition or try, catch?
    – patchfox
    Commented Jun 26, 2020 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.