#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

How To: Import Files From Local Drive And Create Versions Using PowerShell

I recently had someone ask, how do you create a version of a document within ProjectWise while importing files from Windows, if a document already exists with the file name?

This turns out to be a multi-step process if a document already exists within ProjectWise. First you have to create a new version of the ProjectWise document and then update the physical file associated with the document. If the file does not exist within ProjectWise, create a new document.

The cmdlets we will focus on are:

  • New-PWDocument
  • New-PWDocumentVersion
  • Update-PWDocumentFile

First things first. Take a look at the help for the three cmdlets mentioned to familiarize yourself with the available parameters, etc.

Get-Help New-PWDocument -Full
Get-Help New-PWDocumentVersion -Full
Get-Help Update-PWDocumentFile -Full

This New-PWDocument cmdlet has a few required parameters that we will need to deal with.

  • InputFolders or FolderPath
  • FilePath

The New-PWDocument cmdlet has a few more optional parameters available. However, we will not be using them.


This New-PWDocumentVersion cmdlet has only one required parameter that we will need to deal with.

  • InputDocument

The New-PWDocumentVersion cmdlet has one optional parameters available. However, we will not be using them.


This Update-PWDocumentFile cmdlet has a few required parameters that we will need to deal with.

  • InputDocuments
  • NewFilePath

The Update-PWDocumentFile cmdlet has a few more optional parameters available. However, we will not be using them.


Now, we will create and populate a few variables with the ProjectWise Folder Path which is where we want to create our new documents or versions. The source folder path which is the local folder containing the files to use to create the new documents or versions. And a filter to use when selecting documents from the local folder.

# Path to ProjectWise Folder to create documents in.
$PWFolderPath = 'PowerShell\FromLocalDrive'

# Local folder containing files use to create new ProjectWise documents.
$SourceFolderPath = 'D:\temp'

# Filter used with the Get-ChildItem cmdlet.
$FileFilter = '*.dgn'

Next, we will use the Get-PWFolders cmdlet to select the ProjectWise folder object to create the new documents or versions in.

# Get ProjectWise Folder object for specified folder path.
$PWFolder = Get-PWFolders -FolderPath $PWFolderPath -JustOne -Verbose

The following is the contents of the ProjectWise folder prior to processing the files.

newdoc2

Next, we will use the Get-ChildItem cmdlet to select the files to create the new documents or versions while filtering on ‘*.dgn’ file types.

# Import document information from spreadsheet or select from a local folder.
$PWData = Get-ChildItem -Path $SourceFolderPath -Filter $FileFilter -File -Verbose

The following are the files we are starting with.

newdoc1

Create an arraylist to store the ProjectWise document objects for the new documents or versions.

# Array to store the new document objects in.
$NewDocObjects = New-Object System.Collections.ArrayList

In the following snippet, you will see that we are looping through each of the items returned from the Get-ChildItem cmdlet.

First, we are using the Get-PWDocumentsBySearch cmdlet to determine if a document already exists within the target ProjectWise folder.

If a document exists, we need to create a new version of the document and then replace the associated physical file with the current file in the loop. To do this we use the New-PWDocumentVersion cmdlet to create the new ProjectWise document version. We are letting ProjectWise assign the Version designation, however we could have specified what to use by passing a value using the -Version parameter. The new ProjectWise document object is added to the NewDocObjects arraylist.

We then pipe the new ProjectWise document object to the Update-PWDocumentFile cmdlet to replace the associated physical file.

If a document does not exist, we simply need to use the New-PWDocument cmdlet to create a new document within the target ProjectWise folder.  We could have specified the description, file name, etc. using the corresponding parameters if we wanted to.

# Loop thru each file and create a new ProjectWise document or a document version.
foreach($PWFile in $PWData) {
    try { 
        # Determine if current file exists within the specified ProjectWise folder.
        $Splat = @{
            FolderPath = $PWFolderPath
            FileName = $PWFile.Name
        }
        $PWDocName = Get-PWDocumentsBySearch @Splat -JustThisFolder -Verbose -ErrorAction Stop

        # Create a new version of the document. Then update the associated physical file.
        $NewDocObjects.Add( (New-PWDocumentVersion -InputDocument $PWDocName -Verbose |
        Update-PWDocumentFile -NewFilePathName $PWFile.FullName -Verbose ) )
    } catch { 
        # Create new document.
        $NewDocObjects.Add( ( New-PWDocument -FilePath $PWFile.FullName -InputFolders $PWFolder -Verbose ) ) 
    } # end try/catch
} # end foreach($PWFile...

Results

The following is the contents of the ProjectWise folder after processing the files.

Two new documents were created and one version was created for the document that existed in the folder prior to processing the files.

newdoc3

The document objects were saved to the arraylist.

# View contents of results array.
$NewDocObjects | Select-Object Name, Version

newdoc4

 

Experiment with it and have fun.

Hopefully, you find this useful. Please let me know if you have any questions or comments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.