Bienvenue à Blogs CodeS-SourceS Identification | Inscription | Aide

Blog Technique de Romelard Fabrice

Les dernières Actualités de Romelard Fabrice (Alias fabrice69 ou F___) principalement autour des technologies Microsoft

Actualités

  • Toutes les actualités et informations sur les technologies Microsoft principalement autour de .NET et SQL Server

Archives

Office 365: Script PowerShell de création de sous-sites basés sur CSOM ou PnP

Au sein d’Office 365, l’option pour SharePoint "Save as Site template" n’est plus du tout disponible.

En effet, cette stratégie était pourtant largement utilisée par les utilisateurs avancés leur permettant de créer un modèle de site vierge qui pouvait ensuite être répliqué tel un moule à gateaux.

Dans SharePoint Online, cette option est désormais supprimée et il est évident maintenant que Microsoft invite ses utilisateurs à ne plus créer de sous-sites mais bien rester le plus “flat” possible, la hiérarchie est largement déconseillée (il faut voir la logique des HubSites pour s’en convaincre).

La raison principale justifiant ce choix est que SharePoint Online est une branche d’un arbre complexe proposé avec les Office 365 Groups (Planner, Teams, Stream, …).

Le fait est que dans de nombreux cas d’usage, les modèles sont pourtant nécessaires pour industrialiser certaines activités demandant des duplications répétées (projets, analyses, audits, …).

Donc la seule solution applicable dans ce besoin d’industrialisation est de créer son script PowerShell.

Dans cet article, vous trouverez 2 versions distinctes pour mon besoin, qui devra simplement être adapté au votre:

  • Le premier script est basé uniquement sur CSOM
  • Le second script est basé sur PnP

Script PowerShell utilisant CSOM

Pour développer ce script, j’ai utilisé de nombreuses ressources disponibles:

Il ne vous reste plus qu’à l’adapter:


# VALUE TO ADAPT !!!!!
# --------------------------------------------------------------------------------------------
$SiteIntegrationUrl ="https://tenant.sharepoint.com/sites/SiteCollection/Subsite1"
$SubSiteURL = "testsubsite"
$SubSiteName ="_TESTSUBSITE"
$EmailAddressAccessRequest = "emailaddresstosupport@domain.com"
$RootSiteOwnerGroup = "Root Site Collection Owners"
# --------------------------------------------------------------------------------------------


function Load-DLLandAssemblies
{
    [string]$defaultDLLPath = ""

    # Load assemblies to PowerShell session

    $defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
    [System.Reflection.Assembly]::LoadFile($defaultDLLPath)

    $defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
    [System.Reflection.Assembly]::LoadFile($defaultDLLPath)

    $defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
    [System.Reflection.Assembly]::LoadFile($defaultDLLPath)
}


#Custom Function to Check if Site Collection Exists in Given URL
Function Check-SiteExists($MySiteURL, $MyCredentials)
{
    #Setup context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($MySiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($MyCredentials.UserName,$MyCredentials.Password)
    $Web = $Ctx.Web
    $Ctx.Load($web)
    
    Try {
            $Ctx.ExecuteQuery()
            Return $True
         }
    Catch [Exception] {
      Write-host " >>>> ERROR MESSAGE:", $_.Exception.Message -f Red
      Return $False
     }       
}


Function Create-SPWebList([string]$MyShortTitle, [string]$MyDescription, [int]$MyListTemplateType, [Microsoft.SharePoint.Client.ClientContext]$Mycontext)
{
    Write-Host " ---------------------------------------------------------"

    $lci = New-Object Microsoft.SharePoint.Client.ListCreationInformation
    $lci.Title = $MyShortTitle
    $lci.Description = $MyDescription
    $lci.TemplateType = $MyListTemplateType   #see: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx
    $list = $Mycontext.web.lists.add($lci)
    $Mycontext.load($list)
    #send the request containing all operations to the server
    try{
        $Mycontext.executeQuery()
         write-host "    >>> info: List Created $($MyShortTitle)" -foregroundcolor green
    }
    catch{
        write-host "  >>> List Creation Error info: $($_.Exception.Message)" -foregroundcolor red
    }

    Write-Host " ---------------------------------------------------------"
}


# ---------------------------------------------------------------------------------------------------------------


Load-DLLandAssemblies

#get and save your O365 credentials
[string]$username = "AdminSiteCollection@tenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\SECUREDPWD\ExportedPWD-$($username).txt"
$secureStringPwd = ConvertTo-SecureString -string (Get-Content $PwdTXTPath)
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd

$DocLibTitleLong = "Given Documents"

$DocLibTitle = -join($SubSiteURL, "GivenDocuments")
$DocLibTitleLong = -join($SubSiteName, " Given Documents")
$SubSiteTitle = -join($SubSiteName, " - Subsite Usage")

$SubSiteOwnerOwners = -join($SubSiteName, " Subsite Usage Web Owners")
$SubSiteOwnerMembers = -join($SubSiteName, " Subsite Usage Web Members")
$SubSiteOwnerVisitors = -join($SubSiteName, " Subsite Usage Web Visitors")

$SubSiteFullUrl = -join($SiteIntegrationUrl, "/", $SubSiteURL)

# ---------------------------------------------------------------------------------------------------------------
#connect to the web site using the stored credentials
Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
Write-host " ---- CONNECT THE SITE --- " -ForegroundColor green
Write-host "   CONNECTED SITE:", $SiteIntegrationUrl  -ForegroundColor Yellow

$Myctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteIntegrationUrl)

$Myctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName,$cred.Password)
$Myctx.RequestTimeout = 1000000 # milliseconds
$MyspoRootweb = $Myctx.Web
$Myctx.Load($MyspoRootweb)
$Myctx.ExecuteQuery()

Write-Host " "
Write-Host " ---------------------------------------------------------"
Write-Host "  >>>> # Server Version:" $Myctx.ServerVersion " # <<<<<<" -ForegroundColor Green
Write-Host " ---------------------------------------------------------"
Write-Host " "

Write-host " -------------------------------------------------------- "
Write-host "   -->> RootSite:", $MyspoRootweb.URL -ForegroundColor green

Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
Write-host " ---- START THE SUBSITE CREATION  --- " -ForegroundColor green
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green


$SiteExists = Check-SiteExists $SubSiteFullUrl $cred

if($SiteExists -ne $true)
{

    Write-host "   NEW SITE URL TO CREATE:", $SubSiteFullUrl  -ForegroundColor Yellow
    $Subsite = New-Object Microsoft.SharePoint.Client.WebCreationInformation
    $Subsite.WebTemplate = "STS#0"
    $Subsite.Title = $SubSiteTitle
    $Subsite.Url = $SubSiteURL
    $Subsite.Language = "1033"
    $Subsite.Description = $SubSiteTitle

    $SubWeb = $Myctx.Web.Webs.Add($Subsite)
     $Myctx.Load($SubWeb)
    Try{
        $Myctx.ExecuteQuery()
         Write-host " "
        Write-host "     >> Success info creating site for $SubSiteURL" -ForegroundColor green
    }
    Catch{
        Write-host "     >> Error info creating subsite for $SubSiteURL - $_.Exception.Message" -ForegroundColor red
    }

    $context = New-Object Microsoft.SharePoint.Client.ClientContext($SubSiteFullUrl)
    $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName,$cred.Password)
    $web = $context.web
    $context.load($web)
   
    Write-host " "
    Write-host "   Implement the Top menu navigation" -ForegroundColor green
    $web.Navigation.UseShared = $true
    $context.ExecuteQuery();
   
    Write-host " "
    Write-host "   Implement the Access Request settings: ", $EmailAddressAccessRequest -ForegroundColor green
    $Web.RequestAccessEmail = $EmailAddressAccessRequest
    $web.breakroleinheritance($false, $false)
    $web.update()
    $context.ExecuteQuery();

    $AllSiteCollectionGroups = $web.SiteGroups
    $context.Load($AllSiteCollectionGroups)
    $context.executeQuery()
    
    $existingRootSiteOwnerGroup = $AllSiteCollectionGroups.getByName($RootSiteOwnerGroup)
    Write-host " "
    Write-host "   Default Root site group", $existingRootSiteOwnerGroup -ForegroundColor green

    Write-host " "
    $PermissionLevel = $context.Web.RoleDefinitions.GetByName("Full Control")

    #Bind Permission Level to Group
    $RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)
    $RoleDefBind.Add($PermissionLevel)
    $Assignments = $context.Web.RoleAssignments
    $RoleAssignOneNote = $Assignments.Add($existingRootSiteOwnerGroup,$RoleDefBind)
    $context.Load($existingRootSiteOwnerGroup)
    #send the request containing all operations to the server
    try{
        $context.executeQuery()
         Write-host " "
        write-host "     >> Success info: Added Root site group with Full Control" -foregroundcolor green
    }
    catch{
        Write-host " "
        write-host "     >> Error info: $($_.Exception.Message)" -foregroundcolor red
    }

   
    #Create new groups
    $MysiteGroups = $SubSiteGroupOwners, $SubSiteGroupMembers, $SubSiteGroupVisitors
    foreach ($siteGroup in $MysiteGroups){
        if ($siteGroup -like "*Web Visitors")
        {
            Write-host " "
            Write-host "   Creation of the Visitors group", $SubSiteGroupVisitors -ForegroundColor green
            $gci = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
            $gci.Title = $siteGroup
            $gci.Description = $siteGroup
             $siteGroup = $context.Web.SiteGroups.Add($gci)

            $PermissionLevel = $context.Web.RoleDefinitions.GetByName("Read")

            #Bind Permission Level to Group
            $RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)
            $RoleDefBind.Add($PermissionLevel)
            $Assignments = $context.Web.RoleAssignments
            $RoleAssignOneNote = $Assignments.Add($siteGroup,$RoleDefBind)
            $context.Load($siteGroup)
            $context.Web.Update()
            #send the request containing all operations to the server
            try{
                $context.executeQuery()
                write-host "     >> Success info: Added visitors group" -foregroundcolor green
            }
             catch{
                write-host "     >> Error info: $($_.Exception.Message)" -foregroundcolor red
            }
         }
        
        if ($siteGroup -like "*Web Members")
         {
            Write-host " "
            Write-host "   Creation of the Members group", $SubSiteGroupMembers -ForegroundColor green
            $gci = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
            $gci.Title = $siteGroup
            $gci.Description = $siteGroup
            $siteGroup = $context.Web.SiteGroups.Add($gci)

            $PermissionLevel = $context.Web.RoleDefinitions.GetByName("Edit")
             
            #Bind Permission Level to Group
            $RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)
            $RoleDefBind.Add($PermissionLevel)
            $Assignments = $context.Web.RoleAssignments
            $RoleAssignOneNote = $Assignments.Add($siteGroup,$RoleDefBind)
            $context.Load($siteGroup)
            $context.Web.Update()
            #send the request containing all operations to the server
            try{
                $context.executeQuery()
                write-host "     >> Success info: Added members group" -foregroundcolor green
            }
            catch{
                write-host "     >> Error info: $($_.Exception.Message)" -foregroundcolor red
            }
         }
        
        if ($siteGroup -like "*Web Owners")
         {
            Write-host " "
            Write-host "   Creation of the Owners group", $SubSiteGroupOwners -ForegroundColor green
            $gci = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
            $gci.Title = $siteGroup
            $gci.Description = $siteGroup
            $siteGroup = $context.Web.SiteGroups.Add($gci)

            $PermissionLevel = $context.Web.RoleDefinitions.GetByName("Full Control")
             
            #Bind Permission Level to Group
            $RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)
            $RoleDefBind.Add($PermissionLevel)
            $Assignments = $context.Web.RoleAssignments
            $RoleAssignOneNote = $Assignments.Add($siteGroup,$RoleDefBind)
            $context.Load($siteGroup)
             $context.Web.Update()
            #send the request containing all operations to the server
            try{
                 $context.executeQuery()
                write-host "     >> Success info: Added owners group" -foregroundcolor green
             }
            catch{
                write-host "     >> Error info: $($_.Exception.Message)" -foregroundcolor red
             }
        }
    }

    Write-host " "
    Write-host "   Get the Site groups" -ForegroundColor green

    $context.Load($AllSiteCollectionGroups)
    $context.executeQuery()


    Write-host " "
    Write-host "   Update the Groups owners to:",  $existingRootSiteOwnerGroup.Id -ForegroundColor green
    $MyOwnersGroup = $AllSiteCollectionGroups.GetByName($SubSiteGroupOwners)
    $MyMembersGroup = $AllSiteCollectionGroups.GetByName($SubSiteGroupMembers)
    $MyVisitorsGroup = $AllSiteCollectionGroups.GetByName($SubSiteGroupVisitors)
   
    $MyOwnersGroup.Owner = $existingRootSiteOwnerGroup
    $MyOwnersGroup.Update()
    $MyMembersGroup.Owner = $existingRootSiteOwnerGroup
    $MyMembersGroup.Update()
    $MyVisitorsGroup.Owner = $existingRootSiteOwnerGroup
     $MyVisitorsGroup.Update()
    $context.executeQuery()

    Write-host " "
    Write-host "   >>>>> in Case of error, you have to go on the default group page: $($SubSiteFullUrl)/_layouts/15/permsetup.aspx    " -ForegroundColor magenta  # : /_layouts/15/permsetup.aspx   
   
    Write-host " "
    Write-host "   Define the Default SPWeb groups" -ForegroundColor green

    $context.Load($web)
    $context.executeQuery()

    Write-host "  Check the values !!!"
    Write-host "      >>> AssociatedVisitorGroup:", $web.AssociatedVisitorGroup.Id -ForegroundColor Magenta
    Write-host "      >>> AssociatedMemberGroup:", $web.AssociatedMemberGroup.Id -ForegroundColor Magenta
    Write-host "      >>> AssociatedMemberGroup:", $web.AssociatedOwnerGroup.Id -ForegroundColor Magenta

    Write-host " "
    Write-host "   Define the Visitors group:", $MyVisitorsGroup.Id -ForegroundColor green
    $web.AssociatedVisitorGroup = $MyVisitorsGroup
    $web.AssociatedVisitorGroup.Update()

    Write-host " "
    Write-host "   Define the Members group:", $MyMembersGroup.Id -ForegroundColor green
    $web.AssociatedMemberGroup = $MyMembersGroup
    $web.AssociatedMemberGroup.Update()

    Write-host " "
    Write-host "   Define the Owners group:", $MyOwnersGroup.Id -ForegroundColor green
    $web.AssociatedOwnerGroup = $MyOwnersGroup
    $web.AssociatedOwnerGroup.Update()

    $web.Update()
    $context.executeQuery()


    #Create the Doc Lib
    Write-host " "
    Write-host "   Create Doc Lib:", $DocLibTitle -ForegroundColor green
    Create-SPWebList  $DocLibTitle $DocLibTitleLong 101 $context

    $MyList = $context.Web.Lists.GetByTitle($DocLibTitle)
    $context.Load($MyList)
    $context.executeQuery()   
    $MyList.Title = $DocLibTitleLong
    $MyList.OnQuickLaunch = $True
     $MyList.Update()
    $context.executeQuery()   
   
    #Rename Shared Document library
    Write-host " "
    Write-host "   Modification of the Shared Document lib" -ForegroundColor green
    $MyList = $context.Web.Lists.GetByTitle("Documents")
    $context.Load($MyList)
    $context.executeQuery()   
    $MyList.Title = "Other Documents"
    $MyList.OnQuickLaunch = $True
     $MyList.Update()
    $context.executeQuery()   
   
    #Create Contact List
    Write-host " "
    Write-host "   Creation of Contacts list" -ForegroundColor green
    Create-SPWebList  "SubsiteContacts" "Subsite Contacts" 105 $context

    $MyList = $context.Web.Lists.GetByTitle("SubsiteContacts")
    $context.Load($MyList)
    $context.executeQuery()   
    $MyList.Title = "Subsite Contacts"
    $MyList.EnableAttachments = $false
    $MyList.EnableFolderCreation = $false
    $MyList.OnQuickLaunch = $True
    $MyList.Update()
    $context.executeQuery()   
   
    #Create Links List
    Write-host " "
    Write-host "   Creation of Links list" -ForegroundColor green

    Create-SPWebList  "SubsiteLinks" "Subsite Links" 103 $context
    $MyList = $context.Web.Lists.GetByTitle("SubsiteLinks")
    $context.Load($MyList)
    $context.executeQuery()   
    $MyList.Title = "Subsite Links"
    $MyList.EnableAttachments = $false
    $MyList.EnableFolderCreation = $false
    $MyList.OnQuickLaunch = $false
     $MyList.Update()
    $context.executeQuery()   
   
    #Create Calendar List
    Write-host " "
    Write-host "   Creation of Calendar list" -ForegroundColor green

    Create-SPWebList  "SubsiteCalendar" "Subsite Calendar" 106 $context
    $MyList = $context.Web.Lists.GetByTitle("SubsiteCalendar")
    $context.Load($MyList)
    $context.executeQuery()   
    $MyList.Title = "Subsite Calendar"
    $MyList.EnableAttachments = $false
    $MyList.EnableFolderCreation = $false
    $MyList.OnQuickLaunch = $True
    $MyList.Update()
    $context.executeQuery()   

    Write-host " "
    Write-host "   Start the Navigation Cleanup" -ForegroundColor green
   
    $QuickLaunchNodes = $web.Navigation.QuickLaunch
    $context.Load($QuickLaunchNodes)
    #send the request containing all operations to the server
    $context.executeQuery()
    for($i=$QuickLaunchNodes.Count-1;$i -ge 0; $i--)
    {
        write-host "     >>> MenuItem $($QuickLaunchNodes[$i].Title) - ID: $($QuickLaunchNodes[$i].Id)"  -foregroundcolor green
        #write-host "          >>> MenuItem Children $($QuickLaunchNodes[$i].Children) "  -foregroundcolor green

        switch ($QuickLaunchNodes[$i].Title)
        {
            "Home" {
                    $QuickLaunchNodes[$i].DeleteObject()
                    $context.executeQuery()
                    write-host "           >>> MenuItem $($QuickLaunchNodes[$i].Title) Link Found !!!!" -foregroundcolor red
            }
             "Recent" {
                    $QuickLaunchNodes[$i].DeleteObject()
                    $context.executeQuery()
                     write-host "           >>> MenuItem $($QuickLaunchNodes[$i].Title) Link Found !!!!" -foregroundcolor red
            }
             "Pages" {
                    $QuickLaunchNodes[$i].DeleteObject()
                    $context.executeQuery()
                     write-host "           >>> MenuItem $($QuickLaunchNodes[$i].Title) Link Found !!!!" -foregroundcolor red
             }
            "Site contents" {
                    $QuickLaunchNodes[$i].DeleteObject()
                    $context.executeQuery()
                    write-host "           >>> MenuItem $($QuickLaunchNodes[$i].Title) Link Found !!!!" -foregroundcolor red
            }
        }
    }

    Write-host "   Add the new Home Link into the Navigation Menu", $SubSiteFullUrl -ForegroundColor green

    $NavigationNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
    $NavigationNode.Title = "Subsite Home"
    $NavigationNode.Url = $SubSiteFullUrl
    #$NavigationNode.AsLastNode = $true         
    $context.Load($QuickLaunchNodes.Add($NavigationNode))
    $context.executeQuery()   

    Write-host "   Add the Link into the Subsite Navigation Menu", $SiteIntegrationUrl -ForegroundColor green
    $QuickLaunchNodes = $Myctx.web.Navigation.QuickLaunch
    $Myctx.Load($QuickLaunchNodes)
    $Myctx.executeQuery()   

    $NavigationNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
    $NavigationNode.Title = $SubSiteName
     $NavigationNode.Url = $SubSiteFullUrl
    $NavigationNode.AsLastNode = $true         
    $Myctx.Load($QuickLaunchNodes.Add($NavigationNode))
    $Myctx.executeQuery()   
   
}
else
{
     Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
    Write-host " ====>> THE SUBSITE IS YET CREATED:", $SubSiteFullUrl -ForegroundColor red

}


Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green


Script PowerShell basé sur PnP

Les sources utilisées pour celui-ci sont:

A adapter selon vos besoins

if (-not (Get-Module -ListAvailable -Name SharePointPnPPowerShellOnline))
{
    Install-Module SharePointPnPPowerShellOnline
}
Import-Module SharePointPnPPowerShellOnline


# VALUE TO ADAPT !!!!!
# --------------------------------------------------------------------------------------------
$SiteIntegrationUrl ="https://tenant.sharepoint.com/sites/SiteCollection/Subsite1"
$SubSiteURL = "testsubsite"
$SubSiteName ="_TESTSUBSITE"
$EmailAddressAccessRequest = "emailaddresstosupport@domain.com"
$RootSiteOwnerGroup = "Root Site Collection Owners"
# --------------------------------------------------------------------------------------------

#Custom Function to Check if Site Collection Exists in Given URL
Function Check-SiteExists($MySiteURL, $MyCredentials)
{
    Try {
            Connect-PnPOnline -Url $MySiteURL -Credentials $MyCredentials
            Return $True
        }
    Catch [Exception] {
      Write-host " >>>> ERROR MESSAGE:", $_.Exception.Message -f Red
      Return $False
     }       
}

# ---------------------------------------------------------------------------------------------------------------


#get and save your O365 credentials
[string]$username = "AdminSiteCollection@tenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\SECUREDPWD\ExportedPWD-$($username).txt"
$secureStringPwd = ConvertTo-SecureString -string (Get-Content $PwdTXTPath)
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd

$DocLibTitleLong = "Given Documents"

$DocLibTitle = -join($SubSiteURL, "GivenDocuments")
$DocLibTitleLong = -join($SubSiteName, " Given Documents")
$SubSiteTitle = -join($SubSiteName, " - Subsite Usage")

$SubSiteOwnerOwners = -join($SubSiteName, " Subsite Usage Web Owners")
$SubSiteOwnerMembers = -join($SubSiteName, " Subsite Usage Web Members")
$SubSiteOwnerVisitors = -join($SubSiteName, " Subsite Usage Web Visitors")

$siteUrl = -join($SiteIntegrationUrl, "/", $SubSiteURL)

# ---------------------------------------------------------------------------------------------------------------
#connect to the web site using the stored credentials
Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
Write-host " ---- CONNECT THE SITE --- " -ForegroundColor green
Write-host "   CONNECTED SITE:", $SiteIntegrationUrl  -ForegroundColor Yellow
Connect-PnPOnline -Url $SiteIntegrationUrl -Credential $cred

Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
Write-host " ---- START THE SUBSITE CREATION  :", $siteUrl -ForegroundColor green
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green


$SiteExists = Check-SiteExists $siteUrl $cred
if($SiteExists -ne $true)
{
    Write-host "   NEW SITE URL TO CREATE:", $siteUrl  -ForegroundColor Yellow
    #Create the subWeb for the Integration site
    New-PnPWeb -Template "STS#0" -Title $SubSiteTitle -Description $SubSiteTitle -Url $SubSiteURL -Locale "1033" -InheritNavigation -BreakInheritance
    Add-PnPNavigationNode -Title $SubSiteTitle -Url $siteUrl -Location "QuickLaunch"

    #connect to the new web site using the stored credentials
     Write-host " "
    Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
    Write-host " ---- CONNECT THE SITE --- " -ForegroundColor green
    Write-host "   CONNECTED SITE:", $siteUrl  -ForegroundColor green
    Connect-PnPOnline -Url $siteUrl -Credentials $cred
    $MyCurrentWeb = Get-PnPWeb

    #configure the Access request system
    Set-PnPRequestAccessEmails -Emails $EmailAddressAccessRequest
   
    #Create the Doc Lib
    Write-host " "
    Write-host "   Create Doc Lib:", $DocLibTitle -ForegroundColor green
    New-PnPList -Title $DocLibTitle -Template DocumentLibrary -OnQuickLaunch
    Set-PnPList -Identity $DocLibTitle -Title $DocLibTitleLong -EnableVersioning $true -MajorVersions 100 -EnableMinorVersions $false

    #Rename Shared Document library
    Write-host " "
    Write-host "   Modification of the Shared Document lib" -ForegroundColor green
    Set-PnPList -Identity "Documents" -Title "Other Documents" -EnableVersioning $true -MajorVersions 100 -EnableMinorVersions $false

    #Create Contact List
    Write-host " "
    Write-host "   Creation of Contacts list" -ForegroundColor green
    New-PnPList -Title "SubsiteContacts" -Template Contacts -OnQuickLaunch
    Set-PnPList -Identity "SubsiteContacts" -Title "Subsite Contacts"

    #Create Links List
    Write-host " "
    Write-host "   Creation of Links list" -ForegroundColor green
    New-PnPList -Title "SubsiteLinks" -Template Links
    Set-PnPList -Identity "SubsiteLinks" -Title "Subsite Links"

    #Create Calendar List
    Write-host " "
    Write-host "   Creation of Calendar list" -ForegroundColor green
    New-PnPList -Title "SubsiteCalendar" -Template "Events" -OnQuickLaunch
    Set-PnPList -Identity "SubsiteCalendar" -Title "Subsite Calendar"

    #Change the Navifation menu
    Write-host " "
    Write-host "   Modification of the Navigation Menu" -ForegroundColor green
    Remove-PnPNavigationNode -Title "Pages" -Location QuickLaunch -Force
    Remove-PnPNavigationNode -Title "Recent" -Location QuickLaunch -Force
    Remove-PnPNavigationNode -Title "Site contents" -Location QuickLaunch -Force
    Remove-PnPNavigationNode -Title "Home" -Location QuickLaunch -Force
    Add-PnPNavigationNode -Title "Subsite Home" -Url $siteUrl -Location "QuickLaunch" -First

    #Configure the permission set by default
    Write-host " "
    Write-host "   Get the OwnerGroup from the root site", $RootSiteOwnerGroup -ForegroundColor green
    $owner = Get-PnPGroup -Identity $RootSiteOwnerGroup  #(Get-PnPContext).Credentials.UserName
     Write-host "    >>> Group ID:", $owner.ID -ForegroundColor green
    Set-PnPWebPermission -Identity $MyCurrentWeb.Id -Group $RootSiteOwnerGroup -AddRole "Full Control" 
   
    #Create default groups for the new web
    #here, by default owner will be the person provisioning the groups
    Write-host " "
    Write-host "   Creation of the Owners group", $SubSiteOwnerOwners -ForegroundColor green
    $ownerGroup = New-PnPGroup -Title $SubSiteOwnerOwners -Owner $RootSiteOwnerGroup
    Set-PnPGroup -Identity $SubSiteOwnerOwners -SetAssociatedGroup Owners -AddRole "Full Control" 

    Write-host " "
    Write-host "   Creation of the Members group", $SubSiteOwnerOwners -ForegroundColor green
    $memberGroup = New-PnPGroup -Title $SubSiteOwnerMembers -Owner $RootSiteOwnerGroup
    Set-PnPGroup -Identity $SubSiteOwnerMembers -SetAssociatedGroup Members -AddRole "Edit"

    Write-host " "
    Write-host "   Creation of the Visitors group", $SubSiteOwnerOwners -ForegroundColor green
    $visitorGroup = New-PnPGroup -Title $SubSiteOwnerVisitors -Owner $RootSiteOwnerGroup
    Set-PnPGroup -Identity $SubSiteOwnerVisitors -SetAssociatedGroup Visitors -AddRole "Read"

}
else
{
    Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
    Write-host " ====>> THE SUBSITE IS YET CREATED:", $siteUrl -ForegroundColor red

}

Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
Write-host " ---- DISCONNECT THE SITE --- " -ForegroundColor green
# Quit the script and disconnect from the site
Disconnect-PnPOnline

Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green


Comme vous pouvez le constater, le script PnP est bien plus court, puisque cette solution sert d’interface à CSOM. En revanche il vous demandera plus de travail de maintenance.

Fabrice Romelard [MVP]

Ce post vous a plu ? Ajoutez le dans vos favoris pour ne pas perdre de temps à le retrouver le jour où vous en aurez besoin :
Posted: mardi 12 juin 2018 14:58 par ROMELARD Fabrice
Les commentaires anonymes sont désactivés

Les 10 derniers blogs postés

- SharePoint Online: Script PowerShell pour supprimer une colonne dans tous les sites d’une collection par Blog Technique de Romelard Fabrice le 11-27-2018, 18:01

- Office 365: Script PowerShell pour supprimer des comptes utilisateur de collections de sites SharePoint Online par Blog Technique de Romelard Fabrice le 11-19-2018, 16:47

- Retrouvez-moi aux Microsoft experiences18 ! par Blog de Jérémy Jeanson le 11-06-2018, 22:25

- Office 365: Script PowerShell pour extraire les Teams associés à un utilisateur spécifié par Blog Technique de Romelard Fabrice le 11-06-2018, 13:37

- Office 365: Extraire les pages publiées dans un site SharePoint Online de publishing en CSV par Blog Technique de Romelard Fabrice le 11-06-2018, 11:04

- Office 365: Comment créer un jeu de Pages Modernes dans un Modern Site SharePoint Online en utilisant PowerShell depuis un fichier CSV par Blog Technique de Romelard Fabrice le 10-17-2018, 12:50

- Office 365: Comment Utiliser les Modern Pages dans un Site Intranet SharePoint basé sur le Publishing site par Blog Technique de Romelard Fabrice le 10-16-2018, 15:34

- Office 365: Comment changer le nom “Auteur” dans les pages modernes d'un Modern Site SharePoint Online par Blog Technique de Romelard Fabrice le 10-16-2018, 15:07

- Reprise des articles Codes-Sources de 2011 à 2013 par Blog de Jérémy Jeanson le 08-21-2018, 16:08

- Office 365: Modifier les jeux de couleur dans les Thèmes des pages classiques de SharePoint Online par Blog Technique de Romelard Fabrice le 08-08-2018, 17:27