0

I am trying to delete the site with app only permissions.

Below is my Provider Hosted app permissions

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

Below is the code I am trying

using (var ctx = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(
                                <TenantAdminUrl> // Tenant Url
                                , <ClientID> // Client Id
                                , <ClientSecret>)) // Client Secret

{
    var tenant = new Tenant(ctx);
    var removeSite = tenant.RemoveSite(<siteurl>);
    ctx.Load(removeSite);
    ctx.ExecuteQueryRetry();
}

Above code is giving me following error

Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: Access denied. You do not have permission to perform this action or access this resource. at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream) at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse() at Microsoft.SharePoint.Client.ClientRequest.d__6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.SharePoint.Client.ClientRequest.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.SharePoint.Client.ClientRuntimeContext.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.SharePoint.Client.ClientContext.d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.SharePoint.Client.ClientContextExtensions.d__7.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

Update: 7/11 The site is read-only. Is there a way I can delete read-only site?

Tried below code to update site status and it doesn't throw any exception and does not remove read only flag.

using (var ctx = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(
   <TenantAdminUrl>
   , <ClientID>
   , <ClientSecret>)) // Client Secret
{
   var tenant = new Tenant(ctx);
   tenant.SetSiteLockState(<SiteUrl>, OfficeDevPnP.Core.SiteLockState.Unlock,true);
}
  • Does the current user who is performing the step is having "FullControl" access? As to perform an action via add-in, authorization (i.e. by user+add-in policy) is succeeded only when both the current user and the add-in have permission to perform that action. – Divya Sharma Jul 11 at 3:19
  • can you do the app registration process on the tenant site collection and then check ? If you have registered the app on any other site collection, AFAIK it didnt work. I could be wrong though ! – Gautam Sheth Jul 11 at 8:49
  • @DivyaSharma This is running with Azure Job and not running with specific user – Ravi Khambhati Jul 11 at 14:35
  • @GautamSheth other api is working for e.g. tenant.GetSitePropertiesFromSharePointByFilters. Problem is with "tenant.RemoveSite" – Ravi Khambhati Jul 11 at 14:37
  • @DivyaSharma I forgot to mention that site is read only – Ravi Khambhati Jul 11 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.