Worksets

Worksets are a way to divide a set of elements in the Revit document into subsets for worksharing. There may be one or many worksets in a document.

Accessing worksets in the document

The document contains a WorksetTable which is a table containing references to all the worksets contained in that document. There is one WorksetTable for each document. There will be at least one default workset in the table, even if worksharing has not been enabled in the document. The Document.IsWorkshared property can be used to determine if worksharing has been enabled in the document. The WorksetTable class can be used to get the active workset (as shown in the example below) and to set the active workset, by calling SetActiveWorksetId()

Code Region: Get Active Workset

public Workset GetActiveWorkset(Document doc)
{
     // Get the workset table from the document
     WorksetTable worksetTable = doc.GetWorksetTable();
     // Get the Id of the active workset
     WorksetId activeId = worksetTable.GetActiveWorksetId();
     // Find the workset with that Id
     Workset workset = worksetTable.GetWorkset(activeId);
     return workset;
}

Filtering worksets

Since the Workset class is not derived from Element, use FilteredWorksetCollector to search, filter and iterate through a set of worksets. Conditions can be assigned to filter the worksets that are returned. If no condition is applied, this filter will access all of the worksets in the document. The WorksetKind enumerator is useful for filtering worksets as shown in the next example. The WorksetKind identifies the subdivision of worksets:
  • User- user managed worksets for 3D instance elements
  • Family- where family symbols & families are kept
  • Standard- where project standards live including system family types
  • Other- internally used worksets which should not typically be considered by applications
  • View- contain views and view-specific elements

Code Region: Filtering Worksets

public void GetWorksetsInfo(Document doc)
{
    String message = String.Empty;
    // Enumerating worksets in a document and getting basic information for each
    FilteredWorksetCollector collector = new FilteredWorksetCollector(doc);

    // find all user worksets
    collector.OfKind(WorksetKind.UserWorkset);
    IList<Workset> worksets = collector.ToWorksets();

    // get information for each workset
    int count = 3; // show info for 3 worksets only
    foreach (Workset workset in worksets)
    {
        message += "Workset : " + workset.Name;
        message += "\nUnique Id : " + workset.UniqueId;
        message += "\nOwner : " + workset.Owner;
        message += "\nKind : " + workset.Kind;
        message += "\nIs default : " + workset.IsDefaultWorkset;
        message += "\nIs editable : " + workset.IsEditable;
        message += "\nIs open : " + workset.IsOpen;
        message += "\nIs visible by default : " + workset.IsVisibleByDefault;

        TaskDialog.Show("GetWorksetsInfo", message);

        if (0 == --count)
            break;
    }
}

Workset properties

The Workset class represents a workset in a Revit document. As is shown in the filtering worksets example above, the Workset class provides many properties to get information about a given workset, such as the owner and whether or not the workset is editable. These properties are read-only. To change the name of an existing workset, use the static method WorksetTable.RenameWorkset().

Creating worksets

The static Workset.Create() method can be used to create a new workset in a given document with a specified name. Worksets can only be created in a document that has worksharing enabled and the name must be unique. The static method WorksetTable.IsWorksetNameUnique() will confirm if a given name is unique in the document. The following example demonstrates how to create a new workset.

Code Region: Create a new workset

public Workset CreateWorkset(Document document)
{
    Workset newWorkset = null;
    // Worksets can only be created in a document with worksharing enabled
    if (document.IsWorkshared)
    {
        string worksetName = "New Workset";
        // Workset name must not be in use by another workset
        if (WorksetTable.IsWorksetNameUnique(document, worksetName))
        {
            using (Transaction worksetTransaction = new Transaction(document, "Set preview view id"))
            {
                worksetTransaction.Start();
                newWorkset = Workset.Create(document, worksetName);
                worksetTransaction.Commit();
            }
        }
    }

    return newWorkset;
}