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 = "http://localhost/"
$nameSpace = "http://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

    $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"
                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
                    $site = Get-SPSite -Identity $SiteUrl
                    $web = $site.OpenWeb()
                    $web.Lists.Add($Entity.Name, "", [String]::Format($ListUrl, $Entity.Name), $ds);

$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 = "http://$env:USERDNSDOMAIN/".toLower()

As they say in the SuSE Linux motd: Have a lot of fun!

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Posted in PowerShell, SharePoint

Leave a Reply

Your email address will not be published. Required fields are marked *