PtiTom

CSOM Create SubSite

Aug 23rd, 2016
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #################################################
  2. # VARIABLES A MODIFIER
  3. #################################################
  4. # Script variables
  5. $targetWebUrl = "https://XXX.sharepoint.com/sites/templates"
  6. $login = "XXX@XXX.fr"
  7. $WebTitle = "My Project 8"
  8. $WebRelativeUrl = "Prj8"
  9.  
  10.     #Web Template : {8B875C6B-137E-4BD3-A12A-83B2E0E58912}#TemplateDocumentsV1_0
  11.     #Web Template : {DB2762F6-FE0D-4AE5-801D-B03F4705A0F8}#TemplateProjetV1_3
  12. $WebTemplate = "{DB2762F6-FE0D-4AE5-801D-B03F4705A0F8}#TemplateProjetV1_3"
  13.  
  14. #################################################
  15. # FIN DES VARIABLES A MODIFIER
  16. #################################################
  17.  
  18.  
  19. cls
  20. $scriptFolder = "C:\Users\Thomas\Documents\00_Sources\SharePoint Scripts"
  21. Set-Location $scriptFolder
  22.  
  23. Import-Module "../Microsoft.SharePoint.Client.dll"
  24. Import-Module "../Microsoft.SharePoint.Client.Runtime.dll"
  25.  
  26.  
  27. # SharePoint online credentials
  28. $SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString
  29. $myCred = New-Object "Microsoft.SharePoint.Client.SharePointOnlineCredentials" -ArgumentList @($login, $SecurePassword)
  30.  
  31. $csomWebContext = New-Object "Microsoft.SharePoint.Client.ClientContext" -ArgumentList @($targetWebUrl)
  32. $csomWebContext.Credentials = $myCred;
  33. $ctx = $null
  34.  
  35. ####################################################################
  36. #
  37. # FUNCTIONS
  38. #
  39. #####################################################################
  40. function CreateWeb($Title, $RelativeUrl, $Description, $WebTemplate)
  41. {
  42.     $ctx.Load($ctx.Web.Webs)
  43.     $ctx.ExecuteQuery()
  44.  
  45.     $createWeb =  New-Object Microsoft.SharePoint.Client.WebCreationInformation
  46.     $createWeb.Url = $RelativeUrl
  47.     $createWeb.Description = $Description
  48.     $createWeb.UseSamePermissionsAsParentSite = $false
  49.     $createWeb.Title = $Title
  50.     $createWeb.WebTemplate = $WebTemplate
  51.     $createWeb.Language = 1036
  52.  
  53.     $AddWeb = $ctx.Web.Webs.Add($createWeb)
  54.  
  55.     $ctx.Load($AddWeb)
  56.  
  57.     $ctx.ExecuteQuery()
  58.  
  59.     # Affectation des droits
  60.     $roleAssignments = $AddWeb.RoleAssignments
  61.     $ctx.Load($roleAssignments)
  62.     $siteUsersWithTitle = $AddWeb.SiteUsers
  63.     $ctx.Load($siteUsersWithTitle)
  64.     $ctx.ExecuteQuery()
  65.  
  66.     $readerRole = $AddWeb.RoleDefinitions.GetByName("Lecture")
  67.     $contribRole = $AddWeb.RoleDefinitions.GetByName("Modification")
  68.     $adminRole = $AddWeb.RoleDefinitions.GetByName("Contrôle total")
  69.  
  70.     # Create role assignments
  71.     $readerRoleAssignment = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)
  72.     $readerRoleAssignment.Add($readerRole)
  73.     $contribRoleAssignment = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)
  74.     $contribRoleAssignment.Add($contribRole)
  75.     $adminRoleAssignment = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)
  76.     $adminRoleAssignment.Add($adminRole)
  77.  
  78.     # Créer les groupes
  79.     $groupLecteursCI = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
  80.     $groupLecteursCI.Title = "Lecteurs $Title"
  81.     $groupLecteurs = $AddWeb.SiteGroups.Add($groupLecteursCI)
  82.     $groupMembresCI = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
  83.     $groupMembresCI.Title = "Membres $Title"
  84.     $groupMembres = $AddWeb.SiteGroups.Add($groupMembresCI)
  85.     $groupAdminCI = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
  86.     $groupAdminCI.Title = "Admins $Title"
  87.     $groupAdmin = $AddWeb.SiteGroups.Add($groupAdminCI)
  88.  
  89.  
  90.     # Apply the  permission roles to the list.
  91.     $ctx.Load($AddWeb.RoleAssignments.Add($groupLecteurs, $readerRoleAssignment))
  92.     $ctx.Load($AddWeb.RoleAssignments.Add($groupMembres, $contribRoleAssignment))
  93.     $ctx.Load($AddWeb.RoleAssignments.Add($groupAdmin, $adminRoleAssignment))
  94.     $AddWeb.Update()
  95.     $ctx.ExecuteQuery()
  96.  
  97.     $AddWeb
  98. }
  99.  
  100. ####################################################################
  101. #
  102. # MAIN FLOW
  103. #
  104. #####################################################################
  105. $web = $csomWebContext.Web
  106. $csomWebContext.Load($web, $web.Title)
  107.  
  108. #$cTypes = $csomWebContext.Site.GetWebTemplates(1036, 0)
  109. #$csomWebContext.Load($cTypes)
  110.  
  111. $csomWebContext.Load($web.Webs)
  112. $csomWebContext.ExecuteQuery()
  113.  
  114. $web.Title
  115.  
  116. #foreach ($c in $cTypes)
  117. #{
  118. #    $c.Name
  119. #    $c.Title
  120. #}
  121.  
  122. "Create $WebTitle"
  123.  
  124. $ctx = $csomWebContext
  125. # Création du Web :
  126. $rootWeb = CreateWeb $WebTitle $WebRelativeUrl "Generated Web" $WebTemplate
  127.  
  128. $csomWebContext.Dispose();
  129.  
  130. "END"
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×