Change Element Workset

We discussed reading the workset of an element, either using the built-in parameter ELEM_PARTITION_PARAM or, more easily, the dedicated document GetWorksetId method.

This is actually another example of the possibility to choose your data access method by using either a dedicated property or a parameter, as we recently explored to achieve changing the viewport type.

Although the Revit API provides a dedicated property to read an element workset, it does not do so for changing it, which begs the question:

Question: The Element.WorksetId property is read-only.

I would like to change an element's workset to other one, though.

Is this possible?

Answer: You can change the element's workset via the built-in parameter ELEM_PARTITION_PARAM.

Here is a code snippet to show how to retrieve all workset ids and set each one to a given element in turn:

  Reference r = uidoc.Selection.PickObject( ObjectType.Element );
  Element e = doc.GetElement( r.ElementId );
  if( e == null )
  WorksetId wid = e.WorksetId;
  TaskDialog.Show( "Workset id", wid.ToString() );
  Parameter wsparam = e.get_Parameter(
    BuiltInParameter.ELEM_PARTITION_PARAM );
  if( wsparam == null )
  // Find all user worksets 
  FilteredWorksetCollector worksets
    = new FilteredWorksetCollector( doc )
      .OfKind( WorksetKind.UserWorkset );
  using( Transaction tx = new Transaction( doc ) )
    tx.Start( "Change workset id" );
    foreach( Workset ws in worksets )
      wsparam.Set( ws.Id.IntegerValue );
  wid = e.WorksetId;
  TaskDialog.Show( "worksetid", wid.ToString() );

Many thanks to Phil Xia for this hint!