Recently I was working on automating SharePoint site provisioning PnP script. We wanted to schedule this script to auto-trigger on an hourly basis, so we scheduled the script.

As the script was to be scheduled, we started creating SharePoint Apps to manage authentication.

Register SharePoint Add-ins

Go to <site collection url>/_layouts/15/AppRegNew.aspx by using a web browser.

AppRegNew page form

AppRegNew

Enter values for the following form fields.

Select Create on the form. The page reloads and shows a confirmation of the values that you entered. Make a record of these values in a form that is easy to copy and paste.

Setting up an app-only principal with tenant permissions

The next step is granting permissions to the newly created principal. Since we're granting tenant-scoped permissions, this granting can only be done via the appinv.aspx page on the tenant administration site. You can reach this site via https://contoso-admin.sharepoint.com/layouts/15/appinv.aspx. Once the page is loaded, add your client ID and look up the created principal.

Tenant permissions

To grant permissions, you'll need to provide the permission XML that describes the needed permissions. Since this application needs to be able to access all sites + also uses search with app-only it needs below permissions.

<AppPermissionRequests AllowAppOnlyPolicy="true">

  <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />

</AppPermissionRequests>

When you click on Create, you'll be presented with a permission consent dialog. Press Trust It to grant the permissions.

Trust It

With the preparation work done, let's continue to the next chapter showing how you can use the created app principal via its client ID and secret combination.

While we try to connect using.

Connect-PnPOnline -Url <URL> -ClientId <ClientID> -ClientSecret <ClientSecret>

Happy development.

Connect with me on LinkedIn