1.获取原始页面的CanvasContent1存到xml文件里
Connect-PnPOnline -Url 'https://siteur' -UseWebLogin
$homepage=Get-PnPPage -Identity "Home"
$page=Get-PnPListItem -List SitePages -Id $homepage.PageId
$content=$page.FieldValues.CanvasContent1
$content | Out-File 'TestHome.xml'
替换siteUrl和Home ,替换成想要复制的页面所在的site和page。
2.通过Set-PnPListItem -List 'Site Pages' -Identity $id -Values @{"CanvasContent1"=$xmlstr}
把xml部署到新页面上
$siteUrl = Read-Host 'Site Url'
Connect-PnPOnline -Url $siteUrl -UseWebLogin
$pageName = "Home";
$pg = Get-PnPPage -Identity $pageName;
#清空页面
$sectionsCount = $pg.Sections.Count;
for ($i = $sectionsCount - 1; $i -ge 0; $i--) {
$pg.Sections.RemoveAt($i);
}
$id=$pg.PageId
$xml=Get-Content -path ".\TestHome.xml" -Encoding UTF8
$xmlstr = $xml -join ''
$web = Get-PnPWeb
#如果页面上有图片,图片传到siteAssests里,并且replace xml文件里的id
$list = Get-PnPList -Identity 'Site Assets'
$xmlstr = $xmlstr.Replace('89c431b7-734f-48af-afcf-9a6f56311bc6', $site.Id).Replace('2d4695fc-97f1-4c8a-affc-1eb117ef047d', $web.Id).Replace('20e0593c-20bd-4d3f-a775-d99e9624c261', $list.Id).Replace('orgionaldomain', 'newdomain').Replace('/sites/orginal', $web.ServerRelativeUrl)
#上传图片
$file = Add-PnPFile -Path .\SiteAssets\Message.png -Folder "SiteAssets/SitePages/Home"
$xmlstr = $xmlstr.Replace('61f8cba2-a54d-4b52-9b65-8c412d975641', $file.UniqueId)
#部署新页面
Set-PnPListItem -List 'Site Pages' -Identity $id -Values @{"CanvasContent1"=$xmlstr}
Set-PnPPage -Identity "Home" -LayoutType Home -Publish
Set-PnPHomePage -RootFolderRelativeUrl SitePages/Home.aspx
#设置logo
Set-PnPSite -LogoFilePath .\Logo.png
#设置theme
Get-PnPTenantTheme -Name "yourThemeName" | Set-PnPWebTheme
3.补充
(1)通过json文件添加Navigation
function Add-Navigation($navigations, $location) {
if ($navigations.Count -ne 0) {
foreach ($navigation in $navigations) {
$nav = Add-PnPNavigationNode -Title $navigation.Name -Url $navigation.Url -Location $location -External;
if ($null -ne $navigation.Children -and $navigation.Children.Count -ne 0) {
foreach ($subNavigation in $navigation.Children) {
$subNav = Add-PnPNavigationNode -Title $subNavigation.Name -Url $subNavigation.Url -Location $location -Parent $nav.Id -External;
if ($null -ne $subNavigation.Children -and $subNavigation.Children.Count -ne 0) {
foreach ($thirdNavigation in $subNavigation.Children) {
$subsubNav = Add-PnPNavigationNode -Title $thirdNavigation.Name -Url $thirdNavigation.Url -Location $location -Parent $subNav.Id -External;
if ($null -ne $thirdNavigation.Children -and $thirdNavigation.Children.Count -ne 0) {
foreach ($fourthNavigation in $thirdNavigation.Children) {
Write-Host $fourthNavigation.Name
Add-PnPNavigationNode -Title $fourthNavigation.Name -Url $fourthNavigation.Url -Location $location -Parent $subsubNav.Id -External;
}
}
}
}
}
}
}
}
}
$site = Get-PnPSite -Includes IsHubSite,Id
#判断是否为hubsite
if ($site.IsHubSite -eq $true) {
Get-PnPNavigationNode -Location TopNavigationBar | Remove-PnPNavigationNode -Force
$navJson = Get-Content ".\hubnavigation.json" -Raw | ConvertFrom-Json;
Add-Navigation $navJson 'TopNavigationBar'
Get-PnPNavigationNode -Location QuickLaunch | Remove-PnPNavigationNode -Force
}
else {
Get-PnPNavigationNode -Location QuickLaunch | Remove-PnPNavigationNode -Force
$navJson = Get-Content ".\navigation.json" -Raw | ConvertFrom-Json;
Add-Navigation $navJson 'QuickLaunch'
}
navigation.json结构示例:
[
{
"Name": "Nav1",
"Url": "https://nav1"
},
{
"Name": "Nav2",
"Url": "http://linkless.header/",
"Children": [
{
"Name": "Nav2Sub1",
"Url": "https://Nav2Sub1"
},
{
"Name": "Nav2Sub2",
"Url": "Nav2Sub1"
}
]
}
]
(2)命令行添加Navigation
$web = Get-PnPWeb
Get-PnPNavigationNode -Location QuickLaunch | Remove-PnPNavigationNode -Force
Add-PnPNavigationNode -Title $web.Title -Url $web.Url -Location QuickLaunch -External
Add-PnPNavigationNode -Title 'Internal' -Url ($web.Url + '/internal') -Location QuickLaunch -External
Add-PnPNavigationNode -Title 'Resources' -Url ($web.Url + '/resources') -Location QuickLaunch -External
(3) 添加list
$modernEvent = 'Upcoming Events'
New-PnPList -Title $modernEvent -Template GenericList
Remove-PnPContentTypeFromList -List $modernEvent -ContentType 'Item'
Add-PnPContentTypeToList -List $modernEvent -ContentType Event
$category = Get-PnPField -List 'Events' -Identity 'Category'
Set-PnPField -List $modernEvent -Identity 'Category' -Values @{Choices = $category.Choices; DefaultValue = '' }
$fields = @('Title','Location','Start Time','End Time','Description','Category')
Set-PnPView -List $modernEvent -Identity 'All Items' -Fields $fields
$view="Calendar view";
$viewCreationJson = @"
{
"parameters": {
"__metadata": {
"type": "SP.ViewCreationInformation"
},
"Title": "$view",
"ViewFields": {
"__metadata": {
"type": "Collection(Edm.String)"
},
"results": [
"StartDate",
"EndDate",
"Title"
]
},
"ViewTypeKind": 1,
"ViewType2": "MODERNCALENDAR",
"ViewData": "<FieldRef Name=\"Title\" Type=\"CalendarMonthTitle\" /><FieldRef Name=\"Title\" Type=\"CalendarWeekTitle\" /><FieldRef Name=\"Title\" Type=\"CalendarWeekLocation\" /><FieldRef Name=\"Title\" Type=\"CalendarDayTitle\" /><FieldRef Name=\"Title\" Type=\"CalendarDayLocation\" />",
"CalendarViewStyles": "<CalendarViewStyle Title=\"Day\" Type=\"day\" Template=\"CalendarViewdayChrome\" Sequence=\"1\" Default=\"FALSE\" /><CalendarViewStyle Title=\"Week\" Type=\"week\" Template=\"CalendarViewweekChrome\" Sequence=\"2\" Default=\"FALSE\" /><CalendarViewStyle Title=\"Month\" Type=\"month\" Template=\"CalendarViewmonthChrome\" Sequence=\"3\" Default=\"TRUE\" />",
"Query": "",
"Paged": true,
"PersonalView": false,
"RowLimit": 0
}
}
"@
try{
$calendarView=Get-PnPView -List $modernEvent -Identity $view
}catch{
Write-Host "view not exist"
}
#avoid repeat view
if($calendarView -eq $null){
Invoke-PnPSPRestMethod -Method Post -Url "$siteUrl/_api/web/lists/GetByTitle('$ModernEvent')/Views/Add" -ContentType "application/json;odata=verbose" -Content $viewCreationJson
}
Set-PnPView -List $ModernEvent -Identity $view -Values @{DefaultView=$true;MobileView=$true;MobileDefaultView=$true} #Set View To Be Default
#Add Carpark Assigned To column
Add-PnPField -List $modernEvent -DisplayName 'Carpark Assigned to' -InternalName ‘CarparkAssignedTo' -Type User
#set category column value
$category = Get-PnPField -List $modernEvent -Identity 'Category'
$category | Select -Property *
$categoryId=$category.Id
$categorySchemaXml =@"
<Field ID="$categoryId" Name="Category" DisplayName="Event Type" Type="Choice" Format="Dropdown" FillInChoice="TRUE" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="Category" ColName="nvarchar10">
<CHOICES>
<CHOICE>Site Visit</CHOICE>
<CHOICE>Go Live</CHOICE>
<CHOICE>Working at Home</CHOICE>
<CHOICE>On Leave</CHOICE>
<CHOICE>Sick</CHOICE>
<CHOICE>Conference/Training</CHOICE>
<CHOICE>Other</CHOICE>
</CHOICES>
</Field>
"@
Set-PnPField -List $modernEvent -Identity 'Category' -Values @{SchemaXml = $categorySchemaXml; DefaultValue = '' }
#隐藏本来的list
Set-PnpList -Identity 'Events' -Hidden $true