Suppose you would like to have the remote components of a provider-hosted SharePoint Add-in to interact with SharePoint, what should you do? You should first register with the Azure ACS cloud-based service and the SharePoint App Management Service of the tenancy or farm. Let us now dive straight into how to register/unregister SharePoint add-ins.
To register an app into office 365 tenancy we use the layouts/appregnew.aspx page which is a standard old way of doing it. But, I thought there should be some smarter way to do it. After much research, I got a scenario to automate this process using a deployment package. I was searching for a while on the internet and got a few ideas like updating expiring Client Secret etc. More on that here
I have used MSOLService from AzureAD powershell package to register the SharePoint Add-in Office365 tenancy. Here is how to register an Add-in.
Download and install the AzureAD powershell package using the below PS commands.To Save Module run the below script.
- Save-Module -Name MSOnline -Path <path>
- Install-Module -Name MSOnline
Provide your Global administrator username and password while connecting to MSOLService.
- $clientID = "7c6cbd92-073b-4bc9-9d4a-fc27749f340b";
- $bytes = New - Object Byte[] 32
- $rand = [System.Security.Cryptography.RandomNumberGenerator]::Create()
- $rand.GetBytes($bytes)
- $rand.Dispose()
- $newClientSecret = [System.Convert]::ToBase64String($bytes)
- $appDomain = "Yourdomain.azurewebsites.net";
- $appUrl = "https://Yourdomain.azurewebsites.net/";
- $appName = "Your App Name"
- $servicePrincipalName = @("$clientID/$appDomain")
- $dtStart = [System.DateTime]::Now
- $dtEnd = $dtStart.AddYears(3)
- Connect - MsolService
- New - MsolServicePrincipal - ServicePrincipalNames $servicePrincipalName - AppPrincipalId $clientID - DisplayName $appName - Type Symmetric - Usage Verify - Value $newClientSecret - Addresses(New - MsolServicePrincipalAddresses - Address $appUrl) - StartDate $dtStart– EndDate $dtEnd
- New - MsolServicePrincipalCredential - AppPrincipalId $clientId - Type Symmetric - Usage Sign - Value $newClientSecret - StartDate $dtStart– EndDate $dtEnd
- New - MsolServicePrincipalCredential - AppPrincipalId $clientId - Type Password - Usage Verify - Value $newClientSecret - StartDate $dtStart– EndDate $dtEnd
If you have provided the wrong data while registering, then you have to unregister or remove the entire add-in configuration from the Azure AD and you need to register it once again.
To unregister the add-in you can use the below script. Make sure that you are providing the global admin credentials that we already gave while registering.
- $clientID="7c6cbd92-073b-4bc9-9d4a-fc27749f340b";
- Connect-MsolService
- $appPrincipal = Get-MsolServicePrincipal -ServicePrincipalName $clientID
- Remove-MsolServicePrincipal -ObjectId $appPrincipal.ObjectId

Packiaraj SanthiyaguPosted Oct 11, 2018, 8:06 AM
Needed one Arut, Thanks for sharing
Ravishankar NPosted Jul 1, 2018, 9:42 AM
I used your Above code and tried to unregister provided hosted app installed on my sharepoint site... But still my app client id is looked up in appinv.
Chanakya JayabalanPosted Jun 11, 2018, 10:53 AM
Nicely written Arut!
Md Tahmidul AbedinPosted Jun 8, 2018, 3:45 AM
Thanks for this script. :)