How to update a choice field in a content type via PowerShell

Nintex Newbie
11 0 3,137

Products: Nintex Workflow 2013, Nintex Workflow 2010

A request came in for how to dynamically update a content type via Nintex Workflow. The person wanted to update a content type field based on the values of items in a list. Below you will find a PowerShell script that can be used with NTX PowerShell action: NTX PowerShell Action - Initial Beta Release

PowerShell Script
  1. Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
  2. $_web = Get-SPWeb
  3. $_field = $_web.Fields | where {$_.Title -match 'TitleOfTargetField'}
  4. $_ct = $_web.ContentTypes | where {$_.Name -match 'NameOfTargetContentType'}
  5. $_field.Choices.Clear()
  6. foreach ($item in $_web.Lists['TitleOfSourcelist'].Items){
  7. $_field.Choices.Add($item.Title)
  8. }
  9. $_field.Update()
  10. foreach ($usage in [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($_ct)){
  11. $list = $web.GetList("$($web.Url)$($usage.Url)")
  12. if($usage.IsUrlToList){
  13. foreach($field in $_web.Fields){
  14. if($list.Fields.ContainsField($field.InternalName)){
  15. $listField = $list.Fields[$field.Id]
  16. if($listField -ne $null){
  17. $listField.SchemaXml = $field.SchemaXml
  18. $listField.Update()
  19. }
  20. }
  21. }
  22. }
  23. }

To use the script do the following:

  • Replace with the URL of the site you wish to execute the script against.
  • Replace TitleofTargetField with the Title of the ListField you are updating.
  • Replace NameOfContentType with the name of the Content Type you are updating.
  • replace TitleOfSourceList with the title of the source list you are pulling values from.

Note: By default this script uses the item property 'Title' as the source for updating the content type. This can be changed on line 13.