I am using the code below to read files from SharePoint 2019 server. It works perfectly for docx file extension but with doc files it throw an error. Can you guy s help me out here please?

 using (ClientContext clientContext = new ClientContext("http://sharepoint2019/sites/test/"))
            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);


            //sometime if the same query has been made several time,an automatic favorite query is generated so we need to check else the correct rows wont be returned
            if (results.Value.Count > 1)
                NumberOfRow = 1;
                NumberOfRow = 0;

            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();

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

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

                else if (resultRow["FileExtension"].ToString() == "docx" || resultRow["FileExtension"].ToString() == "doc")

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

                        body = wordprocessingDocument.MainDocumentPart.Document.Body;

                        Filebody = body.InnerText;

Error message:

'File contains corrupted data.' at line WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream.Value, false);

  • What part is failing? – James Randal Jun 12 at 7:45
  • It seems like You have lost some part of ending code - please provide full code. And agree with @JamesRandal - could You specify which part is failing and what error are You receiving? – Kristaps Vilerts Jun 12 at 8:19
  • @JamesRandal question updated – Keshav Taurah Jun 12 at 10:19
  • @KristapsVilerts Question updated – Keshav Taurah Jun 12 at 10:20

Based on the error message (and on command WordprocessingDocument) I assume You are trying to get content of Word documents.

Problem is following - DOCX type of documents are OpenXML based so they can be opened as an archive file (for example, with 7zip) and will contain a lot of XML files and relations. This article by Stepan Yakovenko has good cover on working with DOCX type files.

Contrary, DOC type documents are stored in binary, so content cannot be read by OpenXml libraries.

So the answer to question/problem - this code is meant to be used with DOCX type documents, but not for DOC type.

Update after comment request - look for "Microsoft Word interop" library. See this super simple example by Dotnetperls of how to get content of DOC type file.

  • can you kindly provide code that will open SharePoint doc file. – Keshav Taurah Jun 12 at 11:07
  • Updated my answer (and please consider my edit to Your question about error message formatting - it is not a simple text and not a regular sentence). – Kristaps Vilerts Jun 12 at 11:29
  • am getting the following error now : Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 – Keshav Taurah Jun 12 at 11:45
  • To use Microsoft Word interop , there is a need that office is installed on the server the code is running which is not the case for me – Keshav Taurah Jun 12 at 12:06
  • That is true - unfortunately, I do not see any other way (besides Microsoft Word interop) how to read DOC file content. – Kristaps Vilerts Jun 12 at 12:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.