Advertisement
Guest User

Untitled

a guest
Nov 13th, 2017
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
  2.  
  3. Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
  4.  
  5.  
  6. ##Variables for Processing
  7.  
  8. $SiteUrl = "https://defradev.sharepoint.com/sites/EA-SP-LP-DEVWASTE/"
  9. $SiteUrlDest = "https://defradev.sharepoint.com/sites/EA-SP-LP-DEVMASTER/"
  10.  
  11. $UserName ="kaloyan.kurtev@defradev.onmicrosoft.com"
  12. $Password = 'K@l0yan"'
  13.  
  14. $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
  15. $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
  16. $Context.Credentials = $credentials
  17. $Context.Load($Context.Web.AvailableContentTypes)
  18. $Context.ExecuteQuery()
  19.  
  20. $CredentialsDest = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
  21. $ContextDest = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrlDest)
  22. $ContextDest.Credentials = $CredentialsDest
  23.  
  24. $ContextDest.Load($ContextDest.Web.AvailableContentTypes)
  25. #$ContextDest.Load($ContextDest.Web.ContentTypes)
  26. $ContextDest.ExecuteQuery()
  27.  
  28.  
  29. #Get the Content Type Schema from XML
  30. $XMLFile = "./SiteContentTypes.xml"
  31. [xml] $CTypeXML = Get-Content($XMLFile)
  32.  
  33. #Create Site Content Types
  34. #$CTypeXML.ContentTypes.ContentType | ForEach-Object {
  35.  
  36.  
  37. #vars
  38.  
  39.  
  40. ##vars
  41.  
  42.  
  43. foreach($ccSRC in $Context.Web.AvailableContentTypes)
  44. {
  45.  
  46. #$lci =New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
  47. #$lci.Description="Description"
  48. #$lci.Name="Powershell Content Type2"
  49. #$lci.ParentContentType=$ctx.Web.ContentTypes.GetById("0x01")
  50. #$lci.Group="List Content Types"
  51.  
  52. $found = 0
  53.  
  54. foreach($cc in $ContextDest.Web.AvailableContentTypes)
  55. {
  56. if($cc.Name.Equals($ccSRC.Name))
  57. {
  58. $found++
  59. }
  60. }
  61.  
  62. #If not found
  63. if($found -eq 0)
  64. {
  65. write-host "Content type " $_.Name " with ID " $_.Id " not found"
  66.  
  67. #Get the parent
  68. $Context.Load($ccSRC.Parent)
  69. $Context.ExecuteQuery()
  70.  
  71. Write-Host "Parent ID " $ccSRC.Parent.Id
  72.  
  73.  
  74. #Create the content type
  75. #$ContextDest.Web.ContentTypes.AddExistingContentType($ccSRC)
  76. #$ContextDest.ExecuteQuery()
  77.  
  78. #Create it
  79. #Create New Content Type object inheriting from parent
  80. #$SPContentType = New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation # New-Object Microsoft.SharePoint.SPContentType ($_.ID,$TargetWeb.ContentTypes,$_.Name)
  81. $SPContentType = New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
  82.  
  83.  
  84. Write-Host "Type ID " $SPContentType.TypeId
  85.  
  86. #Set Content Type name and id
  87. $SPContentType.Name = $ccSRC.Name
  88. #$SPContentType.Id = $ccSRC.Id.StringValue
  89.  
  90. #Set Content Type description and group
  91. $SPContentType.Description = $ccSRC.Description
  92. $SPContentType.Group = $ccSRC.Group
  93.  
  94.  
  95. $ContextDest.Load($ContextDest.Web.ContentTypes)
  96. $ContextDest.ExecuteQuery()
  97.  
  98. foreach($ccPar in $ContextDest.Web.ContentTypes)
  99. {
  100. if($ccPar.Name.Equals($ccSRC.Parent.Name))
  101. {
  102. $SPContentType.ParentContentType = $ccPar
  103. }
  104. }
  105.  
  106. Write-Host "test " $SPContentType.ParentContentType
  107.  
  108.  
  109. ##The actual creation
  110. $ctCreated = $ContextDest.Web.ContentTypes.Add($SPContentType)
  111. $ContextDest.Load($ctCreated)
  112. $ContextDest.ExecuteQuery()
  113.  
  114.  
  115. $fieldsDEST = $ContextDest.Web.Fields
  116. $fieldsSRC = $Context.Web.Fields
  117. #$contentTypesDEST = New-Object Microsoft.SharePoint.Client.ContentTypeCollection
  118. #$contentTypesDEST = $ContextDest.Web.ContentTypes
  119. $ContextDest.load($fieldsDEST)
  120. $Context.Load($fieldsSRC)
  121. #$ContextDest.load($contentTypesDEST)
  122. $ContextDest.ExecuteQuery()
  123. $Context.ExecuteQuery()
  124.  
  125.  
  126. #$contentTypesDEST.Add($SPContentType)
  127. #$ContextDest.Load($SPContentType)
  128. #$ContextDest.ExecuteQuery()
  129.  
  130.  
  131.  
  132. $newFields = @()
  133.  
  134. foreach($srcField in $fieldsSRC)
  135. {
  136. $fieldExists = 0
  137.  
  138. foreach($dstField in $fieldsDEST)
  139. {
  140. if($dstField.EntityPropertyName.Equals($srcField.EntityPropertyName))
  141. {
  142. $fieldExists++
  143. }
  144.  
  145. }
  146.  
  147. if($fieldExists -eq 0)
  148. {
  149. Write-Host $srcField.InternalName "Does not exist"
  150.  
  151. Write-Host "Creating field " $srcField.InternalName
  152.  
  153. #$fieldAsXML = "<Field Type='Text'
  154. #DisplayName='$srcField.InternalName'
  155. #Name='$srcField.InternalName'
  156. #Group='$srcField.Group'/>"
  157. #$fieldOption = [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint
  158.  
  159. #$field = $fieldsDEST.AddFieldAsXML($srcField.SchemaXml, $true, $fieldOption)
  160. #$ContextDest.load($field)
  161. $newFields += $srcField.SchemaXml
  162. }
  163. }
  164.  
  165. foreach($newField in $newFields)
  166. {
  167. $fieldOption = [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint
  168. $field = $ContextDest.Web.Fields.AddFieldAsXML($newField, $true, $fieldOption)
  169. $ContextDest.load($field)
  170. $ContextDest.ExecuteQuery()
  171.  
  172. #$ContextDest.Load($fieldsDEST);
  173. #$ContextDest.ExecuteQuery()
  174. }
  175.  
  176. #doesnt work
  177. #$newContentType = $ContextDest.Web.ContentTypes.GetById($SPContentType.TypeId)
  178.  
  179. $newContentType = New-Object Microsoft.SharePoint.Client.ContentType
  180. foreach($updatedCT in $ContextDest.Web.ContentTypes)
  181. {
  182. if($updatedCT.Name -eq $SPContentType.Name)
  183. {
  184. $newContentType = $updatedCT
  185. }
  186. }
  187.  
  188.  
  189. $ContextDest.Load($newContentType.FieldLinks)
  190. $ContextDest.ExecuteQuery()
  191.  
  192. $Context.Load($ccSRC.FieldLinks)
  193. $Context.ExecuteQuery()
  194.  
  195. #Add the field mappings
  196. foreach($fieldMap in $ccSRC.FieldLinks)
  197. {
  198. Write-Host $fieldMap.Name $fieldMap.Required
  199.  
  200. $field = $fieldsDEST.GetByInternalNameOrTitle($fieldMap.Name)
  201. #create FieldLinkCreationInformation object (flci)
  202. $flci = new-object Microsoft.SharePoint.Client.FieldLinkCreationInformation
  203. $flci.Field = $field
  204. $addContentType = $newContentType.FieldLinks.Add($flci)
  205. $newContentType.Update($true)
  206. $ContextDest.ExecuteQuery()
  207. }
  208. #$newContentType.Update($true)
  209. #$ContextDest.ExecuteQuery()
  210.  
  211.  
  212. #$newContentType.FieldLinks = $ccSRC.FieldLinks
  213. #$newContentType.Fields = $ccSRC.Fields
  214. #$newContentType.Update($true)
  215. #$ContextDest.ExecuteQuery()
  216.  
  217.  
  218.  
  219.  
  220. # Write-Host $_.Parent
  221. }
  222. }
  223.  
  224.  
  225.  
  226. ###create the content type
  227.  
  228.  
  229.  
  230. #$TargetWeb.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement