Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11508

Loop Through All Items in a Document Set and Return File Names and Custom Columns

$
0
0

I am working on a PowerShell Script that starts with a base web application and loops through all site collections, sites, a library within the sites with a given name that is the same across sites. These libraries are filled with document sets which I need to loop through and perform an action based on a custom column. The part I am stuck on is looping through the document libraries. So what I would really like to do for now is loop through the document sets and output the containing file's names and some custom columns.

I am pretty new to PowerShell, but so far I have come up with two scripts. The first will get to the document set contents, but for the document set name it returns "Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet and the file path starting with the library (ex: My Docs/DocumentSet/myFile.docx). I am not worried about the output of the document set name as long as it loops through the document set, so the output of the namespace (I guess that is what that is) is ok, but I need to work with the properties and not just get the path of the file. The second loops through the library and will show the document set name, but always returns null for items within it (I think I am somehow working with the document set instead of the full object?).

The code for each is below. Any help to achieve my goal would be greatly appreciated.

Returns the content files of the document set's path:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.DocumentManagement");
#Declare variables
$webApp = Get-SPWebApplication "http://mysite.com";
foreach ($site in $webApp.Sites)
{
  Write-Host $site -ForegroundColor Blue;
  foreach ($web in $site.AllWebs)
  {
    Write-Host $web -ForegroundColor Cyan;
    foreach ($list in $web.Lists["My Documents"])
    {
      Write-Host $list -ForegroundColor Magenta;
      $rootFolder = $list.RootFolder;
      foreach ($docsetFolder in $rootFolder.SubFolders)
      {
        $docSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::GetDocumentSet($docsetFolder);
        Write-Host $docSet -ForegroundColor Green;
        foreach ($item in $docsetFolder.Files)
        {
          if ($item -NotMatch "Forms")
          {
            Write-Host $item -ForegroundColor Yellow;
          }
        }
      }
    }
  }
}

This way gets the document set's name, but nothing inside:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.DocumentManagement");
$webApp = Get-SPWebApplication "http://mysite.com";
foreach ($site in $webApp.Sites)
{
  Write-Host $site -ForegroundColor Blue;
  foreach ($web in $site.AllWebs)
  {
    Write-Host $web -ForegroundColor Cyan;
    foreach ($list in $web.Lists["My Documents"])
    {
      Write-Host $list -ForegroundColor Magenta;
      foreach ($docsetFolder in $list.folders)
      {
        Write-Host $docSetFolder.Name -ForegroundColor Green;
        foreach ($item in $docsetFolder.Files) #.Items works the same as .Files, it would seem. I guess there is a difference depending on how it is used, but in this case it returns the same
        {
          if ($item -NotMatch "Forms")
          {
            if ($item -eq $null)
            {
              Write-Host "This is NULL" -ForegroundColor Red;
            }
            else
            {
              Write-Host $item["Name"] -ForegroundColor Yellow;
            }
          }
        }
      }
    }
  }
}

I am still learning and eager to do so. I have been working on this and reasearching for too long. Any help with this would be greatly appreciated.

Many thanks,

J.



Viewing all articles
Browse latest Browse all 11508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>