Bulk import documents from a network
Use a sample PowerShell script to add links for all documents in a network folder structure to Interact's search index.
Before you start
- A Workplace Search record in your Interact intranet. See Set up a custom connector.
- Basic understanding of PowerShell scripts, and PowerShell installed on your machine.
- The files to import must be available to the local PC and on your company network, so users can access them when selecting through from the search results.
Warning: This is provided as an example only. The documents are searchable within Interact, but the links do not work, because modern browsers prevent linking to documents using the
file://protocol.
How it works
The script below:
- Sets up the template for the JSON request to Interact.
- Loops through all the files in the specified location and its subfolders. It excludes temporary files by ignoring files that end in
.dbor start with~. - For each file, compiles the JSON data to send to Interact, setting the title, summary, and description to the file name, and the author to the original creator of the file.
- Sends the data to Interact to add the document to the search index.
How to run it
The script takes three parameters:
networkPath— the file path to the top-level folder containing the documents.uncPath— the file-based URL to the top-level folder, used when accessing the documents from the Interact search index.interactURI— the URL to your Interact installation.
To run it, cd into the folder that contains the script file and run:
./NetworkFolders.ps1 -networkPath "<File Path>" -uncPath "<Network 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
- This is not officially supported by Interact. It is a helper and example of what is possible with search apps.
- There is no error detection or data validation.
- There is no duplicate checking and no easy way of deleting documents.
- This works only as a bulk import. Adding new files to the file system does not add them to Interact.
- Edit the script to include your own secret key, which is added when setting up the search app within Interact.
- If the script is working, you get a success message on screen for each file imported.
The script
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'='<secret key from the search app>'; } -Body $fileJSON
}
}
Related
Section: Workplace Search