Tuesday, July 2, 2013

Column order for SharePoint 2010 ContentTypes using Powershell

 When you run below script it will add a field to content type and set the order. For my field I want to set the order next to ‘SAC Doc’ filed. For that I am reading existing index of ‘SAC Doc’ field and setting to my new field.

For column order you can refer this post as well

function SetColumnOrder([Microsoft.SharePoint.SPContentType]$contentType)
            #Write-Host $contentType.name
            [Microsoft.SharePoint.SPFieldLinkCollection]$flinks = $contentType.FieldLinks;
               #array object
               $fieldsArray = New-Object 'System.Collections.Generic.List[string]';

          #Read all the InternalNames of the FieldLinks into a list
          foreach($fieldLink in $flinks){
              $fieldchkboxInternalName =$contentType.Fields["SAC DOC"].InternalName
             # Write-Host $fieldchkboxInternalName
              $fieldDDLInternalName =$contentType.Fields["ddlTest"].InternalName

              $indexChkbox = $fieldsArray.IndexOf($fieldchkboxInternalName)
                    #Write-Host "checkbox index:" $indexChkbox
                    $indexDDL = $fieldsArray.IndexOf($fieldDDLInternalName)
                     #Write-Host "ddlindex:"  $indexDDL
                     #Write-Host  "fieldsArraycount" $fieldsArray.Count
                    if($indexChkbox -ne $indexDDL )
                          if($indexChkbox -le $fieldsArray.Count -1){
                                     Write-Host "SAC ddl index:"  $fieldsArray.IndexOf($fieldDDLInternalName)
                                     $msg = ("{0},{1}" -f "SAC ddl index order :", $fieldsArray.IndexOf($fieldDDLInternalName))
                                    $msg | out-file -FilePath $logfile  -append
                        #  $fieldsArray.Add($fieldDDLInternalName);
             #Add the array to the reorder
             #Update the ContentYpe


$URL = "http://spdev5:8002/sites/library"
$site = New-Object Microsoft.SharePoint.SPSite($URL)
$web = $site.openweb("")

$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "SAC_AddField2ContentTypes"
$logfile = ("{0}{1}.csv" -f $filenamestart, $timestamp)

$header = "S.No,Message"
$header | out-file -FilePath $logfile

$fieldName = "ddlTest"
$field = $web.Fields[$fieldName]
#$ct = $web.ContentTypes[$contentTypeName]
Write-Host "Fieldname:" $field
$ContentTypes = $web.ContentTypes


      foreach ($ContentType in $ContentTypes)
          $contentTypeGroup = $ContentType.Group
          $contentTypeName = $ContentType.Name

          if ( $contentTypeGroup  -like '*DMID*')
                   #if($ContentType.Fields["SAC DOC"] -ne $null)
                    #  write-host $ContentType.Name #$MyField.Id
                        #$MyField=$ContentType.Fields["SAC DOC"]
                  # }
                  #if($ContentType.Name -eq  "Laboratory Normal Ranges")
                  if($ContentType.Name -eq "CI"  -or $ContentType.Name -eq "Lab" )
                        if($ContentType.Fields["ddlTest"] -eq $null)
                              $link = new-object Microsoft.SharePoint.SPFieldLink $field
                              write-host "'SAC Doc' dropdownlist added to: " $ContentType.Name
                              $msg = ("{0},{1}" -f "'SAC Doc' dropdownlist added to:", $ContentType.Name)
                              $msg | out-file -FilePath $logfile  -append
                              SetColumnOrder $ContentType
                              write-host  "'SAC Doc' dropdownlist already added to: " $ContentType.Name -BackgroundColor Red
                              $msg = ("{0},{1}" -f "'SAC Doc' dropdownlist already added to:", $ContentType.Name)
                              $msg | out-file -FilePath $logfile  -append
 catch [System.Management.Automation.ExtendedTypeSystemException]
    Write-Host $_.Exception.ToString()
       $msg = ( "{0},{1}" -f "Error occurred while reading list..:", $_)
       $msg | out-file -FilePath $logfile  -append


1 comment:

  1. Hi Surya,

    Thanks for sharing info.

    Re-Order the sharepoint site content type columns using powershell script:


    Sasi Kumar Reddy