Control VisualSVN Server from C#

Go To StackoverFlow.com

8

I installed VisualSVN Server 2.5.4. I can create user and repository. My question is how can I create/delete user/repository from C#. Is there any library?

2012-04-04 18:37
by Rased Dot Net


12

Update 03/09/2015

There is no need to write custom WMI scripts anymore; PowerShell cmdlets available beginning with VisualSVN Server 3.4 cover the most Subversion server administration and repositories management use cases. Read about the new feature at https://www.visualsvn.com/server/features/powershell/

VisualSVN Server 3.4 introduces PowerShell module that provides you with a number of helpful cmdlets. The cmdlets can be used for administering VisualSVN Server and its repositories either locally or remotely. Here is the complete reference on VisualSVN Server PowerShell cmdlets.

For example,

  • You can create a new repository MySuperRepo by running this PowerShell command:

    New-SvnReposiory MySuperRepo

  • You can create a project structure in the repository

    New-SvnRepositoryItem MySuperRepo -Path /branches, /tags, /trunk -Type Folder

  • You can provide DOMAIN\Developers Active Directory group account with Read / Write access to the new repository

    Add-SvnAccessRule MyRepo -Path / -AccountName DOMAIN\Developers -Access ReadWrite

  • You can calculate the size the repository takes on disk:

    Measure-SvnRepository MySuperRepo

  • You can verify the repository for corruptions:

    Test-SvnRepository MySuperRepo

    And much, much more!

For more information and the complete list of cmdlets, read the article VisualSVN Server PowerShell Cmdlet Reference.


VisualSVN Server can be managed via WMI (Windows Management Instrumentation) interface.

MOF file which describes the VisualSVN Server interface resides in the %VISUALSVN_SERVER%\WMI on the computer where VisualSVN Server is installed. Using this file as a reference you can write a C# script to manage VisualSVN Server.

Please check the MSDN article: http://msdn.microsoft.com/en-us/library/bb404655

I'm including the following samples for your reference:

  • This C# code will create a Subversion user 'user1' with password 'secret'.

        ManagementClass userClass = new ManagementClass("root\\VisualSVN", "VisualSVN_User", null);
    
        // Obtain in-parameters for the method
        ManagementBaseObject inParams =
            userClass.GetMethodParameters("Create");
    
        // Add the input parameters.
        inParams["Name"] = "user1";
        inParams["Password"] = "secret";
    
        // Execute the method and obtain the return values.
        ManagementBaseObject outParams =
            userClass.InvokeMethod("Create", inParams, null);
    
  • This C# code will create a new repository 'Repo1'.

        ManagementClass repoClass = new ManagementClass("root\\VisualSVN", "VisualSVN_Repository", null);
    
        // Obtain in-parameters for the method
        ManagementBaseObject inParams =
            repoClass.GetMethodParameters("Create");
    
        // Add the input parameters.
        inParams["Name"] = "Repo1";
    
        // Execute the method and obtain the return values.
        ManagementBaseObject outParams =
            repoClass.InvokeMethod("Create", inParams, null);
    
  • This C# code will provide SID S-1-5-32-545 ('BUILTIN\Users') with Read / Write access to repository 'Test'. FYI: The AccessLevel values are as described in the MOF: "0 - no access, 1 - read only, 2 - read/write".

    ManagementClass userClass = new ManagementClass("root\\VisualSVN", "VisualSVN_WindowsAccount", null);                            
    ManagementClass authzClass = new ManagementClass("root\\VisualSVN", "VisualSVN_SecurityDescriptor", null);
    ManagementClass permClass = new ManagementClass("root\\VisualSVN", "VisualSVN_PermissionEntry", null);
    
    ManagementObject userObject = userClass.CreateInstance();
    userObject.SetPropertyValue("SID", "S-1-5-32-545");
    
    ManagementObject permObject = permClass.CreateInstance();
    permObject.SetPropertyValue("Account", userObject);
    permObject.SetPropertyValue("AccessLevel", 2);
    
    ManagementObject repo = new ManagementObject("VisualSVN_Repository.Name='Test'");
    
    ManagementBaseObject inParams =
        authzClass.GetMethodParameters("SetSecurity");
    
    inParams["Object"] = repo;
    inParams["Permissions"] = new object[] { permObject };
    
    ManagementBaseObject outParams =
        authzClass.InvokeMethod("SetSecurity", inParams, null);
    

Updated on 02/10/2013:

WMI schema has been changed (and improved!) in VisualSVN Server 2.6. In short, to set access permissions on a repository path, you are required to:

  • create VisualSVN_Repository class object specifying repository name,
  • create VisualSVN_PermissionEntry entry object specifying account username and access permissions,
  • invoke SetSecurity method on VisualSVN_Repository passing valid repository path and PermissionEntry object.

        ManagementClass userClass = new ManagementClass("root\\VisualSVN", "VisualSVN_WindowsAccount", null);
        ManagementClass permClass = new ManagementClass("root\\VisualSVN", "VisualSVN_PermissionEntry", null);
        ManagementClass repoClass = new ManagementClass("root\\VisualSVN", "VisualSVN_Repository", null);
    
        ManagementObject userObject = userClass.CreateInstance();
        userObject.SetPropertyValue("SID", "S-1-5-32-545");
    
        ManagementObject permObject = permClass.CreateInstance();
        permObject.SetPropertyValue("Account", userObject);
        permObject.SetPropertyValue("AccessLevel", 2);
    
        ManagementObject repoObject = repoClass.CreateInstance();
        repoObject.SetPropertyValue("Name", "MyProject");
    
        ManagementBaseObject inParams =
            repoClass.GetMethodParameters("SetSecurity");
    
        inParams["Path"] = "/trunk";
        inParams["Permissions"] = new object[] { permObject };
    
        ManagementBaseObject outParams =
            repoObject.InvokeMethod("SetSecurity", inParams, null);
    
2012-04-05 13:57
by bahrep
Thanks a lot. This is exactly what I am looking for. One thing is missing. how to map user and repository - Rased Dot Net 2012-04-06 18:43
@RasedDotNet: What do you mean with 'map user and repository' - Ivan Zhakov 2012-04-07 07:35
@IvanZhakov: setup permission for a user so that s/he can only access to (one or more) specific repositories with read only/read-write permissio - Rased Dot Net 2012-04-07 07:56
@RasedDotNet I've added another C# code sample which shows how to set repository access permission to a Windows account (used 'BUILTIN\Users' SID as an example) - bahrep 2012-04-10 13:33
http://groups.google.com/forum/#!msg/visualsvn/0NTgU8XzbUo/NxlOcgGZqyUJ has a copy of this answer. From your profile I'm assuming you're the original author, so did Visual SVN Team copy your answer unattributed? ; - mafu 2012-10-16 10:14
@mafutrct The e-mail you refer to is mine (see origin of the e-mail) :) I'm VisualSVN support technician - bahrep 2012-10-16 11:25
Haha, nice, I almost thought so : - mafu 2012-10-16 14:42
@bahrep, how to get the Repo URL in C - Ramesh 2013-08-14 07:23
That code maps nicely to PowerShell if you load in System.Management.dll. Thanks, you've saved me a lot of time - sean_m 2013-12-24 18:33
@bahrep Subquestion, how would you append a security entry in PowerShell? When I attempt to set more than one value to the "Permissions" parameter by using the += operator, it throws an implicit cast exception. I can post the code in a new question if that would help - sean_m 2013-12-25 01:21
Figured it out, code here: https://gist.github.com/sean-m/952290 - sean_m 2014-03-13 06:41
Ads