manoj´ssharepointblog

SharePoint Blog


Leave a comment

Creating Search Service Application using PowerShell In SharePoint

It might be necessary at some point to use PowerShell to provision search service applications. Below is the script which can be used for creating a Search Service Application With comments

Below is the Power Shell Script which I used for the creation Search Service Application

$searchapp = Get-SPEnterpriseSearchServiceApplication -Identity $SearchServiceApplicationName -ErrorAction silentlycontinue
//Check if the SearchService Application Already exists
if($searchapp -eq $null)
{
//For the content Source after the creation of the search service application
$contentSourceURls = “URL1″,”URL2″,”URL3″,”URL4”;
// Name of Application pool
$ManagedMetaDataAppPool =”MySearchServiceApplicationPoolAccount”
$ManagedAccount = Get-SPManagedAccount | select -First 1
// 1. Creating a an application pool for your search service application and throwing the object into a variable called $ app:
$ApplicationPool = Get-SPServiceApplicationPool $ManagedMetaDataAppPool -ErrorAction SilentlyContinue
if ($ApplicationPool -eq $null) {
$ApplicationPool = New-SPServiceApplicationPool -Name $ManagedMetaDataAppPool -Account $ManagedAccount
}
// “2. Creating search Service.”
$searchapp = New-SPEnterpriseSearchServiceApplication -name $SearchServiceApplicationName -applicationpool $ApplicationPool
//Note: Add the -partitioned switch after -name if the search service application will be consumed in a hosted environment.
// “3. Creating Search Service proxy.”
$proxy = New-SPEnterpriseSearchServiceApplicationProxy -name $SearchServiceApplicationName -Uri $searchapp.uri.absoluteURI
$si = Get-SPEnterpriseSearchServiceInstance –local
// “4. Start Services search services for SSI”
Start-SpEnterpriseSearchServiceInstance -identity $si
Set-SPEnterpriseSearchAdministrationComponent –searchapplication $searchapp –searchserviceinstance $si
// “5.Create a new Crawl Topology.”
// By default, a search application created in PowerShell has a crawl topology but is missing the following:
// · crawl component
// · query component
// You cannot add a crawl\query component to the default crawl\query topology because it’s set as active and the property is read // only. The easiest way around this is creating a new crawl topology and new query topology. After creating both, they will be // set as inactive by default. This allows for both crawl components to be added to crawl topology and query component to be added // to newly created query topology. Finally, you can set this new crawl topology to active.
$ct = $searchapp | New-SPEnterpriseSearchCrawlTopology
$csid = $SearchApp.CrawlStores | select id
$CrawlStore = $SearchApp.CrawlStores.item($csid.id)
// “6.Create a new Crawl Component.”
New-SPEnterpriseSearchCrawlComponent -crawltopology $ct -crawldatabase $Crawlstore -searchserviceinstance $SearchServiceInstance
net stop sptimerv4
net start sptimerv4
$done = $false
do
{
$ct | Set-SPEnterpriseSearchCrawlTopology -Active -ErrorVariable $err
if ($ct.State -eq “Active”)
{
$done = $true
}
Start-Sleep -Seconds 10
Write-Host $ct.State
}
until ($done -eq $true)
// “7. Create a new Query Topology.”
$qt = $searchapp | new-spenterprisesearchquerytopology -partitions 1
$p1 = ($qt | get-spenterprisesearchindexpartition)
// “8. Create a Query Component.”
new-spenterprisesearchquerycomponent -indexpartition $p1 -querytopology $qt -searchserviceinstance $si
$PSID = $SearchApp.PropertyStores | Select id
$PropDB = $SearchApp.PropertyStores.Item($PSID.id)
$p1 | set-spenterprisesearchindexpartition -PropertyDatabase $PropDB
$done = $false
do
{
$qt | Set-SPEnterpriseSearchQueryTopology -Active -ErrorVariable $err -ErrorAction SilentlyContinue
if ($qt.State -eq “Active”)
{
$done = $true
}
Start-Sleep -Seconds 10
Write-Host $qt.State
}
until ($done -eq $true)
// “9. Adding the content source urls.”
$contentSource = New-SPEnterpriseSearchCrawlContentSource -Name “My ContentSource” -SearchApplication $searchapp -Type SharePoint -SharePointCrawlBehavior “CrawlSites”
foreach ($contentSourceURl in $contentSourceURls)
{
$contentSource.StartAddresses.Add($($Url.Trim(“/”)+$contentSourceURl))
}
}

Advertisements


Leave a comment

How to Programmatically find Search Service Application in SharePoint 2013

We found a lot of posts that use the GetDefaultProxy method to get to the Search Service Application but in our case this piece of code always returned our first Search Service Application.

SPServiceContext serviceContext = SPServiceContext.GetContext(site);
SearchServiceApplicationProxy searchProxy =
serviceContext.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
if (searchProxy != null)
{
SearchServiceApplicationInfo searchServiceAppInfo = searchProxy.GetSearchServiceApplicationInfo();
SearchServiceApplication searchServiceApp = SearchService.Service.SearchApplications.GetValue(
searchServiceAppInfo.SearchServiceApplicationId);
Schema searchSchema = new Schema(searchServiceApp);
}

Finally we came out with the following piece of code to get to the correct Search Service Application:

SPServiceContext serviceContext = SPServiceContext.GetContext(site);
SearchService searchService = new SearchService(“OSearch14”, SPFarm.Local);

var srchApp = from SearchServiceApplication sapp in searchService.SearchApplications
where sapp.Name == “Our other search service application”
select sapp;

SearchServiceApplication searchServiceApp = srchApp.First();
searchSchema = new Schema(searchApplication);This does not seemed to be the best way to get to a service application and after some time of research I found out that you can also write it like this, which seems a better way to me:

searchApplication = (SearchServiceApplication)SPFarm.Local.Services.
GetValue().
Applications.GetValue(“Our other search service application”);
It took us several hours to find this out, but I hope that these snippets can save someone a few hours in the future.