How to bulk import documents from a network

This page includes a sample powershell script that will navigate through a folder structure and add links to all the documents it finds to interact

❗️

This is provided as an example only, and whilst the documents would be searchable from within Interact, the links will not work because modern browsers prevent the linking to documents using the file:// protocol

You will need

  1. An Enterprise Search record in your interact intranet. Instructions for this can be found How to set up Enterprise Search
  2. Some basic understanding of PowerShell scripts and PowerShell software installed on your machine
  3. The files to be imported need to be available to the local PC and also available on your company network for users when clicking through from the search results

How it works

The script below works in the following way

  • Sets up the template for the JSON request to interact

  • Loops through all the files in the specified location and the sub folders. This then excludes temporary files by ignoring files that end in .db or start with a ~

  • For each file the script compiles the JSON data to send to Interact by setting the title, summary and description to the file name, and by setting the author to be the original creator of the file

  • The script then sends the data to Interact to add the document to the search index.

How to run it

  • The script takes 3 parameters
    1. networkPath - The file path to the top level folder containing the documents
    2. uncPath - The file based URL to the top level folder for use when accessing the documents from the Interact search index
    3. interactURI - The url to your Interact installation

To run the application cd into the folder that contains the script file and execute the following command

./NetworkFolders.ps1 -networkPath "<File Path>" -uncPath "<Netowrk Path>" -interactURI "<URL to the search app on your intranet>

for example

./NetworkFolders.ps1 -networkPath "C:\Files\" -uncPath "//REDCAR/c$/Files" -interactURI "http://demo.interact.com/api/searchapp/1/document”

Things to consider

If you're using this script you need to bear in mind the following things

  • This is not officially supported by Interact, it is intended as a helper and example to what is possible with search apps
  • There is no error detection or data validation
  • When adding files this way there's no checking for duplicates or easy way of deleting documents
  • This will only work as a bulk import, adding new files to the file system will not also add them to Interact
  • You'll need to edit the script to include your own API Key which is added when setting up the search app within Interact
  • If the script is working you should get a success message on screen for each file that is imported

The script

Here it is...

Param(
	[string]$networkPath,
    [string]$uncPath,
    [string]$interactURI
)

$templateJSON = "{
  ""Url"": ""file:" + $uncPath + "/{filepath}"",
  ""Id"": ""{id}"",
  ""Title"": ""{filename}"",
  ""IsPublic"": ""true"",
  ""Body"": ""{filename}"",
  ""summary"": ""{filename}"",
  ""Author"": ""{author}"",
}"

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$files = Get-ChildItem $networkPath -Recurse -File -exclude "*.db ~*"

for ($i=0; $i -lt $files.Count; $i++) {

    if($files[$i].Length){

      $urlPath = $files[$i].FullName        
      $urlPath = $urlPath.Replace($networkPath, "")
      $urlPath = $urlPath.Replace("\", "/")

      $author = (Get-Acl $files[$i].FullName).Owner
      $author = $author.Split("\")[1]

      $fileJSON = $templateJSON 
      $fileJSON = $fileJSON.Replace("{filename}", $files[$i].Name) 
      $fileJSON = $fileJSON.Replace("{id}", $i) 
      $fileJSON = $fileJSON.Replace("{filepath}", $urlPath)
      $fileJSON = $fileJSON.Replace("{author}", $author)
        
      Invoke-RestMethod -Uri $interactURI -Method Put -ContentType "application/json" -Headers @{'X-ApiKey'='<API Key from Search Apps>'; } -Body $fileJSON
        
    }
    
}