How to create search query content source in SharePoint using PowerShell

They are called Content Sources in the graphical user interface of SharePoint and Search Query Content Source when using PowerShell. A content source is basically pointing to a source of data that can be indexed in the search database. Because the data is indexed, when later users run queries the result is returned almost instantaneous. It is very easy to add a content source by using SharePoint Central Administration, but you might need to create a long list of content sources in a batch for example as part of a disaster recovery process or many other reasons. Here is one way I do it in a PowerShell script.

$contentSourceConfig = @(
        Name = "Articles"
        StartAddresses = "http://mydomain1/site1/fr,http://mydomain1/site1/nl,http://mydomain1/site1/en"
        Name = "Technical"
        StartAddresses = "http://mydomain1/site2/fr,http://mydomain1/site2/nl,http://mydomain1/site2/en"

foreach($config in $contentSourceConfig) {
    $contentSourceName = $config.Name
    $startAddresses = $config.StartAddresses
    $contentSource = Get-SPEnterpriseSearchCrawlContentSource $contentSourceName -SearchApplication $ssa -ErrorAction SilentlyContinue
    if ($contentSource) { $contentSource.Delete() }
    $contentSource = New-SPEnterpriseSearchCrawlContentSource $contentSourceName -Type "Web" -StartAddresses $startAddresses -SearchApplication $ssa
    Write-Host "Search crawl content source: $contentSourceName created successfully."

As you see in the above example I have put all the required configuration in one array of custom objects. If you need to add more content sources you may simply copy-past one and change the properties. This way the configuration is separated and easier to read or change while still it is in the same file with the script so you won’t have to rely on extra XML file(s) to put beside the script.

If you are more comfortable by separating the configuration from the script. You can cut the first part and past it to another script file or use the XML approach.

For more details about the cmdlets I’ve used I suggest you have a look on the following MSDN articles:

BCS Part 1 – What can I build and what is the best way to do that?

If you already know SharePoint and probably experienced developing application in this platform you already know that when thinking about a new solution and all the requirements you’re going to meet in your project, picking the right tools and finding the best way to build the right solution is not always easy. When thinking about maintenance costs, upgradability and deeper integration with SharePoint, solutions that make better use of out the shelf tools win most of the time. Even with the introduction of Apps in SharePoint 2013, you can still benefit a lot from using Business Connectivity Services (aka BCS). Apart from the range of features and tools that are provided under the name of BCS (e.g. WebParts, integration with Secure Store Service, Office client applications) one of the main and sometimes overlooked areas is how BCS and Search can work together. If you enable search over the entities of your external system, you can surface data coming from it through search and you can even customise search pages to create totally different user experience (e.g. I remember a shopping cart solution that might be a subject for a future blog post).

Instead of going through all the possible solutions, in this post I’m going to provide you with some links I have gathered from around the web that I think are essential if one needs to make real word solutions with these technologies. In the future posts I will go into the details and on cover the missing documents that you will need to make solid solutions.

First of all the basics (please note that REST, ODATA and some other features are not available out-of-the-box in SP2010):

And then read carefully the followings to get to know Search interoperability with BCS:

Don’t worry if there are terms that you don’t understand completely, I provide some examples and behind the scene information so you can understand them well.

How to change OSearch14 account (user)?

How to change search service account using SharePoint Central Administration

The simplest way to change the account of a service in SharePoint is to:

  1. Run Central Administrator
  2. Go to Security > Configure service accounts
  3. Select “Windows Service – SharePoint Server Search”
  4. In “Select an account for this component” select the account that you want and if it is not in the list, click “Register new managed account” and add it easily.

How to change search service account using PowerShell

But sometimes it’s not as easy as above to change a service account in SharePoint. For example you have installed SharePoint on a server for development whiout adding “Active Directory Domain Services” server role. In that case you can only add Managed Accounts through PowerShell and even though it won’t be displayed on managed account list in Central Administration and you can only use PowerShell to assign them to different services.

Step1 – Here is how to add a managed account in PowerShell:

First, go to the Start menu, then “Microsoft SharePoint 2010 Products” and run SharePoint 2010 Management Shell” which will run PowerShell and adds SharePoint Snapin so you can run SharePoit’s cmdlets. Now you can type the following commands:

$cred = Get-Credential  "Username"
New-SPManagedAccount -Credential $cred

Step 2 – How to assign it to OSearch14 (Enterprise Search):

$account = Get-SPManagedAccount -Identity "Username"
$procId = (Get-SPEnterpriseSearchService).get_ProcessIdentity()
$procId.CurrentIdentityType = "SpecificUser"
$procId.ManagedAccount = $account

Remember that the last line is important beacuse you won’t see any changes until this step. It actually restarts the service with the recently assigned account.