New folder with subfolders

2015-02-20_00-16-46Some months ago, we made this post regarding creating a folder and automatically copying the subfolder tree from a template. In that post, the folder tree was inside Vault. We received several comments asking whether it would be possible to hold the folder tree outside Vault, for instance in an XML file. This post will show you how to do it.

In an XML file we define which templates are available and how the folder structure looks like. In this case the XML file is called Templates.xml and looks like this

<?xml version="1.0" encoding="utf-8"?>
<Templates>
<Template Name="Production">
   <Folder Name="Folder1" Category="Project">
     <Folder Name="SubFolder1" Category="Folder">
       <Folder Name="SubSubFolder1" Category="Base" />
     </Folder>
     <Folder Name="SubFolder2" Category="Project" />
   </Folder>
   <Folder Name="Folder2" Category="Project"></Folder>
   <Folder Name="Folder3" Category="Project"></Folder>
</Template>
<Template Name="Design">
   <Folder Name="Folder1" Category="Base">
     <Folder Name="SubFolder1" Category="Base">
       <Folder Name="SubSubFolder1" Category="Base" />
     </Folder>
     <Folder Name="SubFolder2" Category="Base" />
   </Folder>
   <Folder Name="Folder2" Category="Base"></Folder>
</Template>
<Template Name="Marketing">
   <Folder Name="Folder1">
     <Folder Name="SubFolder1" Category="Base">
       <Folder Name="SubSubFolder1" Category="Base" />
     </Folder>
     <Folder Name="SubFolder2" Category="Base" />
   </Folder>
   <Folder Name="Folder2" Category="Base"></Folder>
</Template>
</Templates>

As you can see, the first level of the XML contains the templates with an attribute name, while each template contains a nested folder tree structure with folder Name and folder Category.

The XAML file for the folder creation (Folder.xaml), will be extended by a combobox which reads the template names from the XML file. In order to do this, we add a resource in the MainWindow.Resources section of the XAML, like this

<XmlDataProvider x:Key="templates" Source="C:\ProgramData\Autodesk\Vault 2015 R2\Extensions\DataStandard\Vault\Configuration\FolderTemplates.xml" XPath="//Template"/>

later in the combobox, we can point to this resource and display the attribute name in the list, like this:

<ComboBox ItemsSource="{Binding Source={StaticResource templates}}" DisplayMemberPath="@Name" SelectedValuePath="@Name" SelectedValue="{Binding Prop[Template].Value}" IsEnabled="{Binding CreateMode}" Grid.Row="4" Grid.Column="1"/>

As you can see, we store the selected template in a custom property called Template, so that after the folder creation, we can identify the selected template and create the subfolders.

The PowerShell code is also fairly short. In the Menu/CreateFolder.ps1 we have a recursive function that goes into each level of the XML file and creates the according subfolders.

function recursivelyCreateFolders($childFolders, $newFolder,$folderCategories)
{
      if($childFolders -eq $null) { return }
      foreach ($folder in $childFolders) {
            $newSubFolder = $vault.DocumentServiceExtensions.AddFolderWithCategory($folder.Name, $newFolder.Id, $false, $folderCategories[$folder.Category])
            recursivelyCreateFolders -childFolders $folder.ChildNodes -newFolder $newSubFolder -folderCategories $folderCategories
      }
}

Later in the same file, we will retrieve the selected template, load the XML file with the template definitions, also load the category definitions as we will need them later and then start the recursion of above.

$template = $dialog.ViewModel.Prop["Template"].Value
if($template -ne "")
{
   [xml]$xml = Get-Content "C:\ProgramData\Autodesk\Vault 2015 R2\Extensions\DataStandard\Vault\Configuration\FolderTemplates.xml"
   $folderTree = Select-Xml -Xml $xml -XPath "//Template[@Name='$template']"
   $folderCats = $vault.CategoryService.GetCategoriesByEntityClassId("FLDR",$true)
   $fc = @{}
   $folderCats | ForEach-Object { $fc[$_.Name]=$_.Id}
   recursivelyCreateFolders -childFolders $folderTree.Node.ChildNodes -newFolder $newFolder -folderCategories $fc
}

That’s it. In summary, we have an XML that contains the folder template definitions, a combobox for selecting the template and some lines of PowerShell code in order to create subfolders based on the XML definitions. Now the user can create a new folder and pick a template. The new created folder will have the subfolders as defined in the XML file.

Here you can download the ZIP file with the complete code.

Have fun!

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

5 Responses to New folder with subfolders

  1. Lukas says:

    Hi,
    i cannot make it work. Vault creates only top folder but not subfolders. I have checked all details in ps1 and xaml file. I work with Vault 2016.

  2. Prem says:

    Hi
    I could make the dialog box come in for Vault 2017, but i dont find any categories or template attached to this, where am i going wrong

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