web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / trying to obtain numbe...
Power Automate
Unanswered

trying to obtain number of times a word appears in SharePoint

(0) ShareShare
ReportReport
Posted on by 23

Hi,

 

I have a flow, which is taking input from PowerApps, via a input text box, allowing a user to search for a word, this is then triggering a flow, which calls a REST Api to SharePoint. I am able to obtain the files inside a document library which contain the searched word in the documents.

I am trying to enhance the flow to be able to obtain how many times a word appears in each document and this is where I need help please, below is my flow, and its failing in different stages:

Screenshot 2024-01-26 121948.png

Categories:
  • Chriddle Profile Picture
    8,729 Super User 2026 Season 2 on at

    This certainly needs some enhancements, but it might be a start.

    1. Remove special characters such as ".", "!", "?" that may immediately follow a word (this must be done with several other characters such as ",", ";", ")" etc.)
    2. lowercase the text
    3. Split each file content into a word array
    4. Count the occurrences of the search term using xpath

     

    Chriddle_0-1706285653655.png

    Map file:

    item()['file']

    Map content:

    item()['content']

    Map words (not really needed, but here you may check if the words are splitted correctly):

    split(
    	toLower(
    		replace(
    			replace(
    				replace(
    					replace(
    						item()['content'],
    						'.',
    						''
    					),
    					'?',
    					''
    				),
    				':',
    				''
    			),
    			'!',
    			''
    		)
    	),
    	' '
    )

    Map count:

    xpath(
    	xml(json(concat('{"Root":{"Item":',
    		split(
    			toLower(
    				replace(
    					replace(
    						replace(
    							replace(
    								item()['content'],
    								'.',
    								''
    							),
    							'?',
    							''
    						),
    						':',
    						''
    					),
    					'!',
    					''
    				)
    			),
    			' '
    		),
    		'}}'))
    	),
    	concat('count(//Item[.="', toLower(outputs('SearchWord')),'"])')
    )

     Result:

    Spoiler (Highlight to read)
    [
     {
     "file": "file1",
     "content": "The file content of file 1.",
     "words": [
     "the",
     "file",
     "content",
     "of",
     "file",
     "1"
     ],
     "count": 1
     },
     {
     "file": "file2",
     "content": "Another file content, I's of file 2?",
     "words": [
     "another",
     "file",
     "content,",
     "i's",
     "of",
     "file",
     "2"
     ],
     "count": 0
     },
     {
     "file": "file3",
     "content": "A third file content: File 3 also has content!",
     "words": [
     "a",
     "third",
     "file",
     "content",
     "file",
     "3",
     "also",
     "has",
     "content"
     ],
     "count": 2
     },
     {
     "file": "file4",
     "content": "Content of file 4",
     "words": [
     "content",
     "of",
     "file",
     "4"
     ],
     "count": 1
     }
    ]
    [ { "file": "file1", "content": "The file content of file 1.", "words": [ "the", "file", "content", "of", "file", "1" ], "count": 1 }, { "file": "file2", "content": "Another file content, I's of file 2?", "words": [ "another", "file", "content,", "i's", "of", "file", "2" ], "count": 0 }, { "file": "file3", "content": "A third file content: File 3 also has content!", "words": [ "a", "third", "file", "content", "file", "3", "also", "has", "content" ], "count": 2 }, { "file": "file4", "content": "Content of file 4", "words": [ "content", "of", "file", "4" ], "count": 1 } ]
  • PAppsAlways1 Profile Picture
    23 on at

     

    Taking it to another level there wow, I'll need steps to see what you have done and used for each expression.

    For starters I've never used xpath....

     

     

  • Chriddle Profile Picture
    8,729 Super User 2026 Season 2 on at

    If  the search term does not need to match the whole word, you can use this xpath query: 

     

    xpath(
    	xml(json(concat('{"Root":{"Item":',
    		split(
    			toLower(
    				item()['content']
    			),
    			' '
    		),
    		'}}'))
    	),
    	concat('count(//Item[contains(.,"', toLower(outputs('SearchWord')),'")])')
    )

     

    This saves annoying replaces

     

  • Chriddle Profile Picture
    8,729 Super User 2026 Season 2 on at

    And if you only expect a few occurrences you can use an expression like this:

    if(
    	less(
    		nthIndexOf(toLower(item()['content']), toLower(outputs('Searchword')), 1),
    		0
    	),
    	0,
    	if(
    		less(
    			nthIndexOf(toLower(item()['content']), toLower(outputs('Searchword')), 2),
    			0
    		),
    		1,
    		if(
    			less(
    				nthIndexOf(toLower(item()['content']), toLower(outputs('Searchword')), 3),
    				0
    			),
    			2,
    			if(
    				less(
    					nthIndexOf(toLower(item()['content']), toLower(outputs('Searchword')), 4),
    					0
    				),
    				3,
    				'>3'
    			)
    		)
    	)
    )
  • PAppsAlways1 Profile Picture
    23 on at

    @Chriddle 
    There is no way for me to know how many times the word will appear, so I wouldn't want to limit the search 

  • Chriddle Profile Picture
    8,729 Super User 2026 Season 2 on at

    As you can see, this flow expects a Compose action with an array of objects with properties file and content.

    So you have to prepare your data 😉

  • PAppsAlways1 Profile Picture
    23 on at

    tbh, that is where I have been struggling, I cannot get my flow to function after it has got the files back, i've created a 2nd HTTP Get request (Send a HTTP request to SharePoint 2) and after that step, the subsequent steps just fail for different reasons, I don't think I am close to using what you have suggested in my flow yet, as I don't think I'm that far ahead

  • Chriddle Profile Picture
    8,729 Super User 2026 Season 2 on at

    But maybe the exact amount does not matter if there are more than e.g. 3 occurences 😉

  • Chriddle Profile Picture
    8,729 Super User 2026 Season 2 on at

    What errors do you get, and why do you use an API request and not just the "Get files" action?

  • PAppsAlways1 Profile Picture
    23 on at

    Unable to process template language expressions in action 'Compose_6' inputs at line '0' and column '0': 'The template language function 'int' was invoked with a parameter that is not valid. The value cannot be converted to the target type.'.

     

    I've pointed it o a single file, rather than to search through all files in a document library, but the full flow errors

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Automate

#1
Mohsin Ali Profile Picture

Mohsin Ali 430

#2
11manish Profile Picture

11manish 341 Super User 2026 Season 2

#3
M Bilal Khan Profile Picture

M Bilal Khan 282

Last 30 days Overall leaderboard