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...

Friday, 4 July 2014

Powershell Script - Create Term Sets & groups in SharePoint 2013 Managed Meta Data service

The following script will create various term set groups, term sets & terms. GUID is optional. The script will create log file to current directory where the script runs... This script will create two term groups & one term set for each respective group.

Steps involved

1. Save following script as psCreateTermSets.ps1


#-----------------------------------------------------------------------------
# Name:             psCreateTermSets.ps1 
# Description:      This script will create a list of termset groups,termsets,
#                    terms along with guids.
#                    
# Usage:            Run the script by passing the paramter SiteUrl,
#                   ManagedMetaDataServiceName
# Created By:       Vamsi Mohan Mitta
#-----------------------------------------------------------------------------

Param([Parameter(Mandatory=$true)]
      [String]
      $SiteUrl,
      [Parameter(Mandatory=$true)]
      [String]
      $ManagedMetaDataServiceName
)

if ((gsnp Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null){
    asnp Microsoft.SharePoint.Powershell
}

function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

# Create term groups with guid
Function CreateTermGroup($termStore, $termGroupName, $GUID)
 {
     if ($termStore.Groups[$termGroupName] -eq $null)
      {
        $termGroup = $termStore.CreateGroup($termGroupName, $GUID)
        $termStore.CommitAll()
        write-host “Successfully created term group -” $termGroupName -ForegroundColor Green
      }
      else
      { write-host “Term Group already -” $termGroupName " exists" -ForegroundColor Blue }
  }


 Function CreateTermSets($termGroup, $termSet, $termSetGuid)
 {   
    $getTermGroup = $termStore.Groups[$termGroup]
    if($getTermGroup.TermSets[$termSet] -eq $null)
    {
    $createTermSet = $getTermGroup.CreateTermSet($termSet,[System.Guid]($termSetGuid))
    $termStore.CommitAll()
    write-host “Successfully created term set-” $termSet -ForegroundColor Green
    }
    else
      { write-host “Term Set already -” $termSet " exists" -ForegroundColor Blue }
 }

 #Get Current Physical Path
$currentPhysicalPath = Get-ScriptDirectory

$logfile=$currentPhysicalPath + "\log.log"

Start-Transcript $logfile

 #declare all respective term groups, term sets & their guids
 $grpConstructionProject = "Construction Project"
 $guidConsProject = "14a7dfbc-d818-46f4-afbc-578fe1fe65df"

 $grpITProject = "IT Project"
 $guidITProject = "f3036492-1f65-46ba-ba69-4b0b2272ca35"

 $termSetConsClient = "Client"
 $guidConsClient = "19a01208-e1e3-49be-9f8d-fdebcbf9fb83"

 $termSetConsIndustry = "Industry & sector"
 $guidConsIndustry = "2FC48649-FF10-4A2E-BF72-6C600EA3E1AF"

 $termSetITClient = "Client"
 $guidITClient = "32088415-f136-42da-af43-9bcc360e5a34"

 $termSetITIndustry = "Industry"
 $guidITIndustry = "d6f66023-3e31-48ab-b413-64a6cdbca1b2"

 $termSetITSubIndustry = "Sub-Industry"
 $guidITSubIndustry = "87567d4f-69fc-4259-82c7-88042d880b58"

 $termSetITTechnology = "Technology"
 $guidITTechnology = "9f18bc77-3d82-4816-8f02-569cb041a6b6"


$taxonomySite = get-SPSite $SiteUrl
#Connect to Term Store in the Managed Metadata Service Application
 $taxonomySession = Get-SPTaxonomySession -site $taxonomySite
 $termStore = $taxonomySession.TermStores[$ManagedMetaDataServiceName]
 write-host “Connection made with term store -” $termStore.Name -ForegroundColor Green

 #create term groups
 CreateTermGroup -termStore  $termStore -termGroupName $grpConstructionProject -GUID $guidConsProject
 CreateTermGroup -termStore  $termStore -termGroupName $grpITProject -GUID $guidITProject

 #Create term sets
 CreateTermSets -termGroup $grpConstructionProject -termSet $termSetConsClient -termSetGuid $guidConsClient
 CreateTermSets -termGroup $grpITProject -termSet $termSetITClient -termSetGuid $guidITClient
 
   #Create terms for construction project - Client
    $getTermGroup = $termStore.Groups[$grpConstructionProject]
    $getTermSet = $getTermGroup.TermSets[$termSetConsClient]
    $getTermSet.CreateTerm("Apple Corp",1033)
    $getTermSet.CreateTerm("Commonwealth Bank",1033)
    $getTermSet.CreateTerm("Energy Australia",1033)
    $getTermSet.CreateTerm("Government of South Australia",1033)
    $getTermSet.CreateTerm("Sony Music",1033)
    $termStore.CommitAll()
    write-host “Successfully created terms for term set-” $termSetConsClient -ForegroundColor Green
   
    #Create terms for IT project - Client
    $getTermGroup2 = $termStore.Groups[$grpITProject]
    $getTermSet2 = $getTermGroup2.TermSets[$termSetITClient]
    $getTermSet2.CreateTerm("AGL",1033)
    $getTermSet2.CreateTerm("CUG",1033)
    $getTermSet2.CreateTerm("Commonwealth Bank",1033)      
    $termStore.CommitAll()
    write-host “Successfully created terms for term set-” $termSetITClient -ForegroundColor Green

        Stop-Transcript
    Echo Finish

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

cd /d %~dp0
powershell -file  ./psCreateTermSets.ps1 -SiteUrl "https://xxxx..com" -ManagedMetaDataServiceName "MMS_Proxy"
pause


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

- vamsi