SOLVED

Adding SharePoint Site Owner in PowerShell

%3CLINGO-SUB%20id%3D%22lingo-sub-100911%22%20slang%3D%22en-US%22%3EAdding%20SharePoint%20Site%20Owner%20in%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-100911%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%2C%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EI%20am%20working%20on%20a%20provisioning%20script%20that%20will%20allow%20me%20to%20create%20Modern%20SharePoint%20Team%20Sites%20and%20apply%20a%20provisioning%20template%20to%20them%20after%2C%20all%20of%20this%20in%20PowerShell%20Cmdlets.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20create%20my%20Modern%20Team%20Site%20by%20creating%20a%20new%20Unified%20Group%20(New-PnPUnifiedGroup%20cmdlet).%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EI%20need%20to%20add%20an%20member%20to%20the%20Owner%20Group%20of%20the%20SharePoint%20site%20but%20I%20can't%20seem%20to%20make%20it%20work.%20The%20only%20way%20I%20have%20been%20able%20to%20achieve%20this%20is%20by%20applying%20a%20provisioning%20template%20that%20adds%20the%20user%20to%20the%20admin%20group.%20The%20problem%20with%20this%20is%20that%20it%20takes%20a%20random%20amount%20of%20time%20between%205%20minutes%20and%205%20hours%20before%20I%20can%20apply%20the%20template%20to%20my%20newly%20created%20site%20(before%20that%2C%20I%20always%20get%20''Access%20Denied''%20errors).%3CBR%20%2F%3E%3CBR%20%2F%3EI%20have%20tried%26nbsp%3B%24tenant.SetSiteAdmin()%2C%26nbsp%3BSet-SPOUser%20-IsSiteCollectionAdmin%20and%20a%20few%20other%20things%2C%20but%20nothing%20has%20worked%20yet.%3CBR%20%2F%3E%3CBR%20%2F%3EIs%20there%20a%20way%20to%20know%20how%20this%20is%20done%20in%20the%20code%20of%20the%20Apply-PnPProvisioningTemplate%20cmdlet%3F%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-100911%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-103436%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20SharePoint%20Site%20Owner%20in%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-103436%22%20slang%3D%22en-US%22%3E%3CP%3EUpdate%202%3A%3CBR%20%2F%3EThis%20worked%20for%20me%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24context%20%3D%20Get-PnPContext%0A%24web%20%3D%20Get-PnPWeb%0A%24web.Lists.EnsureSiteAssetsLibrary()%0A%24web.Context.ExecuteQuery()%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-102573%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20SharePoint%20Site%20Owner%20in%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-102573%22%20slang%3D%22en-US%22%3E%3CP%3EUpdate%3A%20The%20Site%20Assets%20folder%20is%20only%20created%20once%20the%20user%20browses%20to%20the%20site%20(in%20the%20UI).%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EWould%20there%20be%20a%20way%20to%20fake%20that%20browsing%20action%3F%20Possibly%20a%20Web%20Request%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-102523%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20SharePoint%20Site%20Owner%20in%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-102523%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F2748%22%20target%3D%22_blank%22%3E%40Drew%20Madelung%3C%2FA%3E%26nbsp%3BThat's%20correct.%20I%20have%20been%20digging%20a%20little%20bit%20more%20and%20I%20believe%20the%20problem%20is%20related%20to%20the%20template%20itself.%20I%20think%20the%20site%20has%20not%20been%20completely%20created%20just%20yet%20when%20I%20am%20applying%20the%20template.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20have%20the%20following%20nodes%3A%3C%2FP%3E%3CPRE%3E%26lt%3Bpnp%3AFiles%26gt%3B%0A%20%20%20%20%26lt%3Bpnp%3AFile%20src%3D%22..%5CSiteAssets%5CTest.txt%22%20Folder%3D%22SiteAssets%22%20%2F%26gt%3B%0A%20%20%20%20%26lt%3Bpnp%3AFile%20src%3D%22..%5CSiteAssets%5Clogo.png%22%20Folder%3D%22SiteAssets%22%20%2F%26gt%3B%0A%26lt%3B%2Fpnp%3AFiles%26gt%3B%3C%2FPRE%3E%3CP%3EAnd%20see%20this%20in%20the%20detailed%20logs%3A%20%22Creating%20folder%20'SiteAssets'%20under%20Web%20'%2Fsites%2FMyModernSite19%2F'%22%3C%2FP%3E%3CPRE%3E2017-08-31%2010%3A31%3A15.0785%20%5BFiles%5D%20%5B13%5D%20%5BDebug%5D%20Code%20execution%20scope%20started%200ms%208614b127-0a77-4b98-942b-c298d9ac31e9%3CBR%20%2F%3E2017-08-31%2010%3A31%3A16.1248%20%5BOfficeDevPnP.Core%5D%20%5B0%5D%20%5BInformation%5D%20Creating%20folder%20'SiteAssets'%20under%20Web%20'%2Fsites%2FMyModernSite19%2F'.%200ms%20%3CBR%20%2F%3E2017-08-31%2010%3A31%3A16.2582%20%5BFiles%5D%20%5B13%5D%20%5BDebug%5D%20Code%20execution%20scope%20ended%201180ms%208614b127-0a77-4b98-942b-c298d9ac31e9%3CBR%20%2F%3E2017-08-31%2010%3A31%3A16.2592%20%5BProvisioning%5D%20%5B13%5D%20%5BDebug%5D%20Code%20execution%20scope%20ended%206993ms%208614b127-0a77-4b98-942b-c298d9ac31e9%3C%2FPRE%3E%3CP%3EI%20believe%20the%20access%20Denied%20error%20means%20that%20I%20cannot%20create%20the%20folder%20SiteAssets%20under%20Web%2C%20not%20that%20I%20can't%20apply%20the%20template.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-102402%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20SharePoint%20Site%20Owner%20in%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-102402%22%20slang%3D%22en-US%22%3ESo%20you%20are%20trying%20to%20add%20an%20account%20to%20the%20owners%20group%20which%20works%20with%20the%20powershell%20above%20then%20you%20are%20trying%20to%20use%20that%20same%20account%20to%20apply%20a%20provisioning%20template%20to%20a%20site%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-102354%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20SharePoint%20Site%20Owner%20in%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-102354%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F2748%22%20target%3D%22_blank%22%3E%40Drew%20Madelung%3C%2FA%3E%2C%3CBR%20%2F%3E%3CBR%20%2F%3EI%20did%20try%20this%20before%20and%20it%20added%20my%20user%20to%20the%20Owner%20group%2C%20but%20it%20still%20says%20''access%20denied''%20when%20I%20try%20to%20apply%20the%20provisioning%20template.%3CBR%20%2F%3E%3CBR%20%2F%3EPerhaps%20it%20could%20be%20that%20the%20access%20token%20has%20not%20been%20refreshed%2Frenewed%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-102243%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20SharePoint%20Site%20Owner%20in%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-102243%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20can%20use%20the%20%22Add-PnPUserToGroup%22%20PnP%20Powershell%20cmdlet%20to%20handle%20this.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fblob%2Fmaster%2FDocumentation%2FAddPnPUserToGroup.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fblob%2Fmaster%2FDocumentation%2FAddPnPUserToGroup.md%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELink%20to%20download%20powershell%20cmdlets%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Freleases%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Freleases%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F19432iDD1C5BF3BF497285%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22pic1.png%22%20title%3D%22pic1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-102149%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20SharePoint%20Site%20Owner%20in%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-102149%22%20slang%3D%22en-US%22%3E%3CP%3EBump%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1386166%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20SharePoint%20Site%20Owner%20in%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1386166%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20also%20worked%20for%20me%20-%20however%20the%20context%20operations%20were%20unnecessary.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

Hi all, 

I am working on a provisioning script that will allow me to create Modern SharePoint Team Sites and apply a provisioning template to them after, all of this in PowerShell Cmdlets.

I create my Modern Team Site by creating a new Unified Group (New-PnPUnifiedGroup cmdlet). 

I need to add an member to the Owner Group of the SharePoint site but I can't seem to make it work. The only way I have been able to achieve this is by applying a provisioning template that adds the user to the admin group. The problem with this is that it takes a random amount of time between 5 minutes and 5 hours before I can apply the template to my newly created site (before that, I always get ''Access Denied'' errors).

I have tried $tenant.SetSiteAdmin(), Set-SPOUser -IsSiteCollectionAdmin and a few other things, but nothing has worked yet.

Is there a way to know how this is done in the code of the Apply-PnPProvisioningTemplate cmdlet? 

8 Replies
Highlighted
Highlighted
Best Response
Solution

You can use the "Add-PnPUserToGroup" PnP Powershell cmdlet to handle this.

https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/AddPnPUserToGroup.md

 

Link to download powershell cmdlets:

https://github.com/SharePoint/PnP-PowerShell/releases

 

pic1.png

Highlighted

Hi @Drew Madelung,

I did try this before and it added my user to the Owner group, but it still says ''access denied'' when I try to apply the provisioning template.

Perhaps it could be that the access token has not been refreshed/renewed?

Highlighted
So you are trying to add an account to the owners group which works with the powershell above then you are trying to use that same account to apply a provisioning template to a site?
Highlighted

@Drew Madelung That's correct. I have been digging a little bit more and I believe the problem is related to the template itself. I think the site has not been completely created just yet when I am applying the template.

I have the following nodes:

<pnp:Files>
    <pnp:File src="..\SiteAssets\Test.txt" Folder="SiteAssets" />
    <pnp:File src="..\SiteAssets\logo.png" Folder="SiteAssets" />
</pnp:Files>

And see this in the detailed logs: "Creating folder 'SiteAssets' under Web '/sites/MyModernSite19/'"

2017-08-31 10:31:15.0785 [Files] [13] [Debug] Code execution scope started 0ms 8614b127-0a77-4b98-942b-c298d9ac31e9
2017-08-31 10:31:16.1248 [OfficeDevPnP.Core] [0] [Information] Creating folder 'SiteAssets' under Web '/sites/MyModernSite19/'. 0ms
2017-08-31 10:31:16.2582 [Files] [13] [Debug] Code execution scope ended 1180ms 8614b127-0a77-4b98-942b-c298d9ac31e9
2017-08-31 10:31:16.2592 [Provisioning] [13] [Debug] Code execution scope ended 6993ms 8614b127-0a77-4b98-942b-c298d9ac31e9

I believe the access Denied error means that I cannot create the folder SiteAssets under Web, not that I can't apply the template.

 

Highlighted

Update: The Site Assets folder is only created once the user browses to the site (in the UI). 

Would there be a way to fake that browsing action? Possibly a Web Request?

Highlighted

Update 2:
This worked for me

 

 

$context = Get-PnPContext
$web = Get-PnPWeb
$web.Lists.EnsureSiteAssetsLibrary()
$web.Context.ExecuteQuery()

 

Highlighted

This also worked for me - however the context operations were unnecessary.