Add Field to Sharepoint List Content Type using PNP

ComputerHabit 1,051 Reputation points
2021-08-13T21:11:14.89+00:00

I'm trying to add a field to an existing list content type using PowerShell SharePoint PNP.

Non of the add-pnpfield commands have a list switch. How do you update a content type in a list?

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

3 answers

Sort by: Most helpful
  1. Emily Du-MSFT 51,946 Reputation points Microsoft External Staff
    2021-08-16T08:51:52.683+00:00

    @ComputerHabit

    When you use following Add-PnPField PowerShell, you will add the column to the default list content type.

    $SiteURL = "https://crescenttech.sharepoint.com"  
    $ListName= "Team Projects"  
    $ColumnName= "ProjectStartDate" #Internal Name  
    
    $Cred = Get-Credential  
    
    Try {  
        Connect-PnPOnline -Url $SiteURL -Credentials $Cred  
    
        Add-PnPField -List $ListName -Field $ColumnName -ErrorAction Stop  
        Write-host "Site Column '$ColumnName' Added to List Successfully!" -f Green  
    }  
    catch {  
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red  
    }  
    

    For another list content type, add column in it through PnP PowerShell is not available. You could add the column in it manually. Here're steps:

    1.Go to list settings -> Content types section -> Select the content type.
    123524-11.png
    2.Add from existing site or list columns.
    123460-12.png

    Or you could add a column to the list manually, click "Add to all content types".
    123563-13.png


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.


  2. ComputerHabit 1,051 Reputation points
    2021-08-20T11:57:00.807+00:00

    Still a problem. The issue is after restore the sites content types are not all restored. The lists have the content type in them but they aren't tied to the site content types.

    I think the site has some sort of issues as well. I was attempting to use the Sharepoint PNP commands to export the site. The fields will not export.

    When I created the site I used SharePoint PNP to create all the content types and metadata. I must have done something wrong or the current pnp export commands just fail.

    Currently I working towards replacing some of the metadata in the content types with text boxes instead of lookups. I've been trying use PowerApps but I come up with the limit for lookup columns.

    0 comments No comments

  3. Emily Du-MSFT 51,946 Reputation points Microsoft External Staff
    2021-08-23T08:38:17.857+00:00

    @ComputerHabit

    Based on your description, I understand that you want to backup and restore a site collection including some custom content types with lookup columns.

    I suggest you use "Save site as a template" to backup and restore the site collection. Please refer this article.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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 comments No comments

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.