Create List Column in SharePoint using PowerShell

The example in this topic show how to use PowerShell to Create List Column in SharePoint

  • # Provide credentials over here
  • $creds = (New-Object System.Management.Automation.PSCredential "<<UserName>>",(ConvertTo-SecureString "<<Password>>" -AsPlainText -Force))
  •  
  • # Provide URL of the Site over here
  • # If you do not wish to pass credentials hard coded then you can use: -Credentials (Get-Credential). This will prompt to enter credentials
  • Connect-PnPOnline -Url http://MyServer/sites/MySiteCollection -Credentials $creds
  •  
  • # Get Context
  • $clientContext = Get-PnPContext
  •  
  • ##### Method Parameters #####
  •  
  • #"-List": Specify the list where you want your new column to be created
  •  
  • #"-DisplayName": Display Name of the new column
  •  
  • #"-InternalName": Internal Name of the new column
  •  
  • #"-Type": Type of the new field
  •  
  • #"Required" and "AddToDefaultView" are switch parameters.
  • # A "Switch Parameter" does not take a value. If it is written in code, it turns on the "action" otherwise taken as false.
  •  
  • #"-Required": If we specify the -Required, new column will be a required/mandatory in list
  • #"-AddToDefaultView": If we specify the -AddToDefaultView, new column will be a added to the default view of the list
  •  
  • # FieldOptions #
  •  
  • # It is very important that we don't specify any space in field internal name.
  • # If you want the method to consider your internal name and NOT the display name, you need to specify FieldOptions = "AddFieldInternalNameHint" as an option in below line of code
  •  
  • # 1. defaultValue - specify that a new field added to the list must also be added to the default content type in the site collection.
  •  
  • # 2. addToDefaultContentType - specify that a new field added to the list must also be added to the default content type in the site collection.
  •  
  • # 3. addToNoContentType - specify that a new field must not be added to any other content type.
  •  
  • # 4. addToAllContentTypes - specify that a new field that is added to the specified list must also be added to all content types in the site collection.
  •  
  • # 5. addFieldInternalNameHint - specify adding an internal field name hint for the purpose of avoiding possible database locking or field renaming operations.
  •  
  • # 6. addFieldToDefaultView - specify that a new field that is added to the specified list must also be added to the default list view.
  •  
  • # 7. addFieldCheckDisplayName - specify to confirm that no other field has the same display name.
  •  
  • Add-PnPField -List "New list" -DisplayName "New List Column" -InternalName "NewListColumn" -Type Text -Required -AddToDefaultView -FieldOptions AddFieldInternalNameHint
  •  
  • Disconnect-PnPOnline

Thank you for reading this article. This code was tested in SharePoint 2013


© 2019 Code SharePoint