Advertisement

Responsive Advertisement

Add a new content type in Sharepoint Online o365 using PnP PowerShell

SharePoint Patterns and Practices (PnP) contains a library of PowerShell commands (PnP PowerShell) that allows you to perform complex provisioning and artifact management actions towards SharePoint. The commands use CSOM and can work against both SharePoint Online as SharePoint On-Premises.

The Office 365 Developer Patterns and Practices PowerShell Cmdlets turn out to be lifesavers for many Developers. This will  allow you to create artifacts in your SharePoint sites, like lists, fields, etc. without needing to click around or write a complex C# application.

The PowerShell Cmdlets come in 2 versions
  • Office 365
  • On-prem
To run these scripts, you need to install the SharePoint Online Management Shell and PnP PowerShell modules if you have not yet done .For PowerShell 5.0 or newer, run this command 

Install-Module Microsoft.Online.SharePoint.PowerShell

For older versions, download and run the SharePoint Online Management Shell installation package. After that, run this command in PowerShell as administrator: 

Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

To install PnP PowerShell, run this command as administrator: 

Install-Module SharePointPnPPowerShellOnline -AllowClobber
It’s good to update both of the modules regularly to get the latest changes and possible new command-lets.If you want to run the scripts by executing .ps1 files, you also need to run this command as administrator: 

Set-ExecutionPolicy Unrestricted

And a quick note in case you are using multi factor authentication (MFA): you need to change the authentication in the scripts slightly.

  • In the Connect-SPOService command-let, remove the -Credential parameter and its value. Doing so brings up the web login instead, which will prompt you for the authentication code.
  • For Connect-PnPOnline, omit the -Credentials parameter and its value, and replace it with -UseWebLogin.

To use the library you first need to connect to your tenant:

Connect-PnPOnline Url https://yoursite.sharepoint.com –Credentials (Get-Credential)
This below app will help you to understand how to returns the available apps from the app catalog  using PNP Powershell cmdlets

Run this below powershelgl script in your SharePoint online management shell

param(
    [Parameter(Mandatory)]
    [String]$siteUrl,
[parameter(Mandatory)]
  [string]$Name,
[parameter(Mandatory)]
  [string]$Description,
     [parameter(Mandatory)]
  [string]$Group,
     [parameter(Mandatory)]
  [string]$ParentContentType
     
     
)
Connect-SPOService ("{0}-admin.sharepoint.com" -f ($siteUrl.Substring(0, $siteUrl.IndexOf(".sharepoint.com")))) -Credential (Get-Credential)
Add-PnPContentType -Name "Project Document" -Description "Use for Contoso projects" -Group "Contoso Content Types" -ParentContentType $ct
Read-Host -Prompt "Press Enter to exit"

powershell.exe -Command ‘G:\Gowtham\PowerShell_Scripts\SP_Demo.ps1' -siteUrl “o365siteurl”-Name”]$Name,”-Description”-Group”Contoso Content Types”-ParentContentType”$ct”


Post a Comment

0 Comments