1

In SharePoint Online Powershell, is it possible to connect SPOService using App ID and App Secret instead of passing user name and password

2
  • any particular use for SPO shell ? you can do it easily in PnP PowerShell Feb 28, 2019 at 6:54
  • @GautamSheth In PnP PowerShell "Set-SPOSite $siteUrl -DenyAddAndCustomizePages 0" cmd is not available. It is only available in SharePoint Powershell. Is there any alternate for this?
    – Prakash
    Feb 28, 2019 at 7:01

2 Answers 2

2

As mentioned in comments, you can set the DenyAddAndCustomizePages property via PnP PowerShell.

Its slightly different compared to the OOTB SPO management shell.

You can use the below code to achieve that:

Connect-PnPOnline -Url https://tenant-admin.sharepoint.com -AppId <App id> -AppSecret <app-secret>
Set-PnPTenantSite -Url https://tenant.sharepoint.com/sites/test -NoScriptSite:$false

Reference - Set-PnPTenantSite

3
  • Thank you for Sharing this command. I tried this, even though my app id has tenant level full control I am getting (401) Unauthorized error. Do you have any idea on this?
    – Prakash
    Feb 28, 2019 at 8:41
  • can you try connecting to admin site with Connect-PnPOnline -Url https://tenant-admin.sharepoint.com -AppId <App id> -AppSecret <app-secret> and then run the Set-PnPTenantSite and check ? Also, can you also check with user context as well after that ? Feb 28, 2019 at 8:45
  • 1
    Thank you @GautamSheth. I have connected to admin site and updated the other site, now without any error I am able to update the site.
    – Prakash
    Feb 28, 2019 at 8:59
0

I am doing the same but it is throwing me an error "System.UnauthorizedAccessException : Attempted to perform an unauthorized operation.". What kind of graph api permission required in the App to use these commands? Here is the code what I am using in PnP C#. It is throwing error when using "tenant.Context.ExecuteQuery();".

using (ClientContext clientContext = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(Helper.requestAdminUrl, Constants.AppClientId, Constants.AppClientSecrete))
                {
                    var tenant = new Microsoft.Online.SharePoint.TenantAdministration.Tenant(clientContext);

                    foreach (string s in siteCollectionUrl)
                    {
                        Helper.WriteException("SetUpDenyAddAndCustomizePages, Applying on URL " + s);
                        var siteProperties = tenant.GetSitePropertiesByUrl(s, true);
                        tenant.Context.Load(siteProperties);
                        tenant.Context.ExecuteQuery();

                        siteProperties.DenyAddAndCustomizePages = Microsoft.Online.SharePoint.TenantAdministration.DenyAddAndCustomizePagesStatus.Disabled;
                        var operation = siteProperties.Update();
                        tenant.Context.Load(operation, op => op.IsComplete, op => op.PollingInterval);
                        tenant.Context.ExecuteQuery();

                        // this is necessary, because the setting is not immediately reflected after ExecuteQuery
                        while (!operation.IsComplete)
                        {
                            Thread.Sleep(operation.PollingInterval);
                            operation.RefreshLoad();
                            if (!operation.IsComplete)
                            {
                                try
                                {
                                    tenant.Context.ExecuteQuery();
                                }
                                catch (System.Net.WebException webEx)
                                {
                                    Helper.WriteException("Error when applying SetUpDenyAddAndCustomizePages : " + webEx.Message, webEx);
                                }
                            }
                        }

                        Helper.WriteException("SetUpDenyAddAndCustomizePages, Applied on URL " + s);
                    }

                }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.