I’m picking up on this thread which was looking at updating or fixing the data-shapes node used to add shared parameters from a txt file. The version in the code in this thread works and gets around the BuiltInParameterGroup issue that came with the changes in the Revit 2025 API. However it doesn’t allow you to parse an external Shared Parameter file, it will only use the shared parameter file currently loaded into Revit. Regardless of how I try to parse this file, I consistently get a System.Data.OleDb error from my Python code.
Can anyone shed any light on why this is happening and if there is an easy way around it?
Help with adding shared parameters to project! - Revit - Dynamo
Sorry, how do I post code on here? I don’t seem to have a preformatted text button… </>?
below is the code I use to get shared parameters from a text file 
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
# Input: shared parameter file path from Dynamo
shared_param_file_path = IN[0]
# Get the current document and application
doc = DocumentManager.Instance.CurrentDBDocument
app = doc.Application
# Start a transaction
TransactionManager.Instance.EnsureInTransaction(doc)
# Set the shared parameter file path and open it
app.SharedParametersFilename = shared_param_file_path
spfile = app.OpenSharedParameterFile()
# Retrieve parameter groups and definitions
groups = spfile.Groups
definitions = [g.Definitions for g in groups]
definitions_flat = [x for l in definitions for x in l]
definitions_flat_names = [x.Name for x in definitions_flat]
# End the transaction
TransactionManager.Instance.TransactionTaskDone()
# Output: definitions and their names
OUT = definitions_flat, definitions_flat_names
import clr
import traceback
import System
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
# Get the current Revit document and application
doc = DocumentManager.Instance.CurrentDBDocument
app = doc.Application
# Inputs
parameter_names = IN[0] # List of parameter names (strings)
parameter_category_input = IN[1] # Revit category objects or Dynamo wrappers
parameter_group_name = IN[2] # String or list of strings (e.g., "General", "Dimensions")
is_instance = IN[3] # Boolean for instance or type parameter
external_param_file_path = IN[4] # New input: Path to the external shared parameter file
# If parameter_group_name is passed as a list, use the first element
if isinstance(parameter_group_name, list):
parameter_group_name = parameter_group_name[0]
# Fetch all group IDs and names
from Autodesk.Revit.DB import ParameterUtils, LabelUtils
# Get group type IDs and names
group_ids = ParameterUtils.GetAllBuiltInGroups()
group_names = [LabelUtils.GetLabelForGroup(g) for g in group_ids]
# Function to convert parameter group name to BuiltInParameterGroup
def get_parameter_group_id(group_name):
if group_name in group_names:
group_index = group_names.index(group_name)
return group_ids[group_index]
return None
# Validate and convert the parameter group name
parameter_group_id = get_parameter_group_id(parameter_group_name)
if not parameter_group_id:
raise ValueError(f"Invalid parameter group name: {parameter_group_name}. Available groups: {', '.join(group_names)}")
# Process categories
try:
categories = [UnwrapElement(cat) for cat in parameter_category_input]
except:
raise ValueError("Invalid categories provided.")
# Create CategorySet for binding
catset = app.Create.NewCategorySet()
for cat in categories:
catset.Insert(cat)
# Save the current shared parameter file path
original_param_file_path = app.SharedParametersFilename
# Validate and set the external shared parameter file
if not external_param_file_path or not System.IO.File.Exists(external_param_file_path):
raise ValueError(f"External shared parameter file not found: {external_param_file_path}")
try:
# Set the external shared parameter file
app.SharedParametersFilename = external_param_file_path
# Open the external shared parameter file
shared_param_file = app.OpenSharedParameterFile()
if shared_param_file is None:
raise ValueError(f"Failed to open external shared parameter file: {external_param_file_path}")
# Start the transaction
TransactionManager.Instance.EnsureInTransaction(doc)
# Add parameters
added_params = 0
failed_params = []
for param_name in parameter_names:
try:
# Refresh the bindmap in each loop iteration
bindmap = doc.ParameterBindings
# Check if the parameter already exists in the document
exists = False
iterator = bindmap.ReverseIterator()
while iterator.MoveNext():
if iterator.Key.Name == param_name:
exists = True
break
if exists:
print(f"Parameter '{param_name}' already exists, skipping.")
continue
# Look for the parameter in the shared parameter file
param_def = None
for group in shared_param_file.Groups:
param_def = group.Definitions.get_Item(param_name)
if param_def:
break
if param_def is None:
failed_params.append(f"{param_name}: Not found in shared parameter file.")
continue
# Re-create the binding for each parameter
bind = app.Create.NewInstanceBinding(catset) if is_instance else app.Create.NewTypeBinding(catset)
# Attempt to bind the parameter
if bindmap.Insert(param_def, bind, parameter_group_id):
added_params += 1
else:
failed_params.append(f"{param_name}: Failed to bind parameter.")
except Exception as e:
failed_params.append(f"{param_name}: {str(e)}")
# Commit the transaction
TransactionManager.Instance.TransactionTaskDone()
except Exception as e:
# Ensure transaction is closed in case of error
TransactionManager.Instance.ForceCloseTransaction()
raise ValueError(f"Error processing parameters: {str(e)}\nStack Trace: {traceback.format_exc()}")
finally:
# Revert to the original shared parameter file
if original_param_file_path:
app.SharedParametersFilename = original_param_file_path
else:
app.SharedParametersFilename = ""
# Output results
if failed_params:
OUT = f"{added_params} shared parameters added. Failed to add: {', '.join(failed_params)}"
else:
OUT = f"{added_params} shared parameters added to the project."
Try this, it temporarily switch the project’s shared parameter file to the one selected.
2 Likes
This is the best way to manage this, in Python, C#, or Dynamo.
2 Likes
Thanks, much appreciated. I’ll give that a go this afternoon.
1 Like