Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
- Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
- ##Variables for Processing
- $SiteUrl = "https://defradev.sharepoint.com/sites/EA-SP-LP-DEVWASTE/"
- $SiteUrlDest = "https://defradev.sharepoint.com/sites/EA-SP-LP-DEVMASTER/"
- $UserName ="kaloyan.kurtev@defradev.onmicrosoft.com"
- $Password = 'K@l0yan"'
- $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
- $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
- $Context.Credentials = $credentials
- $Context.Load($Context.Web.AvailableContentTypes)
- $Context.ExecuteQuery()
- $CredentialsDest = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
- $ContextDest = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrlDest)
- $ContextDest.Credentials = $CredentialsDest
- $ContextDest.Load($ContextDest.Web.AvailableContentTypes)
- #$ContextDest.Load($ContextDest.Web.ContentTypes)
- $ContextDest.ExecuteQuery()
- #Get the Content Type Schema from XML
- $XMLFile = "./SiteContentTypes.xml"
- [xml] $CTypeXML = Get-Content($XMLFile)
- #Create Site Content Types
- #$CTypeXML.ContentTypes.ContentType | ForEach-Object {
- #vars
- ##vars
- foreach($ccSRC in $Context.Web.AvailableContentTypes)
- {
- #$lci =New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
- #$lci.Description="Description"
- #$lci.Name="Powershell Content Type2"
- #$lci.ParentContentType=$ctx.Web.ContentTypes.GetById("0x01")
- #$lci.Group="List Content Types"
- $found = 0
- foreach($cc in $ContextDest.Web.AvailableContentTypes)
- {
- if($cc.Name.Equals($ccSRC.Name))
- {
- $found++
- }
- }
- #If not found
- if($found -eq 0)
- {
- write-host "Content type " $_.Name " with ID " $_.Id " not found"
- #Get the parent
- $Context.Load($ccSRC.Parent)
- $Context.ExecuteQuery()
- Write-Host "Parent ID " $ccSRC.Parent.Id
- #Create the content type
- #$ContextDest.Web.ContentTypes.AddExistingContentType($ccSRC)
- #$ContextDest.ExecuteQuery()
- #Create it
- #Create New Content Type object inheriting from parent
- #$SPContentType = New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation # New-Object Microsoft.SharePoint.SPContentType ($_.ID,$TargetWeb.ContentTypes,$_.Name)
- $SPContentType = New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
- Write-Host "Type ID " $SPContentType.TypeId
- #Set Content Type name and id
- $SPContentType.Name = $ccSRC.Name
- #$SPContentType.Id = $ccSRC.Id.StringValue
- #Set Content Type description and group
- $SPContentType.Description = $ccSRC.Description
- $SPContentType.Group = $ccSRC.Group
- $ContextDest.Load($ContextDest.Web.ContentTypes)
- $ContextDest.ExecuteQuery()
- foreach($ccPar in $ContextDest.Web.ContentTypes)
- {
- if($ccPar.Name.Equals($ccSRC.Parent.Name))
- {
- $SPContentType.ParentContentType = $ccPar
- }
- }
- Write-Host "test " $SPContentType.ParentContentType
- ##The actual creation
- $ctCreated = $ContextDest.Web.ContentTypes.Add($SPContentType)
- $ContextDest.Load($ctCreated)
- $ContextDest.ExecuteQuery()
- $fieldsDEST = $ContextDest.Web.Fields
- $fieldsSRC = $Context.Web.Fields
- #$contentTypesDEST = New-Object Microsoft.SharePoint.Client.ContentTypeCollection
- #$contentTypesDEST = $ContextDest.Web.ContentTypes
- $ContextDest.load($fieldsDEST)
- $Context.Load($fieldsSRC)
- #$ContextDest.load($contentTypesDEST)
- $ContextDest.ExecuteQuery()
- $Context.ExecuteQuery()
- #$contentTypesDEST.Add($SPContentType)
- #$ContextDest.Load($SPContentType)
- #$ContextDest.ExecuteQuery()
- $newFields = @()
- foreach($srcField in $fieldsSRC)
- {
- $fieldExists = 0
- foreach($dstField in $fieldsDEST)
- {
- if($dstField.EntityPropertyName.Equals($srcField.EntityPropertyName))
- {
- $fieldExists++
- }
- }
- if($fieldExists -eq 0)
- {
- Write-Host $srcField.InternalName "Does not exist"
- Write-Host "Creating field " $srcField.InternalName
- #$fieldAsXML = "<Field Type='Text'
- #DisplayName='$srcField.InternalName'
- #Name='$srcField.InternalName'
- #Group='$srcField.Group'/>"
- #$fieldOption = [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint
- #$field = $fieldsDEST.AddFieldAsXML($srcField.SchemaXml, $true, $fieldOption)
- #$ContextDest.load($field)
- $newFields += $srcField.SchemaXml
- }
- }
- foreach($newField in $newFields)
- {
- $fieldOption = [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint
- $field = $ContextDest.Web.Fields.AddFieldAsXML($newField, $true, $fieldOption)
- $ContextDest.load($field)
- $ContextDest.ExecuteQuery()
- #$ContextDest.Load($fieldsDEST);
- #$ContextDest.ExecuteQuery()
- }
- #doesnt work
- #$newContentType = $ContextDest.Web.ContentTypes.GetById($SPContentType.TypeId)
- $newContentType = New-Object Microsoft.SharePoint.Client.ContentType
- foreach($updatedCT in $ContextDest.Web.ContentTypes)
- {
- if($updatedCT.Name -eq $SPContentType.Name)
- {
- $newContentType = $updatedCT
- }
- }
- $ContextDest.Load($newContentType.FieldLinks)
- $ContextDest.ExecuteQuery()
- $Context.Load($ccSRC.FieldLinks)
- $Context.ExecuteQuery()
- #Add the field mappings
- foreach($fieldMap in $ccSRC.FieldLinks)
- {
- Write-Host $fieldMap.Name $fieldMap.Required
- $field = $fieldsDEST.GetByInternalNameOrTitle($fieldMap.Name)
- #create FieldLinkCreationInformation object (flci)
- $flci = new-object Microsoft.SharePoint.Client.FieldLinkCreationInformation
- $flci.Field = $field
- $addContentType = $newContentType.FieldLinks.Add($flci)
- $newContentType.Update($true)
- $ContextDest.ExecuteQuery()
- }
- #$newContentType.Update($true)
- #$ContextDest.ExecuteQuery()
- #$newContentType.FieldLinks = $ccSRC.FieldLinks
- #$newContentType.Fields = $ccSRC.Fields
- #$newContentType.Update($true)
- #$ContextDest.ExecuteQuery()
- # Write-Host $_.Parent
- }
- }
- ###create the content type
- #$TargetWeb.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement