vLogic to the Rescue – Assign Category per Folder

I created vLogic to be a useful tool in a variety of situations.  It allows for customization without the overhead of a full-blown .NET DLL.  A lot of people think vLogic is a good idea, but I’m not aware of it being use in any real capacity.  As we all know, there is sometimes a big gap between “good idea” and “useful”.  So I though I would spend a few posts taking a crack at some real-word problems using vLogic.

This week’s post will be addressing an entry on the Vault Idea Exchange.  A user wants to “Have a category automatically assigned to a document, based on the folder it is 'imported' to.  This would work only when there is a new document, or not another category, assigned to the document.”

Yes, I think I can do that with vLogic.


Requirements: 
Vault Workgroup/Collaboration/Professional 2013 and vLogic 2013

Steps: 

  1. Install vLogic if not installed already.
  2. Download the AddFilePost.SetToParentCategory.ps1 script and put it in the vLogic’s “Event Scripts” folder.  The folder can be found at %ProgramData%\Autodesk\Vault 2013\Extensions\vLogic\Event Scripts with Windows Explorer.
  3. If “Base” is not your default file category, open up the .ps1 script and edit the value for $BaseCategory.
  4. Start or restart Vault Explorer.
  5. Set up File and Folder categories with matching names for cases where you want the script to run.  This step is needed because categories can’t be shared across Files and Folders.
  6. Set a folder category and add a file to it through Vault Explorer.  If the file goes into the default category, the vLogic script will assign it to the file category with the same name as the folder’s category.

Notes:

  • The script only works for files added through Vault Explorer.
  • This script can only run on clients with vLogic and the .ps1 file.  Project Thunderdome can be used to deploy them both to multiple clients.

Source code:

Here is the source code.  As you can see, there is not a lot of code needed here.

# This script updates new files not assigned a category are updated to match the category on the parent folder.
# A manual refresh is needed to see the category change.
 
# Set this value to the display name of the "default" file category
$BaseCategory = "Base" 
 
if ($e.Status -eq [Autodesk.Connectivity.WebServices.EventStatus]::SUCCESS -and
    $e.ReturnValue.Cat -and
    $e.ReturnValue.Cat.CatName -eq $BaseCategory)
{
   $parentFolder = $serviceManager.DocumentService.GetFolderById($e.FolderId)

   if ($parentFolder.Cat)
   {
      $fileCats = $serviceManager.CategoryService.GetCategoriesByEntityClassId("FILE", $true)
      $fileCatId = $fileCats |
            Where-Object {$_.Name -eq $parentFolder.Cat.CatName} |
            Select-Object -ExpandProperty Id
     
      if ($fileCatId)
      {
        $temp = $serviceManager.DocumentServiceExtensions.UpdateFileCategories(@($e.ReturnValue.MasterId), @($fileCatId),
          "Setting category to match parent folder")
      }
   }
}


Comments

3 responses to “vLogic to the Rescue – Assign Category per Folder”

  1. Scott Moyse Avatar
    Scott Moyse

    Doug,
    This step will cause an error to be thrown when exporting the vault configuration from the ADMS console:
    “Set up File and Folder categories with matching names for cases where you want the script to run. This step is needed because categories can’t be shared across Files and Folders.”
    Its a bug within the database I believe, once that is fixed then there will be nothing wrong with this approach. Bloody good work otherwise, Cheers.
    Scott

  2. Scott Moyse Avatar
    Scott Moyse

    Maybe you could change your code to look for the same names between the two, but the folder rule could use F_ as a prefix or something to maintain unique category naming?
    The bug should be fixed of course, but this would be a semi-acceptable workaround.

  3. I was able to export just fine in my Vault. Is this an older defect? Maybe it was fixed in Vault 2013.
    Anyway, the nice thing about scripts is that it’s easy to make changes. To make your F_ change, just edit the PS1 file. Find the part that says
    Where-Object {$.Name -eq $parentFolder.Cat.CatName}
    And change it to
    Where-Object {$.Name -eq “F_” + $parentFolder.Cat.CatName}

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading