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.

# script from # Make sure the namespace is the same as the namespace in your exported bdcm files # My $siteURL happens 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 servicecontext localhost # # What this does: # Scans current directory for BDC Export(.bdcm) files # Creates the External System/Lob and the External Content Types # Creates External Lists from those ECT's # Sets the proper permissions

$adminGroup = "DOMAIN\SharePoint Admins" $userGroup = "DOMAIN\SharePoint Users" $serviceContext = "https://localhost/" $nameSpace = "https://sharepoint/" $SiteUrl = $nameSpace

$bdc = Get-SPBusinessDataCatalogMetadataObject -BdcObjectType Catalog -ServiceContext $serviceContext $pathtobdcmfiles = Get-Location $importFiles = Get-Childitem -path $pathtobdcmfiles | Where {$_.extension -eq ".bdcm" -and $_.basename -ne "catalog"}

foreach ($file in $importFiles) { Import-SPBusinessDataCatalogModel -Path $file.FullName -Identity $bdc -force -ModelsIncluded -PropertiesIncluded -PermissionsIncluded -Verbose

# THIS WHOLE CHUNK COMES FROM: # IT MAKES THE EXTERNAL LISTS FROM THE MODELS. AWESOME. $Model = Get-SPBusinessDataCatalogMetadataObject -BdcObjectType Model -Name $file.baseName -ServiceContext $ServiceContext $ListUrl = "Lists/{0}"

ForEach ($Entity in $Model.AllEntities) { $ns = $Entity.Namespace Write-Host "Entity Namespace : $ns" $name = $entity.Name Write-Host "Entity Name : $name"

Write-Host "Looking for MethodInstance specific finder..." $sf = $Entity.MethodInstances | Where-Object{ $_.MethodInstanceType -eq [Microsoft.BusinessData.MetadataModel.MethodInstanceType]::SpecificFinder } If($sf -eq $null) { Write-Host "Skipping external list creation as Method Instance of Type SpecificFinder was not found in Entity : $name" break } else { Write-Host "Creating and Configuring SPListDataSource" $ds = New-Object -TypeName Microsoft.SharePoint.SPListDataSource $instanceName = [String]::Empty ForEach($instance in $entity.LobSystem.LobSystemInstances){$instanceName = $Instance.Name}

$ds.SetProperty("LobSystemInstance", $instanceName) $ds.SetProperty("EntityNamespace", $ns) $ds.SetProperty("Entity", $name) $ds.SetProperty("SpecificFinder", $sf.Name)

$site = $null Try { $site = Get-SPSite -Identity $SiteUrl $web = $site.OpenWeb() $web.Lists.Add($Entity.Name, "", [String]::Format($ListUrl, $Entity.Name), $ds); } Finally { $web.Dispose() $site.Dispose() } } } #END CHUNK }

$claimAdmin = New-SPClaimsPrincipal -Identity $adminGroup -IdentityType WindowsSamAccountName $claimUsers = New-SPClaimsPrincipal -Identity $userGroup -IdentityType WindowsSamAccountName

Grant-SPBusinessDataCatalogMetadataObject -Identity $bdc -Principal $claimAdmin -Right "Execute,SetPermissions,Edit,SelectableInClients" Grant-SPBusinessDataCatalogMetadataObject -Identity $bdc -Principal $claimUsers -Right "Execute,Edit,SelectableInClients"

Copy-SPBusinessDataCatalogAclToChildren -MetadataObject $bdc

#Ignore this, its what I wish my namespace was... #$nameSpace = "https://$env:USERDNSDOMAIN/".toLower()   As they say in the SuSE Linux motd: Have a lot of fun!