Add a Web application by code
Tags: beginners, tips, ensemble, api
Published: 2022-02-25 18:48:34

Hi community,

I'm going to publish several articles on how to perform actions that you can do in the web portal but via code.

Today.... Web Applications via code<!--break-->

## Introduction 

If you want to add a new Web application, you you usually do it with the option System Administration - Security - Applications - Web Applications


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1bieyb6p4kjc2bs7xrcf.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1t0cljgu318lfc4kotfj.png)

Then add the roles to use in the application


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7q0xxtnopo07kssamh9n.png)

 

But, if you have not access to the management portal, you can do it by code.

## RegisterWebApplication

With this method, a new web application can be registered via code.

<pre>/// Register a web application. It configure only services that doesn't need special access.
/// 

<ul>
  /// 
  
  <li>
    <var>pNameSpace</var> Name of the namespace when the web application runs.
  </li>
  /// 
  
  <li>
    <var>pName</var> Service name to create/update.
  </li>
  /// 
  
  <li>
    <var>pClassName</var> Name of class to run.
  </li>
  /// 
  
  <li>
    <var>pDescription</var> Comment to include.
  </li>
  /// 
</ul>
/// &amp;lt;example>
/// // Add web api
/// Do myClass.RegisterWebApplication("SAMPLES","/myApp/api","Host.RS.Rest","This is my api rest")&amp;lt;/example> ClassMethod RegisterWebApplication(pNameSpace As %String, pName As %String, pClassName As %String, pDescription As %String = "") As %Status
{
    New $Namespace
    Set $Namespace = "%SYS"
    Set ret = $$$OK
    Set spec("AutheEnabled") = $$$AutheUnauthenticated
    Set spec("NameSpace") = pNameSpace
    Set spec("Description") = pDescription
    Set spec("IsNameSpaceDefault") = $$$NO
    Set spec("DispatchClass") = pClassName
    Set spec("MatchRoles")=":%All"

    If ('##class(Security.Applications).Exists(pName)) {
        Write !,"Creating Web application """_pName_"""..."
        Set ret = ##class(Security.Applications).Create(pName, .spec)
        Write !, "Web application """_pName_""" is created."
    }
    Else { // ensure configuration matches in case of updating from old terminal versions
        Write !, "Updating web application """_pName_"""..."
        Set ret = ##class(Security.Applications).Modify(pName, .spec)
        Write !, "Web application """_pName_""" is updated."
    }
    Return ret
}</pre>

By default, it uses the %All roles, but you can modify according your needs

 

## RemoveWebApplication

<pre>/// Remove a web application
/// 

<ul>
  /// 
  
  <li>
    <var>pName</var> Name of web application to remove.
  </li>
  /// 
  
  <li>
    <var>pClassName</var> Name of the class linked to the web application.
  </li>
  /// 
</ul>
/// &amp;lt;example>
/// // Remove web api
/// Do myClass.RemoveWebApplication("/myApp/api","Host.RS.Rest")&amp;lt;/example>
ClassMethod RemoveWebApplication(pName As %String, pClassName As %String)
{
    New $Namespace
    Set $Namespace = "%SYS"
    Set ret = $$$OK
    If (##class(Security.Applications).Exists(pName)) {
        Do ##class(Security.Applications).Get(pName, .props)
        If (props("DispatchClass") '= pClassName) {
            Write !, "Web application doesn't refer to DispatchClass "_pClassName
        }
        Else {
            Write !, "Deleting Web application """_pName_"""..."
            Set ret = ##class(Security.Applications).Delete(pName)
            Write !, "Web application """_pName_""" was successfully deleted."
        }
    }
    Return ret
}</pre>

For security, the name of the class is necesary to check that you are not removing a web application by error.

I hope it helps you.

Best regards,  
Kurro Lopez