Add Office 365 CDN rule to have a document library for each SharePoint site

On SharePoint online, there is a possibility to activate a feature on the tenant that allows you to have CDN on some libraries inside your SharePoint. Typically, there are multiple files stored in this service and they will load faster on the page if you have this service activated.

By default, this feature is not activated on the SharePoint Online, so first, we need to run the following command in order to activate this feature. We have the possibility to complete this task in multiple ways using SharePoint Management Shell, PnP PowerShell, and Office 365 CLI (links to activate this below). In this post, I’ll use the PnP PowerShell and a SharePoint Admin which is mandatory for this task.

Connect-PnPOnline -url https://contoso-admin.sharepoint.com/ -UseWebLogin
#Activate the public CDN
Set-PnPTenantCdnEnabled -CdnType Public -Enable $true

We’ve now that we have activated the public CDN for our tenant we need to wait since this can take up to 15 minutes. By default, the public CDN origins are */masterpage, */style library and */clientsideassets. However, we can then add new rules to the SharePoint CDN. We will create one rule that will store on CDN a specific library, named CDNLIBRARY. So on each SharePoint site on your Tenant, you can create a library named CDNLIBRARY that automatically will be store the files on CDN. To activate the public CDN for all those libraries you just run the code below.

# Adding new reference
Add-PnPTenantCdnOrigin -CdnType public -OriginUrl */CDNLIBRARY

The default files stored are CSS, EOT, GIF, ICO, JPEG, JPG, JS, MAP, PNG, SVG, TTF, WOFF but you cand add more file extensions to that. To get the files that are stored on your SharePoint public CDN, use the command below.

Get-PnPTenantCdnPolicies -CdnType Public

If you intend to check if the CDN rule added is already ready to be used use the following command

Get-PnPTenantCdnOrigin -CdnType Public
Sample of Office 365 CDN commands

The URL of the content provided by Office 365 CDN is the following and different from the Original URL that we have most of the time. Remember that you need now to go on each site collection and add a new Document library to store the files extensions defined.

https://publiccdn.sharepointonline.com/<TenantHostName>/sites/siteURL/CDNLIBRARY/asset.png

In the image below, I show the difference of loading times on an image that is huge and beyond the recommended to display on a page. But we can see some differences in the loading time of the file using the CDN and the regular access to the image.

Loading time analyze

Conclusion

The implementation of this can increase the performance of your SharePoint but it is important to mention that you should avoid having big images.

References

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *