1
# CONNECT TO TENNANT
try
{
  Connect-PnPOnline `
    -Url https://bpk.sharepoint.com `
    -Credentials 'pnpdemo'
} 
catch
{
  Write-Host `
    -f red "Error: $($_.Exception.Message)"
}

# CREATE COMMUNICATION SITE
New-PnPSite `
  -Type CommunicationSite `
  -Title 'Provisioning Assignment' `
  -Url https://bpk.sharepoint.com/sites/benn25 `
  -SiteDesign 'Blank' 

# CONNECT TO COMMUNICATION SITE
try
{
  Connect-PnPOnline `
    -Url https://bpk.sharepoint.com/sites/benn25 `
    -Credentials 'pnpdemo'
} 
catch
{
  Write-Host `
    -f red "Error: $($_.Exception.Message)"
}

# GET SITE NAME
$SiteName = `
  Get-PnPWeb | `
  Select-Object `
  -expandProperty Title

# CREATE NEW LIST
New-PnPList `
  -Title 'Honours' `
  -Template GenericList `
  -Url lists/honours

# ADD NEW FIELD TO LIST
Add-PnPField `
  -List 'Honours' `
  -DisplayName 'Honours' `
  -InternalName 'Honours' `
  -Type Choice -Group 'Football Fields' `
  -AddToDefaultView `
  -Choices 'FA Cup', 'League Cup', 'European Cup/Champions League'

# CREATE LIST VIEW
Add-PnPView `
  -List 'Honours' `
  -Title 'Tidy View' `
  -Fields 'Honours'

# SET LIST VIEW AS DEFAULT
Set-PnPView `
 -List 'Honours' `
 -Identity 'Tidy View' `
 -Values @{DefaultView=$True}

# CREATE DOCUMENT LIBRARY
New-PnPList `
   -Title 'Player Bios' `
   -Url 'playerBios' `
   -EnableContentTypes:$true `
   -Template DocumentLibrary

# CREATE CUSTOM ROLE PERMISSION
Add-PnPRoleDefinition `
  -RoleName "Contribute (No Delete)" `
  -Clone "Contribute" `
  -Exclude DeleteListItems

# CREATE GROUP
New-PnPGroup `
  -Title "$SiteName Honours List Editors"

# CHANGE MEMBER PERMISSIONS TO READ ONLY
Set-PnPGroupPermissions `
  -Identity "$SiteName Members" `
  -RemoveRole 'Full Control', 'Edit', 'Read' `
  -AddRole 'Read'

# GET DOCUMENT TYPE
$parentContentType = `
  Get-PnPContentType `
  -Identity "Document" 

# CREATE PLAYER BIO CONTENT TYPE 
Add-PnPContentType `
  -Name 'Player Bio' `
  -Description 'Use for player bios' `
  -Group 'Football Content Types' `
  -ParentContentType $parentContentType

# CREATES NEW FIELDS FOR PLAYER BIO CONTENT TYPE
Add-PnPField `
  -DisplayName 'Squad Number' `
  -InternalName SquadNumber `
  -Type Number `
  -Required `
  -Group 'Football Content Types'

Add-PnPField `
  -DisplayName 'Player Position' `
  -InternalName PlayerPosition `
  -Type MultiChoice `
  -Choices "Striker","Defender","Midfielder","Goally" `
  -Required `
  -Group 'Football Content Types'

Add-PnPField `
  -DisplayName 'Date of Birth' `
  -InternalName DateofBirth `
  -Type Date `
  -Required `
  -Group 'Football Content Types'

Add-PnPField `
  -DisplayName 'Age' `
  -InternalName Age `
  -Type Calculated `
  -Formula ="IF(LEN([Date of Birth]) > 0,DATEDIF([Date of Birth],Today,'Y'),'0')" `
  -Group 'Football Content Types'

Add-PnPField `
  -DisplayName 'Club' `
  -InternalName Club `
  -Type Text `
  -Required `
  -Group 'Football Content Types' 

# ADDS THE FIELDS ONTO THE PLAYER BIO CONTENT TYPE
Add-PnPFieldToContentType `
  -ContentType 'Player Bio' `
  -Field SquadNumber

Add-PnPFieldToContentType `
  -ContentType 'Player Bio' `
  -Field PlayerPosition 

Add-PnPFieldToContentType `
  -ContentType 'Player Bio' `
  -Field DateofBirth 

Add-PnPFieldToContentType `
  -ContentType 'Player Bio' `
  -Field Age

Add-PnPFieldToContentType `
  -ContentType 'Player Bio' `
  -Field Club

# ADD PLAYER BIO CONTENT TYPE TO PLAYER BIO DOCUMENT LIBRARY 
Add-PnPContentTypeToList `
  -List 'Player Bios' `
  -ContentType 'Player Bio' `
  -DefaultContentType

When running my script I get this error:

format-default: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
    + CategoryInfo          : NotSpecified: (:) [format-default], CollectionNotInitializedException
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefaultCommand

Any idea why this is happening?

1
  • Check the comments given in this link. Try adding something like $view = before commands like Add-PnPView. Nov 25, 2019 at 12:31

2 Answers 2

2

I have seem to of fixed the script by putting every cmdlet into a variable.

Like so:

# CREATE NEW LIST
$fix1 = New-PnPList `
  -Title 'Honours' `
  -Template GenericList `
  -Url lists/honours

# ADD NEW FIELD TO LIST
$fix2 = Add-PnPField `
  -List 'Honours' `
  -DisplayName 'Honours' `
  -InternalName 'Honours' `
  -Type Choice `
  -Group 'Football Fields' `
  -AddToDefaultView `
  -Choices 'FA Cup', 'League Cup', 'European Cup/Champions League'

No more errors :)

1
  • 1
    The trick seems to be to run the commands into variables. Thanks @BennKingy. Jun 26, 2020 at 16:42
1

Which line of the PowerShell script throw the error?

Try to use the latest version of the PnP PowerShell to check if it works.

https://github.com/SharePoint/PnP-PowerShell/releases/tag/3.15.1911.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.