How to Execute KeyWord Search in Office 365 SharePoint 2013 site using CSOM

In this post, we’ll see how we can programmatically execute Search Queries for Keywords using SharePoint 2013 Client Side Object Model (CSOM).

Create a Console Application in Visual Studio 2013 and name it as KeyWordSearch

Add a reference to Microsoft.SharePoint.Client.dll

image

Add a reference to Microsoft.SharePoint.Client.Search.dll and it is available in the location C:Program  FilesCommon FilesMicrosoft Sharedweb server extensions15ISAPI

image

Add a reference to Microsoft.SharePoint.Client.Runtime.dll

When I fire a Search Query using the keyword ‘Sundar’ in online portal, it gives the below results.

image

We’ll attempt the same by invoking the SearchQuery using manged .NET Client Side Object Model (CSOM). We’ll be leveraging SearchExecutor class and KeyWordQuery class in the Microsoft.SharePoint.Client.Search and Microsoft.SharePoint.Client.Search.Query namespaces and invoking ExecuteQuery method to get the results back.

Replace the Program.cs with the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Search;
using Microsoft.SharePoint.Client.Search.Query;

namespace ExecuteKeywordSearch
{
    class Program
    {
        static void Main(string[] args)
        {
            //Assign User Id for your SharePoint Online tenant    
            string UserName = “youruserid@yoursite.onmicrosoft.com”;

            //Assign password for your SharePoint online tenant
            string Password = “yourpassword”;

            //Create a SecureString object from password string, needed for SharePointOnlineCredentials class
            SecureString SecurePassword = GetSecureString(Password);

            using (var oClientContext = new ClientContext(“https://yoursite.sharepoint.com/”))
            {
                //assign SharePoint Online Credentials to ClientContext Class
                oClientContext.Credentials = new SharePointOnlineCredentials(UserName, SecurePassword);

                //Create an instance of KeywordQuery Class
                KeywordQuery oKeyWordQuery = new KeywordQuery(oClientContext);
               
                //Assign the Search Query
                oKeyWordQuery.QueryText = “Sundar”;

                //Associate SearchExecutor with ClientContext
                SearchExecutor oSearchExecutor = new SearchExecutor(oClientContext);
               
                //Execute the Search Query
                ClientResult<ResultTableCollection> oResultTables = oSearchExecutor.ExecuteQuery(oKeyWordQuery);

                oClientContext.ExecuteQuery();

                foreach (var oResultRow in oResultTables.Value[0].ResultRows)
                {
                    Console.WriteLine(oResultRow[“Title”].ToString());
                    Console.WriteLine(“n” + oResultRow[“Path”].ToString());

                }

 

                Console.ReadLine();

            }

        }

        private static SecureString GetSecureString(String Password)
        {
            SecureString oSecurePassword = new SecureString();

            foreach (Char c in Password.ToCharArray())
            {
                oSecurePassword.AppendChar(c);

            }
            return oSecurePassword;
        }

    }
}

When we execute the above piece of code, we’ll see the following search results that matches with screenshot (manual search) illustrated above.

image

 

 Subscribe to my blog

Configuring MOSS 2007 to search pdf documents – install and configure pdf ifilters

There is a requirement to index and search PDF documents inside MOSS 2007.  The first thing that comes to my mind is “IFilters”.  Then I need to choose between the V6 and V8.1 of the IFilters. The advantage of Acrobat Reader V8.1 reader is that it has IFilters inside. 

I did not choose V8.1 . Because I don’t have special requirement to deal with WDS search. So I’ve chosen IFilters V6. Here are steps that I followed to install the pdf IFilters

a)Download Adobe PDF IFilters 6.0

b)Start –>Run–>> Services.msc and stop the IIS Admin service. The reason we need to do this is, the IFilters configuration needs an update to DOCICON.XML.

c)Run the IFilters installer on the Indexing Server

d) Download the pdf icon image pdficon of size 17X17 from the following location and save it as “icpdf.gif” somewhere to the local hard disk

http://www.adobe.com/misc/linking.html

d)Copy the icpdf.gif to the following location

C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12TemplateImages”

e) Go the C:Program FilesCommon FilesMicrosoft SharedWeb server extensions12TemplateXml

f) Edit the DOCICON.XML by adding the following entry

<Mapping Key=”pdf” Value=”icpdf.gif”/>

g) Perform an IIS RESET

h) Go to Search Settings under Central Administration and add the pdf as the new file type

g) Perform the crawl again

Now MOSS is supposed to search pdf documents properly. This is what said in so many Articles and Blogs. It did not work for me that easily. After some analysis, I’ve learnt that I need to apply a Microsoft Hot fix to make this work for MOSS 2007. The Microsoft Hot fix mentions the following steps to be performed to make the pdf search work for MOSS 2007

1. Add the following registry entry, and then set the registry entry value to pdf:

HKEY_LOCAL_MACHINESOFTWAREMicrosoftShared ToolsWeb Server Extensions12.0SearchApplications<GUID>GatherSearchExtensionsExtensionList38

To do this, follow these steps: a. Click Start, click Run, type regedit, and then click OK.

b. Locate and then click the following registry subkey:

HKEY_LOCAL_MACHINESOFTWAREMicrosoftShared ToolsWeb Server Extensions12.0SearchApplicationsGUIDGatherSearchExtensionsExtensionList

c. On the Edit menu, point to New, and then click String Value.

d. Type 38, and then press ENTER.

e. Right-click the registry entry that you created, and then click Modify.

f. In the Value data box, type pdf, and then click OK.

2. Verify that the following two registry subkeys are present and that they contain the appropriate values.

Note these registry subkeys and the values that they contain are created when you installed the Adobe PDF IFilter on the server.

• HKEY_LOCAL_MACHINESOFTWAREMicrosoftShared ToolsWeb Server Extensions12.0SearchSetupContentIndexCommonFiltersExtension.pdf

This registry subkey must contain the following registry entry:• Name: Default

Type: REG_MULTI_SZ

Data: {4C904448-74A9-11D0-AF6E-00C04FD8DC02}

  • HKEY_LOCAL_MACHINESOFTWAREMicrosoftShared ToolsWeb Server Extensions12.0SearchSetupFilters.pdf

This registry subkey must contain the following registry entries:

• Name: Default

Type: REG_SZ

Data: (value not set)

• Name: Extension

Type: REG_SZ

Data: pdf

• Name: FileTypeBucket

Type: REG_DWORD

Data: 0x00000001 (1)

• Name: MimeTypes

Type: REG_SZ

Data: application/pdf

3. Upload the PDF documents to the Windows SharePoint Services 3.0 Web site.

a. Stop and then start the Windows SharePoint Services Search service. To do this, follow these steps:a. Click Start, click Run, type cmd, and then click OK.

b. Stop the Windows SharePoint Services Search service. To do this, type net stop spsearch at the command prompt, and then press ENTER.

c. Start the Windows SharePoint Services Search service. To do this, type net start spsearch at the command prompt, and then press ENTER.

d. Type exit to exit the command prompt.

Now MOSS is ready to search pdf documents

 Subscribe to my post