New file inherits from template

2015-03-27_15-31-52

Recently, we received an interesting question about Data Standard. In Vault, it’s possible to create new files based on templates. The question was whether it’s possible to inherit the category and maybe also some properties from the template file into the new file dialog. Obviously, the answer is YES!

The first thing we have to do is to hook the SelectionChanged event from the template combobox. In this case, the combobox is bound to a fixed Data Standard property called SelectedTemplate, which is not accessible through the Prop array. So, we will hook directly the selection changed event from the combobox, called TemplateCB. We do this inside the InitializeWindow function, like this:

function InitializeWindow
{
 ..
 .....

 $dsWindow.FindName("TemplateCB").add_SelectionChanged({
  OnTemplateChanged
 })
}

Every time, the user selects a template, our OnTemplateChanged will be fired. Inside such function, we will take the file information from the selected template file and assign it to our dialog.

As the selected template path and template name are sort of internal properties, we will retrieve these information directly from the DataContext of our dialog.

$context = $dsWindow.DataContext
$path = $context.TemplatePath
$file = $context.SelectedTemplate

Now that we know which template the user has selected from which folder, via the Vault API we can get the file information. The following functions can be placed either in a separate PS1 file or somewhere in the default.ps1.

function OnTemplateChanged
{
 $context = $dsWindow.DataContext
 $path = $context.TemplatePath
 $file = $context.SelectedTemplate
 $template = $path + "/" + $file
 $folder = $vault.DocumentService.GetFolderByPath($path)
 $files = $vault.DocumentService.GetLatestFilesByFolderId($folder.Id,$false)
 $file = $files | Where-Object { $_.Name -eq $file }
 $Prop["_Category"].Value = $file.Cat.CatName

 $properties = GetFileProperties -fileId $file.Id
 $Prop["Title"].Value = $properties["Title"]
}

At the end we can set the current category with the category of the template file. In the last two lines we set also the title with the same value as the template, so in case your templates have some default values, they can be brought over to the new file.

Now, as the file object does not expose the properties, we need to pick them via the Vault API. For this purpose, I created another small function that retrieves the properties and returns an array with property name and value.

function GetFileProperties($fileId)
{
 $global:propDefs = $vault.PropertyService.GetPropertyDefinitionsByEntityClassId("FILE")
 $props = $vault.PropertyService.GetPropertiesByEntityIds("FILE",@($fileId))
 $properties = @{}
 foreach ($prop in $props) {
  $propDef = $global:propDefs | Where-Object { $_.Id -eq $prop.PropDefId }
  $properties[$propDef.DispName] = $Prop.Val
 }
 return $properties
}

You will notice that when you select a template now, the dialog takes a second or so to update. The reason is that collecting all the properties from the template is time consuming and delays the dialog refresh. If you comments the two lines

#$properties = GetFileProperties -fileId $file.Id
#$Prop["Title"].Value = $properties["Title"]

You will notice that the dialog is again reactive.

So, now it’s up to you to define which information shall be copied from the template into your new file.

Have fun!

This entry was posted in Data Standard. Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s