Create SharePoint Online list columns using powershell

SR VSP 1,251 Reputation points
2022-02-21T01:38:39.33+00:00

Hello,

I want to create a SharePoint Online list having 50 columns with different datatypes like Text, Choice, Multi Text, Lookup... can you advise if any PowerShell script to create this columns ?

Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

Answer accepted by question author
  1. Echo Du_MSFT 17,341 Reputation points
    2022-02-21T07:15:13.427+00:00

    Hi @SR VSP ,

    Long time no see, how are you?

    Please run the below PnP PowerShell script as an admin. It's worth noting, don't create more than 12 Lookup columns(fields) in the same list.

    #Config Variables  
    $SiteURL = "https://domain.sharepoint.com/sites/sitename"  
    $ListName= "ListNameA"  
    $ParentListName="ListNameB"  
       
    #Connect to PnP Online  
    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)  
    #Get the ID of the Parent List  
    $LookupListID = (Get-PnPList -Identity $ParentListName).ID  
       
    #Define XML Schema for Text Field  
    $ProjectCodeFieldXML= "<Field Type='Text' Name='ProjectCode' ID='$([GUID]::NewGuid())' DisplayName='Project Code' Required ='FALSE' EnforceUniqueValues = 'TRUE' Indexed='TRUE'></Field>"  
       
    #Define XML Schema for Multiline Text Field  
    $ProjectDescriptionFieldXML= "<Field Type='Note' Name='ProjectDescription' ID='$([GUID]::NewGuid())' DisplayName='Project Description' Required ='FALSE' RichText='TRUE' RichTextMode='FullHtml' ></Field>"  
      
    #Define XML Schema for Choice Field  
    $PriorityFieldXML= "<Field Type='Choice' Name='Priority' ID='$([GUID]::NewGuid())' DisplayName='Priority'>  
                     <CHOICES>  
                          <CHOICE>(1) High</CHOICE>  
                          <CHOICE>(2) Normal</CHOICE>  
                          <CHOICE>(3) Low</CHOICE>  
                    </CHOICES>  
                    <Default>(2) Normal</Default>  
                </Field>"  
      
      
    #Define XML Schema for Lookup Field  
    $ParentProjectFieldXML= "<Field Type='Lookup' Name='ParentProject' ID='$([GUID]::NewGuid())' DisplayName='Parent Project' List='$LookupListID' ShowField='Title'></Field>"  
       
    #Add Text Field to list from XML  
    Add-PnPFieldFromXml -FieldXml $ProjectCodeFieldXML -List $ListName   
      
    #Add Multiline Text Field to list from XML  
    Add-PnPFieldFromXml -FieldXml $ProjectDescriptionFieldXML -List $ListName  
      
    #Add Choice Field to list from XML  
    Add-PnPFieldFromXml -FieldXml $PriorityFieldXML -List $ListName  
      
    #Add Lookup Field to list  
    Add-PnPFieldFromXml -FieldXml $ParentProjectFieldXML -List $ListName  
    

    Here is my test:

    176287-1.jpg

    176250-2.jpg

    176256-3.jpg

    176321-4.jpg

    Thanks,
    Echo Du

    ======================================

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.