Home Copy SharePoint list to another site using PNP.PowerShell
Post
Cancel

Copy SharePoint list to another site using PNP.PowerShell

Summary

This sample demonstrates how to copy SharePoint list to another SharePoint site using PNP.PowerShell.

Pre-requisites

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Create object to store list templates
$SPBaseTemplates = @{ 
  100 = 'GenericList'; 
  101 = 'DocumentLibrary'; 
  102 = 'Survey'; 
  103 = 'Links'; 
  104 = 'Announcements'; 
  105 = 'Contacts'; 
  106 = 'Events'; 
  107 = 'Tasks'; 
  108 = 'DiscussionBoard'; 
  109 = 'PictureLibrary'; 
  110 = 'DataSources'; 
  118 = 'WorkflowProcess'; 
  120 = 'CustomGrid'; 
  140 = 'WorkflowHistory'; 
  1100 = 'IssuesTracking'; 
  119 = 'WebPageLibrary' 
  }

# Additional list settings
$DraftVersionVisibilityList = @{ 0 = 'Reader'; 1 = 'Author'; 2 = 'Approver'; }
$ReadSecurityList = @{ 1 = 'AllUsersReadAccess'; 2 = 'AllUsersReadAccessOnItemsTheyCreate'; }
$WriteSecurityList = @{ 1 = 'WriteAllItems'; 2 = 'WriteOnlyMyItems'; 3 = 'WriteNoItems' }

$RoleDefinitionBindingsList = @{ 
  1073741826 = 'Full Control'; 
  1073741827 = 'Design'; 
  1073741828 = 'Edit'; 
  1073741829 = 'Contribute'; 
  1073741830 = 'Read' 
  }

$listTitle = "<ListTitle>"
$sourceWeb = "<SourceSiteUrl>"
$destinationWeb = "<DestintionSiteUrl>"

Write-host "Ensure logged in"
try {
    $context = Get-PnPContext

    if ($context -eq $null) {
        throw "No PnP context found."
    }
    Write-Host "PnP context found."
}
catch {
    Connect-PnPOnline -Url $sourceWeb -Interactive
}

Write-Host "Get list settings, views, fields and Check if list has unique permissions"
$sourceListSettings = Get-PnPList -Identity $listTitle -Includes RoleAssignments, Fields, Views, BaseTemplate, DraftVersionVisibility
$sourceListViews = $sourceListSettings.Views
$sourceListColumns = $sourceListSettings.Fields
$sourceListPermissions = $sourceListSettings.RoleAssignments

#AllUsersReadAccess, AllUsersReadAccessOnItemsTheyCreate
$sourceListSettings.ReadSecurity = "AllUsersReadAccess"

# Switch context to destination web

Connect-PnPOnline -Url $destinationWeb -Interactive

Write-Host "Translate template number to name"
$baseTemplate = $SPBaseTemplates[$sourceListSettings.BaseTemplate]

Write-Host "Create new list"

New-PnPList -Title $sourceListSettings.Title -Template $baseTemplate -OnQuickLaunch

Write-Host "Set other list settings"
# Note: You will need to map all other properties from the source list to the appropriate parameters for Set-PnPList

Set-PnPList `
-Identity $sourceListSettings.Title `
-Description $sourceListSettings.Description `
-EnableAttachments $sourceListSettings.EnableAttachments `
-EnableContentTypes $sourceListSettings.AllowContentTypes `
-EnableFolderCreation $sourceListSettings.EnableFolderCreation `
-EnableMinorVersions $sourceListSettings.EnableMinorVersions `
-EnableModeration $sourceListSettings.EnableModeration `
-DraftVersionVisibility $sourceListSettings.DraftVersionVisibility `
-EnableVersioning $sourceListSettings.EnableVersioning `
-ForceCheckout $sourceListSettings.ForceCheckout `
-Hidden $sourceListSettings.Hidden `
-ListExperience $sourceListSettings.ListExperienceOptions `
-MajorVersions $sourceListSettings.MajorVersionLimit `
-MinorVersions $sourceListSettings.MajorWithMinorVersionsLimit `
-ReadSecurity $ReadSecurityList[1] `
-WriteSecurity $WriteSecurityList[1] `
-NoCrawl `
-DisableGridEditing $sourceListSettings.DisableGridEditing


# Permissions are not copied by default, so we need to check if the source list has unique permissions and if so, copy them to the destination list
if ($sourceListSettings.HasUniqueRoleAssignments) {
    Write-Host "Set unique permissions"
    
    # Break role inheritance without copying existing permissions
    Set-PnPList -Identity $sourceListSettings.Title -BreakRoleInheritance $true -CopyRoleAssignments $false
    
    # Retrieve all role definitions for the site
    $RoleDefinitionBindingsList = Get-PnPRoleDefinition | Select-Object -Property Id, Name
    
    # Create a hashtable mapping role definition names to their IDs for easy lookup
    $roleMap = @{}
    foreach ($role in $RoleDefinitionBindingsList) {
        $roleMap[$role.Name] = $role.Id
    }
    
    foreach ($roleAssignment in $sourceListPermissions) {
        $member = $roleAssignment | Select-Object -ExpandProperty Member
        
        # Assuming the role assignment has a single role definition. If there are multiple, this needs to be adjusted.
        $roleName = $roleAssignment.RoleDefinitionBindings[0].Name
        
        if ($roleMap.ContainsKey($roleName)) {
            Set-PnPListPermission -Identity $sourceListSettings.Title -User $Member.LoginName -AddRole $roleName
        } else {
            Write-Host "Role $roleName not found in destination site." -ForegroundColor Yellow
        }
    }
}


Write-Host "Add columns"
$destinationListColumns = Get-PnPField -List $sourceListSettings.Title
foreach ($column in $sourceListColumns) {
    $fnd = $destinationListColumns | Where-Object { $_.InternalName -eq $column.InternalName }
    if (-not $fnd) {
        Add-PnPFieldFromXml -List $sourceListSettings.Title -FieldXml $column.SchemaXml
    }
}


Write-Host "Add views"
foreach ($view in $sourceListViews) {
    $schema = [xml]$view.HtmlSchemaXml
    $fields = $schema.View.ViewFields.FieldRef.Name
    
    $viewQuery = $view.ViewQuery.replace('"', "'")
    if (-not $viewQuery) {
        Add-PnPView -List $sourceListSettings.Title -Title $view.Title -Fields $fields | Out-Null
    } else {
        Add-PnPView -List $sourceListSettings.Title -Title $view.Title -Fields $fields -Query $viewQuery | Out-Null
    }
}

PNP Script sample site

Script sample site

This post is licensed under CC BY 4.0 by the author.

Upload spfx solution to app catalog and Azure AD App Certificate

SharingIsCaring. Advent Calendar - A Fun Christmas-Themed SPFx