SharePoint + PowerShell: Batch Import Models, Create ECT's, Set Permissions
Here is a script that will batch import Models, create associated ECT's, and set permissions for users and admins. Please modify the variables as needed.
The script does the following:
- Scans current directory for BDC Export (.bdcm) files
- Creates the External System/Lob and the External Content Types
- Creates External Lists from those ECTs
- Sets the proper permissions
Notes:
- Make sure the namespace matches the namespace in your exported BDCM files.
- My
$SiteUrlhappens to be the same as my$nameSpace. That may not be the case for you. - Do this only on dev machines. I'm running this on the SharePoint 2010 server so I made my service context localhost.
1# script from netnerds.net
2
3$adminGroup = "DOMAIN\SharePoint Admins"
4$userGroup = "DOMAIN\SharePoint Users"
5$serviceContext = "https://localhost/"
6$nameSpace = "https://sharepoint/"
7$SiteUrl = $nameSpace
8
9$bdc = Get-SPBusinessDataCatalogMetadataObject -BdcObjectType Catalog -ServiceContext $serviceContext
10$pathtobdcmfiles = Get-Location
11$importFiles = Get-ChildItem -Path $pathtobdcmfiles | Where-Object { $_.Extension -eq ".bdcm" -and $_.BaseName -ne "catalog" }
12
13foreach ($file in $importFiles) {
14 Import-SPBusinessDataCatalogModel -Path $file.FullName -Identity $bdc -Force -ModelsIncluded -PropertiesIncluded -PermissionsIncluded -Verbose
15
16 # THIS WHOLE CHUNK COMES FROM (Wayback):
17 # https://web.archive.org/web/20121102153207/http://gallery.technet.microsoft.com/scriptcenter/82437789-f294-4bcd-8210-a1ba0e081f82
18 # IT MAKES THE EXTERNAL LISTS FROM THE MODELS. AWESOME.
19 $Model = Get-SPBusinessDataCatalogMetadataObject -BdcObjectType Model -Name $file.BaseName -ServiceContext $ServiceContext
20 $ListUrl = "Lists/{0}"
21
22 foreach ($Entity in $Model.AllEntities) {
23 $ns = $Entity.Namespace
24 Write-Host "Entity Namespace : $ns"
25 $name = $Entity.Name
26 Write-Host "Entity Name : $name"
27
28 Write-Host "Looking for MethodInstance specific finder..."
29 $sf = $Entity.MethodInstances | Where-Object { $_.MethodInstanceType -eq [Microsoft.BusinessData.MetadataModel.MethodInstanceType]::SpecificFinder }
30
31 if ($sf -eq $null) {
32 Write-Host "Skipping external list creation as Method Instance of Type SpecificFinder was not found in Entity : $name"
33 break
34 }
35 else {
36 Write-Host "Creating and Configuring SPListDataSource"
37 $ds = New-Object -TypeName Microsoft.SharePoint.SPListDataSource
38 $instanceName = [String]::Empty
39 foreach ($instance in $Entity.LobSystem.LobSystemInstances) { $instanceName = $Instance.Name }
40
41 $ds.SetProperty("LobSystemInstance", $instanceName)
42 $ds.SetProperty("EntityNamespace", $ns)
43 $ds.SetProperty("Entity", $name)
44 $ds.SetProperty("SpecificFinder", $sf.Name)
45
46 $site = $null
47 try {
48 $site = Get-SPSite -Identity $SiteUrl
49 $web = $site.OpenWeb()
50 $web.Lists.Add($Entity.Name, "", [String]::Format($ListUrl, $Entity.Name), $ds)
51 }
52 finally {
53 $web.Dispose()
54 $site.Dispose()
55 }
56 }
57 } # END CHUNK
58}
59
60$claimAdmin = New-SPClaimsPrincipal -Identity $adminGroup -IdentityType WindowsSamAccountName
61$claimUsers = New-SPClaimsPrincipal -Identity $userGroup -IdentityType WindowsSamAccountName
62
63Grant-SPBusinessDataCatalogMetadataObject -Identity $bdc -Principal $claimAdmin -Right "Execute,SetPermissions,Edit,SelectableInClients"
64Grant-SPBusinessDataCatalogMetadataObject -Identity $bdc -Principal $claimUsers -Right "Execute,Edit,SelectableInClients"
65
66Copy-SPBusinessDataCatalogAclToChildren -MetadataObject $bdc
67
68# Ignore this, it's what I wish my namespace was...
69# $nameSpace = "https://$env:USERDNSDOMAIN/".ToLower()
As they say in the SuSE Linux motd: Have a lot of fun!