Azure Search Service – Create a Data Source programmatically – C# Programmatically

Sathish Nadarajan
 
Solution Architect
February 14, 2019
 
Rate this article
 
Views
2488

Sometime back, we saw the entire process of Creating and using the Azure Search Service through the portal here. I would suggest to go through that article before further proceeding. This article, can be considered as the follow up. The same activities, let us do through programmatically.

Now, the Objective is to create the Index and do a query against that Index. Already to Query against the index has been covered in one of the earlier article.

Here, we will see how to Create a Data Source for the Search Indexer. To create an Indexer, we require the below items in sequence.

1. SkillSet

2. DataSource

3. Index

4. Indexer.

We will see them in sequence in the upcoming Articles. In this article, let us focus only on the creation of data source.

1. To Create a Data Source, as usual I have created a console application.

2. Add the below NuGet Package. “Microsoft.Azure.Search”

3. Go to the Azure Portal and make a note of the below items. Provided, I assume that, we have created a Azure Search Service and a Blob Storage and a Container in the Blob Storage.

4. Make a Note of the

a. Search Service Name

b. Search Query API Key – Make a note of either the primary or secondary admin keys. Both should work.

c. Blob Storage Connection string. To get this, go to the blob storage and the Access Keys as shown below.

d. Container Name. If we want only a folder inside the container, make a note of that as well.

5. Now, we got all the required parameters. With these parameters, the below code will create the Azure Search Service DataSource.

 namespace CS.Help.Console
 {
     using Microsoft.Azure.Search;
     using Microsoft.Azure.Search.Models;
 
     class Program
     {
         static void Main(string[] args)
         {
             CreateDataSource();
 
             System.Console.ReadLine();
         }
 
         private static void CreateDataSource()
         {
             //Assign the Search Service Name
             string searchServiceName = "SEARCH SERVICE NAME";
             string searchQueryApiKey = "KEY WHICH WE GOT FROM THE AZURE PORTAL";
 
             string dataSourceName = "DATASOURCE NAME";
 
             SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(searchQueryApiKey));
 
             bool isDataSourceExists = serviceClient.DataSources.Exists(dataSourceName);
 
             if (!isDataSourceExists)
             {
                 string storageConnectionString = "CONNECTION STRING WHICH WE GOT FROM THE PORTAL";
                 var ds = DataSource.AzureBlobStorage(dataSourceName, storageConnectionString, "palsbsearchcontainer");
                 DataSource dataSource = serviceClient.DataSources.CreateOrUpdate(ds);
                 System.Console.WriteLine("Data Source Created Successfully");
             }
             else
             {
                 System.Console.WriteLine("Data Source Already Exists");
             }
         }
     }
 }
 

6. After the execution, the data source will be created as below.

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment