Opening a Workshared Document

Opening a Workshared Document

The Application.OpenDocumentFile(ModelPath, OpenOptions) method can be used to set options related to opening a workshared document. In addition to options to detach from the central document or to allow a local file to be opened ReadOnly by a user other than its owner, options may also be set related to worksets. When a workshared document is opened, all system worksets are automatically opened, however user-created worksets can be specified to be opened or closed. Elements in an open workset can be expanded and displayed. However, elements in a closed workset are not displayed to avoid expanding them. By only opening worksets necessary in the current session, Revit's memory footprint is reduced, which can help with performance.

In the example below, a document is opened with two worksets specified to be opened. Note that the WorksharingUtils.GetUserWorksetInfo() method can be used to access workset information from a closed Revit document.

Code Region: Open Workshared Document

void OpenDocumentWithWorksets(Application app, ModelPath projectPath)
{
    try
    {
        // Get info on all the user worksets in the project prior to opening
        IList<WorksetPreview> worksets = WorksharingUtils.GetUserWorksetInfo(projectPath);
        IList<WorksetId> worksetIds = new List<WorksetId>();
        // Find two predetermined worksets
        foreach (WorksetPreview worksetPrev in worksets)
        {
            if (worksetPrev.Name.CompareTo("Workset1") == 0 ||
                worksetPrev.Name.CompareTo("Workset2") == 0)
            {
                worksetIds.Add(worksetPrev.Id);
            }
        }

        OpenOptions openOpts = new OpenOptions();
        WorksetConfiguration openConfig = new WorksetConfiguration();
        // Set to close worksets by default
        openConfig.CloseAll();
        // Set list of worksets for opening
        openConfig.Open(worksetIds);
        openOpts.SetOpenWorksetsConfiguration(openConfig);
        Document doc = app.OpenDocumentFile(projectPath, openOpts);
    }
    catch (Exception e)
    {
        TaskDialog.Show("Open File Failed", e.Message);
    }
}