1

I want to reorder the columns in the document library that's associated with my custom content types when end user uploads a document.I am having few columns col1, col2, col3, col4,col5 where col2, col4 are mandatory.So i need to get the Name of the file as the first item, when the user uploads the document then col2, and col4.How to do this using PowerShell.

I tried with the code with the InternalNames of the columns:

      function ResetColumnOrderInListCT($siteurl,$columnorder)
      {
      foreach($ContentType in $ContentTypes)
          {
              $ctype = $list.contenttypes[$ContentType]
              $newFieldOrderfromxml = $colorder
              $newfieldorder = $newFieldOrder.Split(',')
              $ctype.FieldLinks.Reorder($newFieldOrder)
              $ctype.update()
              #$result.Add($ctype)
              Write-Host "Reordered fields for" $web.title "-" 
              $list.title "-" $ContentType
          }
        }
      ResetColumnOrderInListCT "http://sirvr1:123/sites/IT"  
      "LinkFilename,Title,Col2,col4" .

But here my LinkFileName is not coming in the top order as the internal name of the name is LinkFileName.

2
  • Are you looking for reordering fields in existing content type using a PowerShell function? – Asad Refai Jun 2 '15 at 9:58
  • Yes.that's what I am looking for and I am facing the issue with the Name field of the document library item.Other fields are coming properly with this function. – userAZLogicApps Jun 2 '15 at 10:00
2

Function to reorder fields in content type using PowerShell client object model.

Try{
    Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll'
    Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
    Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll'
    Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll'
}
catch {
    Throw "Unable to load SharePoint Client runtime"
}

function Reorder-ContentTypeFields()
{
    param(
        [Parameter(Mandatory=$true)][string]$url,
        [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
        [Parameter(Mandatory=$true)][string]$ContentTypeName
    )

    begin{
        try
        {
            #get Client Object
            $Context = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
            $Context.Credentials = $Credentials 

            #Load web object
            $web = $Context.Web
            $Context.Load($web)
            $Context.ExecuteQuery()

        }
        catch
        {
            Write-Host "Error while getting context. Error -->> "  + $_.Exception.Message -ForegroundColor Red
        }
    }
    process{
        try
        {
            $contentTypes = $Context.Site.RootWeb.ContentTypes
            $Context.Load($contentTypes)
            $Context.ExecuteQuery()
            $contentType = $contentTypes | Where {$_.Name -eq $ContentTypeName}
            $viewFields = New-Object System.Collections.Specialized.StringCollection  
            #Col1 = Internal Name of a field
            $viewFields.Add("Col1")
            $viewFields.Add("Col2")
            $viewFields.Add("Col3")


            $contentType.FieldLinks.Reorder($viewFields)
            $contentType.Update($true)
            $Context.Load($contentType)
            $Context.ExecuteQuery()

            Write-Host "Fields reordered successfully" -ForegroundColor Green
        }
        catch
        {
            Write-Host ("Error while reordering fields in contet type. Error -->> " + $_.Exception.Message) -ForegroundColor Red
        }
    }
    end{
        $Context.Dispose()
    }


}
2
  • No worries.. Always happy to help.!! :) – Asad Refai Jun 2 '15 at 10:33
  • That StringCollection object did the trick. That was the key. :-) – userAZLogicApps Jun 2 '15 at 10:38

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.