Latest Posts

Add-PnPField: Create List Column in SharePoint using PowerShell

The example in this topic show how to use PowerShell to Create List Column in SharePoint
  • Using PnP (Pattern and Practice) PowerShell commands, you can access and manipulate SharePoint objects easily.
  • Prerequisite: You have to install cmdlets to work in PnP PowerShell.

How to run PnP PowerShell command in SharePoint?

  • First, open powershell ise.
  • powershell ise
  • Make sure you run it in Administrator Mode.
  • Now, copy and paste the code mentioned below and click on Run Script as shown in the screenshot.
  • powershell ise run command

  • # 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


We value your Feedback:

Page URL:

Name:

Email:


Suggestion:

© 2024 Code SharePoint