powershellsharepoint-2010document-librarysharepointdocumentlibrary

PowerShell script to update ContentType of existing records in Document Library


we are using Document Library in SharePoint 2010 environment. there are about 200 document present in the document library which have been identified (based on location - England and employee type - FT) and now i have to change the content type of these 200 documents to a new content type 'New FT'. I'm using the below power shell code for the purpose -

$webUrl  = "http://www.site.com/sites/Library/"
$web = Get-SPWeb -Identity $webUrl
$list = $web.Lists["CURRENT FTE"]
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'";
$spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition

    foreach($item in $listItems)
    {
        $lookup  = [Microsoft.SharePoint.SPFieldLookupValue]$item["ROLE"];   
        $role = $lookup.LookupValue;

        $EMPTYPE  = $item["EMP TYPE"]

        $lookup4  = [Microsoft.SharePoint.SPFieldLookupValue]$item["Location"];   
        $LOCATION = $lookup4.LookupValue;

        $ContentTypeName = $item.ContentType.Name
        $ContentTypeID = $item.ContentType.Id

         if ($LOCATION -eq "England") 
         {
            if($EMPTYPE -eq "FT")
            {   
                #CheckList - 1
                Write-Output  "ID - $($item.id) " 
                Write-Output  "Name - $($item.name)"
                Write-Output  "Role - $($role) "  
                Write-Output  "emptype - $($EMPTYPE) "
                Write-Output  "location - $($LOCATION) "
                Write-Output  "Content Type Name - $($ContentTypeName) "
                Write-Output  "Content Type ID - $($ContentTypeID) "
                Write-Output "  "

                $newct = $list.ContentTypes["New FT"]
                $newctid = $newct.ID

                    If (($newct -ne $null) -and ($newctid -ne $null))
                        {
                            $item["ContentTypeId"] = $newctid
                            $item.Update()

                            $newContentTypeName = $item.ContentType.Name
                            $newContentTypeID = $item.ContentType.Id

                            #CheckList - 2
                            Write-Output  "ID - $($item.id) " 
                            Write-Output  "Name - $($item.name)"
                            Write-Output  "Role - $($role) "  
                            Write-Output  "emptype - $($EMPTYPE) "
                            Write-Output  "location - $($LOCATION) "
                            Write-Output  "Content Type Name - $($newContentTypeName) "
                            Write-Output  "Content Type ID - $($newContentTypeID) "
                            Write-Output "  "
                            }



            }
        }

    } 

Now the code identifies each document/record and then prints the details as listed on CheckList - 1, then i do an update and try to print the new updated values in CheckList - 2 - but the problem is the CheckList -2 doesn't print the updated content type and updated content type ID ($newContentTypeName, $newContentTypeID).

Am i doing some thing wrong here , Please advise ! (feel free to update the code if necessary)


Solution

  • Well with the help from 'Yevgeniy.Chernobrivets' comment above, I was able to make all the fields appear in the CheckList - 2. As per the comment I had to do the below -

     $updateItem = $list.GetItemById($item.ID); 
    $contentTypeID = $updateItem.ContentType.Id
    $contentTypeName = $updateItem.ContentType.Name