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

  • 17 March 2015
  • 0 replies
  • 19 views

Userlevel 7
Badge +10

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.  
  3. $_web = Get-SPWeb http://contoso.com
  4.  
  5. $_field = $_web.Fields | where {$_.Title -match 'TitleOfTargetField'}
  6.  
  7. $_ct = $_web.ContentTypes | where {$_.Name -match 'NameOfTargetContentType'}
  8.  
  9. $_field.Choices.Clear()
  10.  
  11. foreach ($item in $_web.Lists['TitleOfSourcelist'].Items){
  12.  
  13. $_field.Choices.Add($item.Title)
  14.  
  15. }
  16.  
  17. $_field.Update()
  18.  
  19. foreach ($usage in [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($_ct)){
  20.  
  21. $list = $web.GetList("$($web.Url)$($usage.Url)")
  22.  
  23. if($usage.IsUrlToList){
  24.  
  25. foreach($field in $_web.Fields){
  26.  
  27. if($list.Fields.ContainsField($field.InternalName)){
  28.  
  29. $listField = $list.Fields[$field.Id]
  30.  
  31. if($listField -ne $null){
  32.  
  33. $listField.SchemaXml = $field.SchemaXml
  34. $listField.Update()
  35.  
  36. }
  37.  
  38. }
  39.  
  40. }
  41.  
  42. }
  43.  
  44. }

 

To use the script do the following:

  • Replace http://contoso.com 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.


0 replies

Be the first to reply!

Reply