It looks like you may be using code more suited to pyRevit.
Post your full code including imports.
Ensure you surround the entire code block with triple backticks (```) so that it displays properly
or paste the code, select/highlight all the code and hit the code format </> in the menu
Thank you for your reply. I’m reposting my code below—could you please share any advice on how to fix the issue?
You’re correct: I usually write code for pyRevit. However, in this case I need to use an Excel file as an input, which is challenging with pyRevit. Unfortunately, my company does not allow us to install additional Python dependencies such as NumPy or Pandas to read Excel files.
Python can read CSV just fine, so you can skip excel. That said I’m not a fan on the Python 2 dependencies in that other tool - they’re a massive security risk.
I did a quick pass at a definition that attempts to make worksets by names here which should work just fine in the default Python engine from 2024 to 2027 (all supported builds). It might give you some ideas on the root cause as well as how to circumvent your issue.
########################################
############## Properties ##############
########################################
__author__ = 'Jacob Small'
__version__ = '0.1.0'
__description__ = "Generate worksets from a provided list of names."
__RevitBuilds__ = "2024, 2027"
__DynamoBuilds__ = "2.19, 4.0"
__ReleaseNotes__ = "POC for educational purposes only"
__Dependancies__ = "None"
__Copyright__ = "2026, Autodesk Inc."
__License__ = "Apache 2"
########################################
### Configure the Python environment ###
########################################
### standard imports ###
import sys #add the sys class to the Python environment so we can work with the sys objects
import clr #add the CLR (common language runtime) class to the Python environment so we can work with .net libraries
### Dynamo Revit imports ###
clr.AddReference("RevitNodes") #add Dynamo's Revit nodes library to the clr
import Revit #import Dynamo's Revit node class
clr.ImportExtensions(Revit.Elements) #add the element conversion methods to the CLR
clr.AddReference("RevitServices") #add the Revit services library to the CLR
import RevitServices #import the Revit services class to the Python environment
from RevitServices.Persistence import DocumentManager #import the document manager class to the Python environment
from RevitServices.Transactions import TransactionManager #import the transaction manager class to the Python environment
### Revit API imports ###
clr.AddReference("RevitAPI") #add the Revit API to the CLR
import Autodesk #add the Autodesk class to the Python environment
from Autodesk.Revit.DB import * #import every class of the Revit API to the Python environment
#########################################
######## Classes and Definitions ########
#########################################
def GenerateWorksets(doc, names):
if doc.IsWorkshared: #if the document is workshared
errors = [] #set error to a empty list
results = [] #set results to an empty list
newWorksetNames = [i for i in names if WorksetTable.IsWorksetNameUnique(doc, i)] #get the new workset names by filter out names not unique
unusedNames = [i for i in names if not i in newWorksetNames] #get the list of used names by taking thsoe which were not unique
if newWorksetNames: #if there are new workset names
transaction = Transaction(doc, "Generate Worksets") #create a transaction to make the worksets
transaction.Start() #start the transaction
for name in newWorksetNames: #for each name
try: #try
workset = Workset.Create(doc, name) #create the workset
results.append(workset) #append the workset to the results list
except: #if the try fails
import traceback #import traceback
msg = traceback.format_exc() #format the exception
errors.append([name, msg]) #return the name that failed and the exception
transaction.Commit() #commit the transaciton
else: #if all names were in use
errors.append("All workset names are already in use. Provide new list or use the ones you already have.") #append a helpful message to the errors list
return {"Results":results, "Errors": errors}
else: #if the document wasn't workshared
sys.exit("\r\rDocument is not workshared. Enable worksharing and try again.\r\r") #close the python engine with an error message instructing the user on what to do
#########################################
###### Global variables and inputs ######
#########################################
doc = DocumentManager.Instance.CurrentDBDocument #the current Revit document
proviedWorksetNames = IN[0] #the list of workset names from IN[0] of the Dynamo environment
if not proviedWorksetNames.__class__ == list: proviedWorksetNames = [proviedWorksetNames] #ensure that workset names is a list
#########################################
###### Code execution starts here #######
#########################################
TransactionManager.Instance.ForceCloseTransaction() #force the transactions to close incase that's part of the issue
newWorksets = GenerateWorksets(doc, proviedWorksetNames) #try to generate the worksets
#########################################
##### Return the results to Dynamo ######
#########################################
OUT = newWorksets #return the new worksets to the Dynamo environment