0

My scenario :

I have a SharePoint 2019 server that contain some documents with different file extension (ppt,doc,docx,xls,xlsx,pdf etc). I have manually forced indexing and crawling to make sure my search result are correct.

I was able to query SharePoint using the search API and what i got back was the file path of files containing the queried word and i can get the Hit-highlighted summary that is only a maximum of 10000 character.

What i have done is read that file and extract the paragraph where that queried word is found. I have been able to do it using file stream for docx,pdf,and txt file types.There 50 more file extension that i need to cater for.

My question : Is there another way to query the content of the search result returned by the search API instead of opening the individual file returned and reading the content?.

           Microsoft.SharePoint.Client.ClientResult<System.IO.Stream> stream = null;

            KeywordQuery keywordQuery = new KeywordQuery(clientContext);

            keywordQuery.QueryText = "SharePoint";

            keywordQuery.EnablePhonetic = true;

            keywordQuery.EnableOrderingHitHighlightedProperty = true;
            //keywordQuery.SummaryLength = 500;


            SearchExecutor searchExecutor = new SearchExecutor(clientContext);

            ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);

            clientContext.ExecuteQuery();

 foreach (var resultRow in results.Value[NumberOfRow].ResultRows)
            {
                Console.WriteLine("{0}: {1} ({2}) {3}", resultRow["Title"], resultRow["Path"], resultRow["Write"], resultRow["HitHighlightedSummary"]);

                File file = clientContext.Web.GetFileByUrl(resultRow["Path"].ToString());
                stream = file.OpenBinaryStream();
                clientContext.Load(file);
                clientContext.ExecuteQuery();


                if (resultRow["FileExtension"].ToString() == "pdf")
                {

                    Filebody = GetTextFromPDF(resultRow["Path"].ToString());
                }

                else if (resultRow["FileExtension"].ToString() == "docx" || resultRow["FileExtension"].ToString() == "doc")
                {
                    //use file Stream
                    byte[] byteArray = file.OpenBinary();

                    using (MemoryStream memStr = new MemoryStream())
                    {
                        memStr.Write(byteArray, 0, (int)byteArray.Length);

                        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(memStr, false))
                        {

                            WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream.Value, false);

                            body = wordprocessingDocument.MainDocumentPart.Document.Body;
                        }

                        Filebody = body.InnerText;
                    }
                }
  • 1
    Unfortunately no, Search results are returned from different source...SP can't afford to keep all content duplicated in search results DB...you would have to handled it by get file again and reading content... – Siddharth Vaghasia Jun 10 at 9:04
  • 1
  • @Fox i updated the code for reading doc files, the line file.OpenBinary doesn't seem to work here. Error : File does not contain a definition for OpenBinary – Keshav Taurah Jun 10 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.