Free Macro to Batch Relinquish All Mine from Multiple Revit Files

If you have gone through a process of saving Central models, and you have forgotten to ‘Synchronize with Central’ before closing them for the first time, you may find that your user has all User Created Worksets checked out in those files. A quick recap:

  • Workshared Revit files use a persons Revit user name (sometimes linked to an Autodesk SSO login) to determine if things are checked out
  • If someone has a User Workset checked out, you won’t be able to edit it until they Relinquish. (Note: you can Detach and recreate the file but that is dangerous if you have multiple people working on something)

Basically I had a bunch of Revit files that had all User Worksets from certain usernames checked out. They were upgraded and they were Central files. All that was needed was a simple Open and Relinquish. As I didn’t want to do this manually, I sourced some macro code from here and adapted it for my situation.

What does it do?

This Application level macro starts with a dialog box where you can select files. After you select them, it then loops over each file and Opens it, then does a Relinquish All Mine on User Created Worksets, and then it Syncs with Central and Closes the file. The key part of the code is here:

How to set it up?

First, get the code below. Copy and paste it into a new Application Macro in Revit.

 /*
  * Created by SharpDevelop.
  * User: lukes
  * Date: 1/10/2018
  * Time: 2:54 PM
  * 
  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  */
 using System;
 using Autodesk.Revit.UI;
 using Autodesk.Revit.DB;
 using Autodesk.Revit.UI.Selection;
 using System.Collections.Generic;
 using System.Linq;
 using Autodesk.Revit.ApplicationServices;
 using System.IO;
 using System.Windows.Forms;
 
 namespace relinquish
 {
     [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
     [Autodesk.Revit.DB.Macros.AddInId("30EBC375-5A4C-4917-AB07-D7212C9ED3FA")]
     public partial class ThisApplication
     {
         private void Module_Startup(object sender, EventArgs e)
         {
 
         }
 
         private void Module_Shutdown(object sender, EventArgs e)
         {
 
         }
 
         #region Revit Macros generated code
         private void InternalStartup()
         {
             this.Startup += new System.EventHandler(Module_Startup);
             this.Shutdown += new System.EventHandler(Module_Shutdown);
         }
         #endregion
         public void RelinquishMineFromFiles()
         {
             
             
 OpenFileDialog theDialogRevit = new OpenFileDialog();
 theDialogRevit.Title = "Select Revit Project Files";
 theDialogRevit.Filter = "RVT files|*.rvt";
 theDialogRevit.FilterIndex = 1;
 theDialogRevit.InitialDirectory = @"C:\";
 theDialogRevit.Multiselect = true;
 if (theDialogRevit.ShowDialog() == DialogResult.OK)    
             
     {             
 /* string mpath = "";
         string mpathOnlyFilename = "";
         FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
         folderBrowserDialog1.Description = "Select Folder Where Revit Projects to be Saved in Local";
         folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
         if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
         {
          mpath = folderBrowserDialog1.SelectedPath;*/
                 foreach (String projectPath in theDialogRevit.FileNames)
                 {
                  FileInfo filePath = new FileInfo(projectPath);
                         ModelPath mp = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath.FullName);
                         OpenOptions opt = new OpenOptions();
 /*                        opt.DetachFromCentralOption = DetachFromCentralOption.DetachAndDiscardWorksets;*/
                          WorksetConfiguration openConfig = new WorksetConfiguration(WorksetConfigurationOption.CloseAllWorksets);
                         // Set list of worksets for opening 
 /*                        openConfig.Open(worksetIds);
                         opt.SetOpenWorksetsConfiguration(openConfig);
                         mpathOnlyFilename = filePath.Name;*/
                         Document openedDoc = Application.OpenDocumentFile(mp, opt);                               
 /*                        SaveAsOptions options = new SaveAsOptions();*/
                          TransactWithCentralOptions twcOpts = new TransactWithCentralOptions();
                         SynchronizeWithCentralOptions syncopt = new SynchronizeWithCentralOptions();
                         RelinquishOptions rOptions = new RelinquishOptions(true);
                         rOptions.UserWorksets = true;
                         syncopt.SetRelinquishOptions(rOptions);
                         syncopt.SaveLocalBefore = false;
                         syncopt.SaveLocalAfter = false;
 /*                        options.OverwriteExistingFile = true;
                         ModelPath modelPathout = ModelPathUtils.ConvertUserVisiblePathToModelPath(mpath + "\\" + mpathOnlyFilename);
                         openedDoc.SaveAs(modelPathout, options);*/
                         openedDoc.SynchronizeWithCentral(twcOpts, syncopt);
                         openedDoc.Close(false);
                }
         }
 }}}

Then,

  • Add the System.Windows.Form reference and
  • Build the Solution

Note: I built and tested this on Revit 2018.2.

How to Use It?

  1. Set your Revit User Name to the user that you want to Relinquish the Worksets for…
    • You may have to logout of your own SSO first
    • Go to Revit Options
    • Input the exact user name (including @ if an email address)
  2. Start a new blank project in Revit
  3. Start the Macro Manager
  4. Select the RelinquishMineFromFiles macro that you built
  5. Click Run
  6. Select the files you want to fix
  7. Click Ok
  8. Wait for the result. The macro will step through them, Relinquish, Sync and Close the files.

 

Note:

Please use at your own risk, this has the potential to be pretty risky in a real project environment. Only use it if you understand what is going on 🙂

Also, you can refer to this Revit API Doc page.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments