About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Monday 25 August 2014

Powershell Script - Import BCS model along with permissions in SharePoint 2013

The following script will import BCS entity to SharePoint environment. The script need following as input parameters

1. Site Url
2.Admin user account
3.All users group (NT AUTHORITY\authenticated users)
4.Catalog Name (The BCS entity name which you are going to create/update)

Steps involved


Note: Make sure that, your .bdcm file should be in the current directory (where you are running your script)

1. Save following script as psDeployBCSModel.ps1

Param(           
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]           
[string]$SiteUrl,           
[Parameter(Mandatory=$true)]           
[string]$AdminUser,           
[Parameter(Mandatory=$true)]           
[string]$AllUsers,
[Parameter(Mandatory=$true)]           
[string]$CatalogName        
)

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell";
}
    
function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

function SetBcsProfileHostUrl($bdc, $siteUrl)
{
    #$catalog = Get-SPBusinessDataCatalogMetadataObject -BdcObjectType "Catalog" -ServiceContext $siteUrl
    $property = $bdc.Properties | Where { $_.Name -eq "Profile_HostURL" }
    if ($property) {
        $bdc.Properties.Remove("Profile_HostURL")
    }
    $bdc.Properties.Add("Profile_HostURL", $siteUrl)
    write-host write-host “Successfully added BCS profile page to the model-” -ForegroundColor Green
}

#Get Current Physical Path
$currentPhysicalPath = Get-ScriptDirectory
$logfile=$currentPhysicalPath + "\log.log"
Start-Transcript $logfile

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

#$ecConvCatalog = Get-SPBusinessDataCatalogMetadataObject -BdcObjectType "Catalog" -Name $CatalogName -ServiceContext $SiteUrl

#if($ecConvCatalog)
#{

#Remove-SPBusinessDataCatalogMetadataObject –Identity $ecConvCatalog
#write-host write-host “Successfully removed existing BCS model-” -ForegroundColor Green
#}

foreach ($file in $importFiles) {
     if($file -ne $null)
     {

         Import-SPBusinessDataCatalogModel -Path $file.FullName -Identity $bdc -force -ModelsIncluded -PropertiesIncluded -PermissionsIncluded -Verbose
         write-host write-host “Successfully imported BCS model-” $file.Name -ForegroundColor Green
    
         $claimAdmin = New-SPClaimsPrincipal -Identity $AdminUser -IdentityType WindowsSamAccountName
         $claimUsers = New-SPClaimsPrincipal -Identity $AllUsers -IdentityType WindowsSamAccountName

         $claimAdmin = New-SPClaimsPrincipal -Identity $AdminUser -IdentityType WindowsSamAccountName
         $claimUsers = New-SPClaimsPrincipal -Identity $AllUsers -IdentityType WindowsSamAccountName

         Grant-SPBusinessDataCatalogMetadataObject -Identity $bdc -Principal $claimAdmin -Right "Execute,SetPermissions,Edit,SelectableInClients"
         Grant-SPBusinessDataCatalogMetadataObject -Identity $bdc -Principal $claimUsers -Right "Execute"
         write-host write-host “Successfully granted access rights to BCS model-” $file.Name -ForegroundColor Green

         Copy-SPBusinessDataCatalogAclToChildren -MetadataObject $bdc
         write-host write-host “Successfully deployed BCS entity-” $file.Name -ForegroundColor Green
    }
}# end of for
   
SetBcsProfileHostUrl -bdc $bdc -siteUrl $SiteUrl
}
catch
{
    $Host.UI.RawUI.WindowTitle = " -- Error --"
    Write-Host -ForegroundColor Red $_.ToString()
    Write-Host -ForegroundColor Red $_.Exception.ToString()
}

Stop-Transcript


Step2: Create batch file to run the script. Save file in .bat format.

/cd /d %~dp0
powershell -file ./psDeployBCSModel.ps1 -SiteUrl "https://devpub.ec.com" -AdminUser "EC\SPBCS" -AllUsers "NT AUTHORITY\authenticated users" -CatalogName "ECConversation"
pause


Happy powershell coding... please feel free to comment...

6 comments:

  1. Copied from a product named EC ?

    ReplyDelete
    Replies
    1. FYI.. I am the author for this script check my details in the script file in Oakton in project EC... if you want, I can send TFS path..sending a comment as axannonumus is not good..

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. This comment has been removed by the author.

      Delete