PowerAppsWebApiUtils 0.0.0.10

This is an API to work with PowerApps and Dynamics 365 Web API

Install-Package PowerAppsWebApiUtils -Version 0.0.0.10
dotnet add package PowerAppsWebApiUtils --version 0.0.0.10
<PackageReference Include="PowerAppsWebApiUtils" Version="0.0.0.10" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PowerAppsWebApiUtils --version 0.0.0.10
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

This is an API to work with PowerApps and Dynamics 365 Web API

Early bound classes for web api
Simple Linq provider for web api.

Targets .netcore 2.0 and .net framework 4.7
Integrate well with Azure Active Directory

Feedback and contributions welcome...

How tos:

Setup an application user for PowerAppsWebApiUtils: https://www.linkedin.com/pulse/how-setup-application-user-powerappswebapiutils-philippe-dufag
Generate early bound classes and setup a project to use PowerAppsWebApiUtils: https://www.linkedin.com/pulse/powerappswebapiutils-howto-philippe-dufag

Examples of supported Linq queries:

To get the service context:
var config =
new PowerAppsAuthenticationSettings
{
ClientId = configuration["clientId"],
ClientSecret = configuration["secret"],
DirectoryId = configuration["directory"],
ApiUrl = configuration["powerappsApiUrl"],
ResourceUrl = configuration["powerappsUrl"],
};

serviceProvider =
new ServiceCollection()
.AddPowerAppsWebApiConfiguration(config)
.BuildServiceProvider();

//To impersonate, create, query, diassociate and delete
using (var context = serviceProvider.GetService<WebApiContext>())
{
context.CallerObjectId = Guid.Parse("cabfad08-d910-4c38-9873-ee9bec791238");
var account =
new Account {
Name = "JB Petroleum Inc.",
NumberOfEmployees = 6,
IndustryCode = account_industrycode.PetrochemicalExtractionandDistribution,
Revenue = 100000m,
CreditLimit = 250000m,
CustomerTypeCode = account_customertypecode.Reseller,
};

account.Id = await context.Create(account);
var contact = 
    new Contact {
        FirstName = "J",
        LastName = "B",
        ParentCustomerId = null,
        AccountRoleCode = contact_accountrolecode.DecisionMaker,
        Address1_Line1 = "1450 JB Street",
        Address1_Country = "Canada",
    };
contact.Id = await context.Create(contact);  
//To delete an entity:
await context.Delete(contact);

contact = 
    new Contact {
        FirstName = "J",
        LastName = "B",
        ParentCustomerId = account.ToNavigationProperty(),
        AccountRoleCode = contact_accountrolecode.DecisionMaker,
        Address1_Line1 = "1450 JB Street",
        Address1_Country = "Canada",
    };

contact.Id = await context.Create(contact);
var parentcustomerid = context.CreateQuery<Contact>().Where(p => p.Id == contact.Id).Select(p => p.ParentCustomerId).FirstOrDefault();
Assert.NotNull(parentcustomerid);
Assert.Equal(account.Id, parentcustomerid.Id);

await context.Update(new Contact(contact.Id){ ParentCustomerId = null });
contact = context.CreateQuery<Contact>().Where(p => p.Id == contact.Id).Select(p => new Contact(p.Id) { ParentCustomerId = p.ParentCustomerId  }).FirstOrDefault();
Assert.NotNull(contact);
Assert.Null(contact.ParentCustomerId);

await context.Update(new Contact(contact.Id){ ParentCustomerId = account.ToNavigationProperty() });
contact = context.CreateQuery<Contact>().Where(p => p.Id == contact.Id).Select(p => new Contact(p.Id) { ParentCustomerId = p.ParentCustomerId  }).FirstOrDefault();
Assert.NotNull(contact);
Assert.NotNull(contact.ParentCustomerId);
Assert.Equal(account.Id, contact.ParentCustomerId.Id);

}

//Sample queries:
{
context.CreateQuery<Account>().Where(p => p.Address1_Country == "Canada").Select(p => new Account(p.Id) { Name = p.Name}).OrderBy(p => p.Name).ToList();
context.CreateQuery<Account>().ToList();
context.CreateQuery<Account>().FirstOrDefault();
context.CreateQuery<Account>().Where(p => p.Name == "Test").ToList();
context.CreateQuery<Account>().Where(p => p.Name == "Test").FirstOrDefault();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).FirstOrDefault();
context.CreateQuery<Account>().Where(p => p.Name == "Test").Where(p => p.StateCode == account_statecode.Active);
context.CreateQuery<Account>().Where(p => p.Name == "Toto" || p.Name == "Tata").Where(p => p.StateCode == account_statecode.Active).ToList();
context.CreateQuery<Account>().Where(p => p.Name == "Toto" || p.Name == "Tata").Where(p => p.StateCode == account_statecode.Active).FirstOrDefault();

context.CreateQuery<CustomerAddress>().Where(p => p.ParentId == new Account(Guid.NewGuid()).ToNavigationProperty()).Where(p => p.ShippingMethodCode == customeraddress_shippingmethodcode.Airborne && p.Country == "Canada").ToList();
context.CreateQuery<CustomerAddress>().Where(p => p.ParentId == new Account(Guid.NewGuid()).ToNavigationProperty()).Where(p => p.ShippingMethodCode == customeraddress_shippingmethodcode.Airborne && p.Country == "Canada").FirstOrDefault();
context.CreateQuery<CustomerAddress>().Where(p => p.ParentId == new Account(Guid.NewGuid()).ToNavigationProperty()).Select(p => new { Id = p.Id, OwnerId = p.OwnerId }).FirstOrDefault();

context.CreateQuery<Account>().OrderByDescending(p => p.Name).ToList();
context.CreateQuery<Account>().OrderByDescending(p => new {p.Address1_City, p.Name} ).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).OrderBy(p => p.Name).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).OrderByDescending(p => p.Name).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).OrderBy(p => new {p.Address1_City, p.Name}).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).OrderByDescending(p => new {p.Address1_City, p.Name}).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).Select(p => new { Name = p.Name, Id = p.Id, CreatedBy = p.CreatedBy }).OrderBy(p => p.Name).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).Select(p => new { Name = p.Name, Id = p.Id, CreatedBy = p.CreatedBy }).OrderByDescending(p => p.Name).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).OrderByDescending(p => p.Name).Select(p => new { Name = p.Name, Id = p.Id, CreatedBy = p.CreatedBy }).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).Select(p => new Account{ Id = p.Id, CustomerTypeCode = p.CustomerTypeCode }).OrderByDescending(p => p.Name).ToList();
context.CreateQuery<Account>().Where(p => p.Name == "John").Where(p => p.StateCode == account_statecode.Active).OrderByDescending(p => p.Name).Select(p => new Account{ Id = p.Id, CustomerTypeCode = p.CustomerTypeCode }).ToList();
context.CreateQuery<Account>().Where(p => p.Name == "John" || p.Name == "Doe").Where(p => p.StateCode == account_statecode.Active).OrderByDescending(p => p.Name).Select(p => new { Id = p.Id, CustomerTypeCode = p.CustomerTypeCode, ExchangeRate = p.ExchangeRate }).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).Select(p => new { Id = p.Id, CreatedBy = p.CreatedBy }).ToList();

}

=================================================================
You can still override the webapi request using the following method for operation not supported by webapi or use the async/await pattern:

var repository = serviceProvider.GetRequiredService<GenericRepository<Contact>>();
var querystring = $"{config.ApiUrl}{Contact.CollectionName}?$select=firstname,lastname,_parentcustomerid_value,address1_city&$filter=address1_city eq 'Redmond'&$orderby=lastname";
var contacts = await repository.RetrieveMultiple(querystring);

This is an API to work with PowerApps and Dynamics 365 Web API

Early bound classes for web api
Simple Linq provider for web api.

Targets .netcore 2.0 and .net framework 4.7
Integrate well with Azure Active Directory

Feedback and contributions welcome...

How tos:

Setup an application user for PowerAppsWebApiUtils: https://www.linkedin.com/pulse/how-setup-application-user-powerappswebapiutils-philippe-dufag
Generate early bound classes and setup a project to use PowerAppsWebApiUtils: https://www.linkedin.com/pulse/powerappswebapiutils-howto-philippe-dufag

Examples of supported Linq queries:

To get the service context:
var config =
new PowerAppsAuthenticationSettings
{
ClientId = configuration["clientId"],
ClientSecret = configuration["secret"],
DirectoryId = configuration["directory"],
ApiUrl = configuration["powerappsApiUrl"],
ResourceUrl = configuration["powerappsUrl"],
};

serviceProvider =
new ServiceCollection()
.AddPowerAppsWebApiConfiguration(config)
.BuildServiceProvider();

//To impersonate, create, query, diassociate and delete
using (var context = serviceProvider.GetService<WebApiContext>())
{
context.CallerObjectId = Guid.Parse("cabfad08-d910-4c38-9873-ee9bec791238");
var account =
new Account {
Name = "JB Petroleum Inc.",
NumberOfEmployees = 6,
IndustryCode = account_industrycode.PetrochemicalExtractionandDistribution,
Revenue = 100000m,
CreditLimit = 250000m,
CustomerTypeCode = account_customertypecode.Reseller,
};

account.Id = await context.Create(account);
var contact = 
    new Contact {
        FirstName = "J",
        LastName = "B",
        ParentCustomerId = null,
        AccountRoleCode = contact_accountrolecode.DecisionMaker,
        Address1_Line1 = "1450 JB Street",
        Address1_Country = "Canada",
    };
contact.Id = await context.Create(contact);  
//To delete an entity:
await context.Delete(contact);

contact = 
    new Contact {
        FirstName = "J",
        LastName = "B",
        ParentCustomerId = account.ToNavigationProperty(),
        AccountRoleCode = contact_accountrolecode.DecisionMaker,
        Address1_Line1 = "1450 JB Street",
        Address1_Country = "Canada",
    };

contact.Id = await context.Create(contact);
var parentcustomerid = context.CreateQuery<Contact>().Where(p => p.Id == contact.Id).Select(p => p.ParentCustomerId).FirstOrDefault();
Assert.NotNull(parentcustomerid);
Assert.Equal(account.Id, parentcustomerid.Id);

await context.Update(new Contact(contact.Id){ ParentCustomerId = null });
contact = context.CreateQuery<Contact>().Where(p => p.Id == contact.Id).Select(p => new Contact(p.Id) { ParentCustomerId = p.ParentCustomerId  }).FirstOrDefault();
Assert.NotNull(contact);
Assert.Null(contact.ParentCustomerId);

await context.Update(new Contact(contact.Id){ ParentCustomerId = account.ToNavigationProperty() });
contact = context.CreateQuery<Contact>().Where(p => p.Id == contact.Id).Select(p => new Contact(p.Id) { ParentCustomerId = p.ParentCustomerId  }).FirstOrDefault();
Assert.NotNull(contact);
Assert.NotNull(contact.ParentCustomerId);
Assert.Equal(account.Id, contact.ParentCustomerId.Id);

}

//Sample queries:
{
context.CreateQuery<Account>().Where(p => p.Address1_Country == "Canada").Select(p => new Account(p.Id) { Name = p.Name}).OrderBy(p => p.Name).ToList();
context.CreateQuery<Account>().ToList();
context.CreateQuery<Account>().FirstOrDefault();
context.CreateQuery<Account>().Where(p => p.Name == "Test").ToList();
context.CreateQuery<Account>().Where(p => p.Name == "Test").FirstOrDefault();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).FirstOrDefault();
context.CreateQuery<Account>().Where(p => p.Name == "Test").Where(p => p.StateCode == account_statecode.Active);
context.CreateQuery<Account>().Where(p => p.Name == "Toto" || p.Name == "Tata").Where(p => p.StateCode == account_statecode.Active).ToList();
context.CreateQuery<Account>().Where(p => p.Name == "Toto" || p.Name == "Tata").Where(p => p.StateCode == account_statecode.Active).FirstOrDefault();

context.CreateQuery<CustomerAddress>().Where(p => p.ParentId == new Account(Guid.NewGuid()).ToNavigationProperty()).Where(p => p.ShippingMethodCode == customeraddress_shippingmethodcode.Airborne && p.Country == "Canada").ToList();
context.CreateQuery<CustomerAddress>().Where(p => p.ParentId == new Account(Guid.NewGuid()).ToNavigationProperty()).Where(p => p.ShippingMethodCode == customeraddress_shippingmethodcode.Airborne && p.Country == "Canada").FirstOrDefault();
context.CreateQuery<CustomerAddress>().Where(p => p.ParentId == new Account(Guid.NewGuid()).ToNavigationProperty()).Select(p => new { Id = p.Id, OwnerId = p.OwnerId }).FirstOrDefault();

context.CreateQuery<Account>().OrderByDescending(p => p.Name).ToList();
context.CreateQuery<Account>().OrderByDescending(p => new {p.Address1_City, p.Name} ).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).OrderBy(p => p.Name).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).OrderByDescending(p => p.Name).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).OrderBy(p => new {p.Address1_City, p.Name}).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).OrderByDescending(p => new {p.Address1_City, p.Name}).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).Select(p => new { Name = p.Name, Id = p.Id, CreatedBy = p.CreatedBy }).OrderBy(p => p.Name).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).Select(p => new { Name = p.Name, Id = p.Id, CreatedBy = p.CreatedBy }).OrderByDescending(p => p.Name).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).OrderByDescending(p => p.Name).Select(p => new { Name = p.Name, Id = p.Id, CreatedBy = p.CreatedBy }).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).Select(p => new Account{ Id = p.Id, CustomerTypeCode = p.CustomerTypeCode }).OrderByDescending(p => p.Name).ToList();
context.CreateQuery<Account>().Where(p => p.Name == "John").Where(p => p.StateCode == account_statecode.Active).OrderByDescending(p => p.Name).Select(p => new Account{ Id = p.Id, CustomerTypeCode = p.CustomerTypeCode }).ToList();
context.CreateQuery<Account>().Where(p => p.Name == "John" || p.Name == "Doe").Where(p => p.StateCode == account_statecode.Active).OrderByDescending(p => p.Name).Select(p => new { Id = p.Id, CustomerTypeCode = p.CustomerTypeCode, ExchangeRate = p.ExchangeRate }).ToList();
context.CreateQuery<Account>().Where(p => p.StateCode == account_statecode.Active).Select(p => new { Id = p.Id, CreatedBy = p.CreatedBy }).ToList();

}

=================================================================
You can still override the webapi request using the following method for operation not supported by webapi or use the async/await pattern:

var repository = serviceProvider.GetRequiredService<GenericRepository<Contact>>();
var querystring = $"{config.ApiUrl}{Contact.CollectionName}?$select=firstname,lastname,_parentcustomerid_value,address1_city&$filter=address1_city eq 'Redmond'&$orderby=lastname";
var contacts = await repository.RetrieveMultiple(querystring);

Release Notes

0.0.0.10 Fix issue with Linq Provider serializing "not" instead of "ne"
            Fix issue with Json Serialization for lookups (NavigationProperty) when set to null in Create and Update operations
   0.0.0.9 Diassociate function now available
           Code Generation [Early bound classes need to be regenerated]:
             Refactor attribute NavigationPropertyAttributeTargets into NavigationPropertyAttributeTarget (used with Diassociate)
             Fix issue with PrimaryId generation in some situation
   0.0.0.8 Fix issue with CallerObjectId.
           Code Generation [Early bound classes need to be regenerated]:
             Fix issue in codegen with picklist. EntityMetadata DefaultFormValue is optional.
             Fix issue with lookup. Lookups are now using SchemaName for json serialization for 'Create' and 'Update' operations when attribute isCustomType and attributeType is 'Lookup', otherwise it uses LogicalName
   0.0.0.7 Impersonation (new properties CallerObjectId and MSCRMCallerID on WebApiContext and GenericRepository. see Microsoft docs https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/impersonate-another-user-web-api).
   0.0.0.6 Modification to service registration (Scoped -> Transient) to ease integration with Azure Functions.
   0.0.0.5 Add missing nuget assemblies in "earlybound" folder.
           Change PowerAppsWebApiUtils.Console to tools folder.
           From docs.microsoft.com, "The tools folder is added to the PATH environment variable for the Package Manager Console only"

   0.0.0.4 Standard OData string query functions support (BeginsWith, EndsWith, Contains)
           Azure Functions have changed their dependency on Newtonsoft [Newtonsoft.Json (>= 11.0.2)], so integration with Azure Functions will be simplier now  Microsoft.NET.Sdk.Functions/1.0.30-beta1
   0.0.0.3 OrderBy / OrderByDescending support - Readme.md having few examples
   0.0.0.2 Include missing file PowerAppsWebApiUtils.Console.runtimeconfig.json
   0.0.0.1 Initial release. .net core app to generate early bound classes. Api exposes in .netstandard 2.0 supportin all CRUD operations and limited Linq support (ToList and FirstOrDefault)

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
0.0.0.10 223 1/22/2020
0.0.0.9 104 1/19/2020
0.0.0.8 206 1/15/2020
0.0.0.7 112 1/14/2020
0.0.0.6 168 8/13/2019
0.0.0.5 144 8/10/2019
0.0.0.4 141 8/10/2019
0.0.0.3 137 7/28/2019
0.0.0.2 141 7/15/2019