1

I would like to read the content of XML files in SharePoint Online. I would like to check if the XML file contains some text. I already tried something like this but it is not working:

Connect-PnPOnline https://myCompany.sharepoint.com/teams/SomeSiteCollection $fileRelativeURL = "/SomeDocumentLibrary/test.xml" $file = Get-PnPFile -Url $fileRelativeURL -AsListItem $stream = $file.OpenBinaryStream() 

But the stream is empty and has no value.

enter image description here

How to get the content of a file in PowerShell and SharePoint Online?

3
  • Try this and let me know if it works for you.CommentedJun 17, 2020 at 9:48
  • Hi @GaneshSanap, thank you. This article is SharePoint on prem. I would like to run a powershell script to SharePoint Online.
    – Jackson
    CommentedJun 17, 2020 at 9:55
  • 1
    You can get the XML content as a string using PnP PowerShell in SP Online. check my answer below.CommentedJun 17, 2020 at 9:58

1 Answer 1

1

To get the XML file content as a string in a variable, use below code:

Connect-PnPOnline -Url https://tenant.sharepoint.com/sites/SiteName -UseWebLogin $fileRelativeURL = "/sites/SiteName/Shared Documents/test.xml" $file = Get-PnPFile -Url $fileRelativeURL -AsString 

Documentation: Get-PnPFile - Examples

Then you can check if the text is present in XML file string or not using:

$textToFind = "SomeTextFromFile" $index = $file.indexOf($textToFind) 

If $index is equal to -1 then the substring ($textToFind) is not found in XML file content Else it is found.

2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.