DEV Community

Cover image for Building Advanced Solutions with Power Apps and .NET Core
Paulo Torres
Paulo Torres

Posted on

Building Advanced Solutions with Power Apps and .NET Core

Power Apps is a powerful platform that enables businesses to create custom applications without extensive coding knowledge. However, for more complex scenarios, integrating Power Apps with .NET Core allows developers to build advanced, scalable solutions that leverage the full power of the .NET ecosystem. This article explores how to integrate Power Apps with .NET Core, using real-world examples to illustrate advanced techniques for building robust, enterprise-level applications.

Understanding Power Apps and .NET Core Integration

What is Power Apps?

Power Apps is a low-code platform developed by Microsoft that allows users to create custom business applications. It provides a range of templates and pre-built components, enabling users to quickly build apps that can be used across various devices. Power Apps integrates seamlessly with other Microsoft services like SharePoint, Dynamics 365, and Office 365, making it a versatile tool for business process automation.

Why Integrate with .NET Core?

While Power Apps is powerful, there are scenarios where its capabilities might not be sufficient for complex business logic or performance-critical operations. Integrating Power Apps with .NET Core allows you to:

  • Leverage the full capabilities of the .NET ecosystem.
  • Build custom APIs that can be consumed by Power Apps.
  • Implement advanced business logic that goes beyond the built-in capabilities of Power Apps.
  • Securely access on-premises and cloud-based resources.

Setting Up the Integration

Step 1: Create a .NET Core API

The first step in integrating Power Apps with .NET Core is to create a .NET Core API that will serve as the backend for your Power App.

  1. Create a new .NET Core Web API project:
   dotnet new webapi -n PowerAppsIntegration
   cd PowerAppsIntegration
Enter fullscreen mode Exit fullscreen mode
  1. Define your API models: For this example, let's create a simple model representing a product.
   public class Product
   {
       public int Id { get; set; }
       public string Name { get; set; }
       public decimal Price { get; set; }
   }
Enter fullscreen mode Exit fullscreen mode
  1. Create the API controller: Implement the controller to handle HTTP requests.
   [ApiController]
   [Route("api/[controller]")]
   public class ProductsController : ControllerBase
   {
       private static readonly List<Product> Products = new List<Product>
       {
           new Product { Id = 1, Name = "Laptop", Price = 999.99M },
           new Product { Id = 2, Name = "Smartphone", Price = 499.99M }
       };

       [HttpGet]
       public ActionResult<IEnumerable<Product>> Get()
       {
           return Products;
       }

       [HttpGet("{id}")]
       public ActionResult<Product> Get(int id)
       {
           var product = Products.FirstOrDefault(p => p.Id == id);
           if (product == null) return NotFound();
           return product;
       }

       [HttpPost]
       public ActionResult<Product> Post(Product product)
       {
           product.Id = Products.Max(p => p.Id) + 1;
           Products.Add(product);
           return CreatedAtAction(nameof(Get), new { id = product.Id }, product);
       }
   }
Enter fullscreen mode Exit fullscreen mode
  1. Deploy the API: Deploy the .NET Core API to Azure or any other cloud service. Ensure that the API is accessible from the internet, as Power Apps will need to consume it.

Step 2: Secure the API with Azure AD

Security is a critical aspect when integrating Power Apps with .NET Core. One effective way to secure your API is by using Azure Active Directory (Azure AD) for authentication and authorization.

  1. Register the API in Azure AD: Go to the Azure portal and register your API as an application in Azure AD. Configure the API permissions and define scopes that will be required by clients (e.g., Power Apps).

  2. Update the .NET Core API to use Azure AD: Install the necessary NuGet packages and update the Startup.cs to configure authentication.

   public void ConfigureServices(IServiceCollection services)
   {
       services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
           .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));

       services.AddControllers();
   }

   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
   {
       app.UseRouting();

       app.UseAuthentication();
       app.UseAuthorization();

       app.UseEndpoints(endpoints =>
       {
           endpoints.MapControllers();
       });
   }
Enter fullscreen mode Exit fullscreen mode
  1. Test the secured API: Use tools like Postman to test the API with Azure AD authentication, ensuring that only authorized requests can access the API.

Step 3: Connect Power Apps to the .NET Core API

Now that your API is deployed and secured, the next step is to connect Power Apps to this API.

  1. Create a new Power App: Open Power Apps and create a new app (Canvas App or Model-Driven App) depending on your requirements.

  2. Add a custom connector: In Power Apps, navigate to "Data" -> "Custom connectors" and create a new connector. Provide the API URL, define the operations (GET, POST, etc.), and configure the authentication to use OAuth2 with Azure AD.

  3. Use the connector in Power Apps: Once the custom connector is set up, you can use it to connect to your .NET Core API. For example, you can create a form in Power Apps that allows users to submit data, which is then sent to your API.

   // Example formula in Power Apps to create a new product
   SubmitForm(CreateProductForm);
Enter fullscreen mode Exit fullscreen mode
  1. Display data from the API: You can also retrieve data from the API and display it in Power Apps. For example, use a gallery to list products:
   // Example formula to retrieve products
   Products = MyApi.GetProducts();
Enter fullscreen mode Exit fullscreen mode

Advanced Scenarios

1. Implementing Complex Business Logic

.NET Core allows you to implement complex business logic that goes beyond the capabilities of Power Apps. For instance, you can perform complex calculations, integrate with third-party services, or apply advanced validation rules in the backend.

Example: Calculating Discounts Based on User Role

In your .NET Core API, you can implement logic to apply discounts based on the user's role:

[Authorize(Roles = "Admin")]
[HttpPost("CalculateDiscount")]
public ActionResult<decimal> CalculateDiscount(Product product)
{
    var discount = product.Price * 0.1M;
    return Ok(discount);
}
Enter fullscreen mode Exit fullscreen mode

2. Integrating with On-Premises Systems

In some scenarios, your Power Apps solution may need to interact with on-premises systems. You can achieve this by using a hybrid architecture where your .NET Core API connects to on-premises resources using technologies like Azure Hybrid Connections or a VPN.

Example: Accessing On-Premises SQL Server

Your .NET Core API can connect to an on-premises SQL Server instance to retrieve or update data, and then expose this functionality to Power Apps:

public class OnPremisesDataService
{
    private readonly string _connectionString;

    public OnPremisesDataService(IConfiguration configuration)
    {
        _connectionString = configuration.GetConnectionString("OnPremisesSqlServer");
    }

    public IEnumerable<Product> GetProducts()
    {
        using var connection = new SqlConnection(_connectionString);
        return connection.Query<Product>("SELECT * FROM Products");
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Using Power Automate for Workflow Automation

Power Automate (formerly Microsoft Flow) can be used alongside Power Apps and .NET Core to automate workflows. For example, when a new record is created in Power Apps, Power Automate can trigger a workflow that calls your .NET Core API, sends an email notification, or updates another system.

Example: Triggering a Workflow on Record Creation

In Power Automate, create a flow that triggers when a new item is created in Power Apps:

Trigger: When a new item is created in Power Apps
Action: HTTP request to your .NET Core API to process the data
Action: Send an email notification to the user
Enter fullscreen mode Exit fullscreen mode

Conclusion

Integrating Power Apps with .NET Core opens up a world of possibilities for building advanced, enterprise-grade applications. By leveraging the full power of the .NET ecosystem, you can implement complex business logic, integrate with on-premises systems, and securely expose APIs to Power Apps. Whether you're building custom connectors, implementing advanced authentication, or automating workflows, the combination of Power Apps and .NET Core provides a flexible, scalable, and powerful platform for modern application development.

Top comments (1)

Collapse
 
jangelodev profile image
João Angelo

Hi Paulo Torres,
Top, very nice and helpful !
Thanks for sharing.