Working with Site collection app catalog using CSOM in SharePoint Online

1) Add a site collection app catalog using CSOM

2) Remove a site collection app catalog using CSOM

3) List available site collection app catalog using CSOM

4) Find out if the site collection app catalog exists in a particular site collection

If you want to do these operations via PowerShell or O365 CLI, you can refer the official Microsoft Docs link here .


Adding Modern webparts to classic pages using PnP PowerShell

Recently stumbled upon something very interesting.

I received a very peculiar requirement. The gist of it was that they(our client) wanted to have Modern webparts in classic pages. So, consider for example, we have some SPFx webparts and some OOTB modern webparts like Highlighted Content webpart , People webpart etc. on a Modern page. Now, we want all of these webparts on a classic page.

So, I decided to use the Add-PnPWebPartToWebPartPage/Add-PnPWebPartToWikiPage commandlet of the PnP PowerShell.

Basically, it involves couple of steps which I have mentioned below:

Adding Highlighted Content webpart to Classic Publishing page/ Wiki Page (using .webpart file)

1) Go to your SharePoint Workbench page. The URL would be something like:

2) In the workbench page, add your Modern webpart like Highlighted Content webpart.

3) Now in the Edit mode itself, click on the Web part data button:


4) It opens a popup like below. Click on the Classic Pages tab :


5) Copy the entire contents, including the webParts tags and store it locally as a .dwp or .webpart file in your local machine or file storage.

6) Now, we will deploy the webpart on a classic page using PnP PowerShell.

7) Open your PowerShell and read the contents of the webPart xml as below and then add it your classic publishing page in a webPart zone:

or for Wiki pages as:

My Webpart file looks like this:


End Result:

Highlighted content webpart on classic page


People webpart on Classic page


Note: This approach works even for custom SPFx webparts. We just need to ensure that the SPFx webpart is deployed in the site collection.

Hopefully this answers questions as:
1) how to provision/deploy SPFx webparts on classic pages ?
2) How to provision/deploy OOTB SPFx/Modern webparts on classic pages on team sites or publishing sites ?

Automating add-in/app installation using ALM APIs with CSOM and PowerShell in SharePoint Online

Winter has Arrived ! The good guys at MS have come bearing gifts.

With the ALM APIs becoming available in SharePoint online, we are now able to do ALM operations and automate add-in/app operations on Provider-hosted, SharePoint-hosted as well as SharePoint Framework applications(webparts and extensions).

These ALM APIs are now the answer to several questions such as :

How to Install an app from the app catalog using CSOM C# or PowerShell ?

How to Deploy Provider Hosted Apps (Add-Ins) / SharePoint-hosted Apps(Add-ins) / SharePoint Framework Solution(SPFx webpart and extensions) programmatically Using CSOM C# or PowerShell In SharePoint Office 365 ?

Can I automate app/add-in installation for apps in the SharePoint ?

Can I automate app/add-in installation from app-catalog ?

Using these APIs, we are able to add, install, deploy, uninstall, retract and remove operations on the SharePoint solutions .

The ever awesome PnP community has now provided these hooks via the PnP Core as well as PnP PowerShell components. OOTB support is also possible via REST APIs.

In the below post we will take a look at how to add, install, deploy, upgrade, uninstall, retract and remove apps and SPFx webparts using CSOM C# as well as PowerShell.

Here onwards, app == Provider-hosted app or SharePoint-hosted app or SharePoint Framework Webpart or extension

Firstly, you need to install the latest PnP Core dll or PnP PowerShell (Nov 2017 or higher) to perform these operations as these are not supported via OOTB CSOM components currently.

Now, to get list of all available apps in app catalog, you can use the below code.:

To, upload an app, there are 2 methods. You can pass the file name or you can pass the byte array.

They are as below:

1) Upload using file path

2) Upload using byte array

Now, that we have uploaded the app, we need to deploy the app so that it can available to be installed in the sites.

Again, there are 2 methods. You can the appMetadata or you can pass the GUID of the app.

They are as below:

1) Deploy the app using AppMetadata

2) Deploy the app using App Id

After deploy the app, it will be available for installation.

There are 2 methods using which you can install the app in a site collection.

They are as below:

1) Install the app using AppMetadata

2) Install the app using App Id

Also, if you want to upgrade the app, you can use the Upgrade method.

1) Upgrade the app using AppMetadata

2) Upgrade the app using App Id

After installation/upgradation of the app, should you decide to uninstall the app from the site collection, you can use the below 2 snippets. It will do a clean uninstall. We dont need to remove it from the recycle bin as it will handled itself

1) Uninstall the app using AppMetadata

2) Uninstall the app using App Id

If you want to retract the app, you can use the Retract method. When you retract the app, it will remain unavailable for installation in the sites.

1) Retract app using AppMetadata

2) Retract app using App GUID

If you want to completely remove from the App Catalog, you can use the Remove
method. It will be a clean removal and the app will be removed from the recycle bin as well.

1) Remove app using AppMetadata

2) Remove app using App GUID

To use these above methods, you need to download the latest PnP CSOM core component. The version should be 2.20.1711 or higher.

Download and install it from the the nuget.


The equivalent methods in PnP PowerShell are listed below:

In the above code samples, I am using userName and password to authenticate. However, app-only authentication will also work i.e you can just provide a client id and secret with necessary permissions and make it work by somewhat as:

//SharePoint Online - App Only
string siteUrl = "";
string acsAppId = "70DA500D-6000-48D4-AA1F-22793A5FE814";
string acsSupport = GetString("ACS App Secret");
AuthenticationManager authManager = new AuthenticationManager();
ClientContext context = authManager.GetAppOnlyAuthenticatedContext(siteUrl, acsAppId, acsSupport);

Please note – These methods will only work on SharePoint online at the moment. If and when MS pushes it out to On-Premises, will update the post to reflect the same.

PS – Currently, upload, retract and remove operations will work only on the App catalog site. However, with the Site-collection specific App catalog being rolled out in coming days, they will be supported for those site collections as well.

References –

Authenticate to SharePoint using PnP Authentication Manager






PnP PowerShell download link(Nov 2017 or higher) – PnP November 2017 Release

Create Modern Team and Communication Sites in SharePoint Online using CSOM and PowerShell

At the moment of writing this article, we can’t create “Modern” site using pure CSOM and the Microsoft.SharePoint.Client.Tenant.dll.

However, using the latest PnP Core and PnP PowerShell components, you will be able to create them programmatically.

You can use the below code to create Communication site:

To create a Modern team site, you can use the below code:

These are currently in beta and may not always work as expected.

To install the latest PnP core, go to your project references > Manage nuget packages

Download the SharePointPnPCoreOnline component. Its version should be 2.20.1711 or higher.

You also need to add the statement

using OfficeDevPnP.Core.Sites;

in your class.

Install the PnP component of SPO mentioned in below screenshot.


To create “Modern” team site, you can also follow the approach that I have mentioned here which requires access to azure portal.

Using PnP PowerShell

To provision these site programmatically using PowerShell, you can use the PnP PowerShell commandlets mentioned below. Download the latest PnP PowerShell (November 2017) or higher) so that these commands are available.

For communication site –

    New-PnPSite -Type CommunicationSite -Title Contoso 
    -SiteDesign Showcase -AllowFileSharingForGuestUsers

For modern team site –

    New-PnPSite -Type TeamSite -Title Test Modern TeamSite 
    -Alias TestModernTeamSite -IsPublic

Reference – New-PnPSite

Consume the Graph API and create O365 groups/modern team sites with CSOM C#

In this post, we will talk about how to get the access token in CSOM C# and then talk to Graph.

From Interact with Graph and make O365 Groups with AzureFunctions PowerShell

What is the Trick?

The problem is to authenticate with Azure AD and get an access token that we can use to talk to the Office 365 Graph. The trick, is a little not-well known thing called Resource Owner grant.


I have a few links about the Resource Owner grant type at the end. Basically, this grant_type lets you use username/password to obtain an access token.

First of all we will setup Azure App registration.

We need the permission to Read and Write All groups (Group.ReadWrite.All).

If you are familiar with it, just skip ahead to the code section.

If not, you need to follow the below steps:

First of all, navigate to your Azure Portal site or Azure AD portal. You need to use the account which has access to the Azure AD.

Once logged in, go to the App registration page as below:

Azure Active Directory page > App registrations > New Application registration.


Inside that, enter your details by providing some name and a valid Sign-on URL. Keep the Application type as Web App / API. Then click on Create. See below screengrab :


Once the application is created, navigate to it.

Please copy the application ID. This will be our client Id.


Now, go to the required permission and click on Add.





Inside the delegated permissions, click on the Read and write all groups


Click the awesome Grant Permissions button at the top of the permissions registration, this grants it for users in your Active Directory.



You will need some clientsecrets – create them from Keys. I like them not expiring for a long time. So I pick the Never expires option. It’ll expire when I’m long gone.



Copy down your ClientSecret.

Also, copy the Application ID which will be you client Id.

Now, lets do some coding 🙂

For this demo purposes, I am using a console application.

You will need the below Nuget packages:


We are going to make use to PnP Core’s UnifiedGroupsUtility.CreateUnifiedGroup method to create a Unified group.

We are also going to make a POST request to get the Graph API access token, for that we are going to make use of the

System.Net.Http's HttpClient.

First we will create a class named AuthenticationResponse to map the JSON response.

Now, in our main method, it will be as below:

Done. This will create a Modern Unified team site with Office 365 group enabled.

References – Provisioning a “modern” team site programmatically

Interact with Graph and make O365 Groups with AzureFunctions PowerShell

SharePoint Online – Create communication sites using REST API and jQuery

At this year’s SharePoint Virtual Summit, Microsoft announced several improvements to SharePoint Online and One Drive. One of the important and interesting additions to that is new “Communication Sites”.

So, what is a communication site ?

According to this

A SharePoint communication site is a great place to share information with others. You can share news, reports, statuses, and other information in a visually compelling format.

Communication site is like Team Site in SharePoint site that anyone can create in the organization from SharePoint Home (it is available only in Office 365 and not in the on premise environment, neither 2013 nor 2016). So, this is for internal users of organization.
Communication sites are beautiful, dynamic sites that let you reach a broad internal audience, and that appear great on the web, in the SharePoint mobile app, on PC and of course on Mac.

So, how do I create communication site ?

There are 2 options –

1) Using the UI.

To create a communication site from the UI, simply go to your app launcher and click on the SharePoint tile and then click on create a site. Follow the below 2 screenshots:



I am using a first release tenant. In case you dont see these options, you need to wait for the general release of communication sites.

2) Using REST API endpoint and jQuery.

You can programmatically create communication sites using the REST API endpoint provided by SharePoint. Below is a simple example using jQuery, however its easily portable to angular or any other framework that you use.

There are 3 flavors available when you create a communication site, namely, Topic, Showcase and Blank.

Note: New in this API is the concept of SiteDesignID. Much like the in-product site creation flow, the SiteDesignID parameter maps to the included site designs. They are as below, just modify the above endpoint’s SiteDesignID parameter to create a communication site of your flavour:

  • Topic: null
  • Showcase: 6142d2a0-63a5-4ba0-aede-d9fefca2c767
  • Blank: f6cc5403-0d63-442e-96c0-285923709ffc
  • SPFx – how to detect modern or classic page in SharePoint Online

    When you create a webpart using the new SPFx model, you are able to create and deploy the webpart not only in “Modern” sites like Team sites or Communication sites, but also in the classic sites.

    The rendering of pages in classic sites is quite different from that in Modern sites. Because of that, there could be some issues related to css and html. So, sometimes you need to inject specific css or html in the webpart.

    Now, to do that, you first need to detect whether the webpart is present in classic site or modern site. Fortunately, SPFx provides us with Environment type variable, present inside the Microsoft sp-core-library, using which you can determine whether it is a classic site or modern site or if its your workbench page.

    The code for that is as below. Do note, it is available only if your SPFx version is 1.2 or higher.

    Add the below import statement in your SPFx webpart:

    import { Environment, EnvironmentType } from '@microsoft/sp-core-library';

    Now, in your method, you can detect the environment as below:

    private _renderHTML(): void {
    	if(Environment.type == EnvironmentType.ClassicSharePoint){
    		//do some stuff on classic page
    	}else if(Environment.type === EnvironmentType.SharePoint){
    		//do some stuff on modern page
    	}else if(Environment.type === EnvironmentType.Local){
    		//do some stuff on SharePoint workbench page

    Reference – EnvironmentType enumeration