Deploy

Add SharePoint Framework package to the app catalog and Azure AD app certificate

This is example script how to add SharePoint Framework package to the app catalog using Microsoft 365 CLI and Azure AD app certificate.

Summary

This sample demonstrates how to upload spfx solution to app catalog using CLI for Microsoft 365 and Azure AD App created with certificate authentication.

Outupt Screenshot

powershell

#create self signed certificate
function CreateSelfSignedCertificate{

    $cert = New-SelfSignedCertificate -Subject "CN=$CommonName" -FriendlyName $CommonName -NotBefore $StartDate -NotAfter $EndDate  -CertStoreLocation "Cert:\CurrentUser\My" -KeyExportPolicy Exportable -KeySpec Signature -KeyLength 2048 -KeyAlgorithm RSA -HashAlgorithm SHA256

    # Export Certificate from Variable to cert file to the location where script is executed
    Export-Certificate -Cert $cert -FilePath ".\$CommonName.cer"

    # Secure the file with password for enhanced security
    $mypwd = ConvertTo-SecureString -String "$Password" -Force -AsPlainText

    # Exporting the file to PFX file with Password
    Export-PfxCertificate -Cert $cert -FilePath ".\$CommonName.pfx" -Password $mypwd
}

$CommonName= "My App Certificate"
$StartDate= "2023-07-27"
$EndDate= "2045-12-31"
$Password = "yourPassword"
$TenantId = "yourTenantId"
$PathToSpfxApp = "spfx-solution.sppkg"

CreateSelfSignedCertificate -CommonName $CommonName -StartDate $StartDate -EndDate $EndDate -Password $Password

# create aad app and grant permissions to graph and sharepoint
m365 login 
$appName = "AAD app demo"
$app = m365 aad app add --name $appName  --certificateDisplayName "Certification" --certificateFile "$CommonName.cer" --apisApplication 'https://graph.microsoft.com/Sites.Read.All,https://microsoft.sharepoint-df.com/Sites.FullControl.All' --grantAdminConsent
$appId = $app | ConvertFrom-Json | select -ExpandProperty  appId 

# grant permissions to site
$site = "https://tenant.sharepoint.com/sites/SiteColectionName"
m365 spo site apppermission add --appId $appId --permission fullcontrol --siteUrl $site 

# login to sharepoint using app
m365 login --authType certificate --certificateFile "$CommonName.pfx" --password $Password --appId $appId --tenant $TenantId
m365 spo set --url $site
m365 status

# upload sppkg to app catalog
m365 spo app add --filePath $PathToSpfxApp --overwrite --verbose

Contributors

Author(s)
Valeras Narbutas

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Tool documentation & references

Keep exploring