2

What I am trying to do is access a SharePoint 2010 document library, change all root and sub folders of that library from their existing content type to a new content type. So far my code has somewhat worked but I cannot seem to see the content types although I can get the folders.

    Function RunThroughSubFolders($folder)
    {
       foreach($subfolder in $folder.SubFolders)
           {
            #write-host $subfolder.Name

            $OldCTName = "Document Set"
            $oldCT = $subfolder.ContentTypes[$OldCTName]

               if ($subfolder.ContentType.Name -eq $oldCT.Name)
                   {

                       write-host "OLD CONTENT TYPE NAME FOUND!("$oldCT.Name ")" $subfolder.Name

                       #write-host $subfolder["ContentTypeId"]
                        <#

                         $subfolder["ContentTypeId"] = $newCTID
                         $subfolder.Update()

                         #>

                    }
                    else
                    {
                     ##
                    }
             }
     }

     Function GetFolders($Folder)
       {
          foreach($subfolder in $Folder.SubFolders)
              {
                 RunThroughSubFolders($subfolder)
              }
       }

     $web = Get-SPWeb "//url//”

         foreach($list in $web.Lists)
           {
             if($list.Title -eq "Document Library")
                {
                  GetFolders($list.RootFolder)
                }
           }
1

ContentTypes and ContentType are not properties of an SPFolder object.

So your line

$oldCT = $subfolder.ContentTypes[$OldCTName]

will not work, and neither will

$subfolder.ContentType.Name -eq $oldCT.Name
#...
$subfolder["ContentTypeId"] = $newCTID

To fix the first part, I would recommend getting the SPContentType object from the list, so maybe something like:

if($list.Title -eq "Document Library")
{
    $OldCTName = "Document Set"
    $oldCT = $list.ContentTypes[$OldCTName]

    # multiple parameters are passed in Powershell as space separated, not comma separated
    GetFolders $list.RootFolder $oldCT 
}

and then pass the content type object on to your next function(s) in a similar way

Function RunThroughSubFolders($folder, $oldCT)
{
    # ...
}

Function GetFolders($Folder, $cType)
{
    foreach($subfolder in $Folder.SubFolders)
    {
        RunThroughSubFolders $subfolder $cType
    }
}

As for the second problem, what you probably want to be doing is

if ($subfolder.Item.ContentType.Name -eq $oldCT.Name)
{
    $subfolder.Item["ContentTypeId"] = $newCTID
    $subfolder.Item.Update()
}

Documentation:

SPFolder members

SPListItem members

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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