Setting the Phase of a View

Here is a quick summary of one of the many issues being discussed on the Revit API forum, on changing the phase of a view, with a quick and happy conclusive result:

Question: Is it possible to change the phase of a view? Something like view.phase?

I've tried with the created phase but it doesn't work.

Thank for your help!

Answer: Does this old discussion on creating a room on a level in a phase help?

Please look specifically at the reply to most recent comment by Jared.

Response: I tried what you explain on the blog but I don't know why it doesn't work:

  View active = commandData.Application
    .ActiveUIDocument.ActiveGraphicalView;

  foreach( Phase ii in phase )
  {
    Parameter p = active.get_Parameter(
      BuiltInParameter.VIEW_PHASE );

    ElementId iiId = ii.Id;
    p.SetValueString( iiId );
  }

Answer: How is your variable 'phase' defined?

Have you checked what storage type the built-in parameter VIEW_PHASE is expecting?

You can use RevitLookup to explore it.

Whatever storage type it is expecting, I am pretty sure that you cannot set it using SetValueString.

I would expect the storage type to be ElementId, in which case you need to set it using the element id directly, e.g. like this:

  p.Set( iiId );

I hope this helps.

Response: Yeah it works!

phase is defined like this: PhaseArray phase = doc.Phases;

Here is the working code to set it:

  ElementId iiId = ii.Id;
 
  Parameter p = active.get_Parameter(
    BuiltInParameter.VIEW_PHASE );
 
  p.Set( iiId );

with:

Thank a lot!