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($
// “6.Create a new Crawl Component.”
New-SPEnterpriseSearchCrawlComponent -crawltopology $ct -crawldatabase $Crawlstore -searchserviceinstance $SearchServiceInstance
net stop sptimerv4
net start sptimerv4
$done = $false
$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($
$p1 | set-spenterprisesearchindexpartition -PropertyDatabase $PropDB
$done = $false
$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)


Leave a comment

This functionality is unavailable for field collections not associated with a list

For my scenario the above issue occurs when I try to add a field to the content type i.e. when i use ContentType.AddField.

I have solved the above issue by using SPFieldLink instead of SPField

Below is code for your reference

SPContentType cDocumen = newSPContentType(null,Web.ContentTypes,“MyDocument”);


Web.Fields.Add(“Autor”, SPFieldType.Text, false);

SPFieldLink SPFLautor = newSPFieldLink(miWeb.Fields[“Autor”]);ctDocumento.FieldLinks.Add(SPFLautor);



Ampersands Comparision Issue in a Taxonomy term

There are two types of ampersands that you need to be aware of when playing with SharePoint Taxonomy

Our favorite and most loved

& ASCII Number: 38

And the impostor

& ASCII Number: 65286

When you create a term it replaces the 38 ampersand with a 65286 ampersand.

This then becomes a problem if you want to do a comparison with your original source (spreadsheet, database, etc) as they are no longer the same.

As detailed in Nick’s article, you can use the TaxonomyItem.NormalizeName method to create a “Taxonomy” version of your string for comparison.

Below is the code I used in the SharePoint 2013 Client Component which is a little different from the server code.

string myString = “Current accounts & cards
using (var context = new ClientContext(“http://MyTestSite”))
var result = TaxonomyItem.NormalizeName(context, myString);

string normalisedString = result.Value;

In Server Object model it is TaxonomyItem.NormalizeName(myString);

Leave a comment

How to programmatically search for the Index Component in SharePoint 2013

At the company I work for, we are going to start working with synonyms in our Search results (we are still on SharePoint 2010). The synonyms will be maintained by our Content Editors team and therefore I decided to let them maintain the synonyms in the term store (Managed metadata service application), as it is done in SharePoint 2013. Every night a timer job will run to read the term store and to create the tsenu.xml file and to copy it to the \Microsoft Office Servers\14.0\Data\Office Server\ApplicationsGUID-query-0\config\tsenu.xml folder.

When our code goes in production, it goes through a DTAP process, meaning that our code needs to be deployed on dev, on test, on acceptance before it goes in production. So we have different SharePoint farms for the different environments. Although the SharePoint folders our always deployed to the same drive, the folder for the tsenu.xml can be different as one of the folders is the GUID of the Search Service Aplication. So I wanted that the timer job would figure out the correct folder using the Search topology.

SearchService s = new SearchService(“OSearch14”, SPFarm.Local);
if (s != null)
var srchApp = from SearchServiceApplication sapp in s.SearchApplications
where sapp.Name == “Enterprise Search Service Application”
select sapp;

SearchServiceApplication serviceApp = srchApp.FirstOrDefault();
if (serviceApp != null)
var queryComponent = serviceApp.QueryTopologies.ActiveTopology.QueryComponents.FirstOrDefault();
if (queryComponent != null)
string serverName = queryComponent.ServerName;
string directoryPath = queryComponent.IndexLocation;
string foldername = queryComponent.Name;

string configfoldername = directoryPath + “\\” + foldername + “\\Config”;

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


Adding Services to Default Application Proxy Group in SharePoint 2010 Using Powershell

$serviceAppProxy = Get-SPServiceApplicationProxy | where { $_.Name -eq “Name of the service” }
$proxygroup = Get-SPServiceApplicationProxyGroup | where { $_.FriendlyName -eq “[default]” }
Add-SPServiceApplicationProxyGroupMember -Identity $proxygroup -Member $serviceAppProxy