How to get a list of all company accounts? - c#

I'm trying to get list of all company accounts using SAP Business One (B1 or BO) Data Interface API (DI API) for .NET.
An account is represented as ChartOfAccounts SDK's type.
I can't figure out if there is a way to do something like this (it's how I'm getting the list of items):
var oItem = (Items) Company.GetBusinessObject(BoObjectTypes.oItems);
var oSBObob = (SBObob)Company.GetBusinessObject(BoObjectTypes.BoBridge);
var oRecordSet = oSBObob.GetItemList();
But it seems there is no method to similar to GetItemList() for accounts in SBObob type.
Does anybody know how to get list of company accounts?

I implemented this using a Recordset approach by querying OACT DB table directly for the list of account keys (AcctCode DB field) and then using ChartOfAccounts's GetByKey() method to fill other ChartOfAccounts's fields like this:
var sapAccount = (ChartOfAccounts)Company.GetBusinessObject(BoObjectTypes.oChartOfAccounts);
var oRecordSet = (Recordset)company.GetBusinessObject(BoObjectTypes.BoRecordset);
oRecordSet.DoQuery("SELECT AcctCode FROM OACT");
while (!oRecordSet.EoF)
{
var key = oRecordSet.Fields.Item(0).Value.ToString();
sapAccount.GetByKey(key)
// Now sapAccount is filled with current account data - do something with its fields
oRecordSet.MoveNext();
}

Related

How to map Azure Cosmos DB query recordset to C# class object [duplicate]

I'd like to store several different object types in a single Cosmos DB container, as they are all logically grouped and make sense to read together by timestamp to avoid extra HTTP calls.
However, the Cosmos DB client API doesn't seem to provide an easy way of doing the reads with multiple types. The best solution I've found so far is to write your own CosmosSerializer and JsonConverter, but that feels clunky: https://thomaslevesque.com/2019/10/15/handling-type-hierarchies-in-cosmos-db-part-2/
Is there a more graceful way to read items of different types to a shared base class so I can cast them later, or do I have to take the hit?
Thanks!
The way I do this is to create the ItemQueryIterator and FeedResponse objects as dynamic and initially read them untyped so I can inspect a "type" property that tells me what type of object to deserialize into.
In this example I have a single container that contains both my customer data as well as all their sales orders. The code looks like this.
string sql = "SELECT * from c WHERE c.customerId = #customerId";
FeedIterator<dynamic> resultSet = container.GetItemQueryIterator<dynamic>(
new QueryDefinition(sql)
.WithParameter("#customerId", customerId),
requestOptions: new QueryRequestOptions
{
PartitionKey = new PartitionKey(customerId)
});
CustomerV4 customer = new CustomerV4();
List<SalesOrder> orders = new List<SalesOrder>();
while (resultSet.HasMoreResults)
{
//dynamic response. Deserialize into POCO's based upon "type" property
FeedResponse<dynamic> response = await resultSet.ReadNextAsync();
foreach (var item in response)
{
if (item.type == "customer")
{
customer = JsonConvert.DeserializeObject<CustomerV4>(item.ToString());
}
else if (item.type == "salesOrder")
{
orders.Add(JsonConvert.DeserializeObject<SalesOrder>(item.ToString()));
}
}
}
Update:
You do not have to use dynamic types if want to create a "base document" class and then derive from that. Deserialize into the documentBase class, then check the type property check which class to deserialize the payload into.
You can also extend this pattern when you evolve your data models over time with a docVersion property.

List all users in specific role

In ASP.NET Core 2.2 MVC I'm trying to get a list of all users in a specific role.
Fx. list of all users in role named "Admin":
var idsWithPermission = _userManager.GetUsersInRoleAsync("Admin").Result;
var users = _db.ApplicationUser.Where(u => idsWithPermission.Contains(u.Id)).ToListAsync();
return(users);
Compiler fails "u.Id" here: idsWithPermission.Contains(u.Id)
Error: Argument 1: Cannot convert from "string" to Microsoft.AspNetCore.Identity.IdentityUser
This is a newbie questions, so might be very simple for a shark :-)
Thanks a lot in advance...
GetUsersInRoleAsync returns a list of IdentityUser objects. To get a list of IDs, you need to access the Id property of these objects.
// Get a list of users in the role
var usersWithPermission = _userManager.GetUsersInRoleAsync("Admin").Result;
// Then get a list of the ids of these users
var idsWithPermission = usersWithPermission.Select(u => u.Id);
// Now get the users in our database with the same ids
var users = _db.ApplicationUser.Where(u => idsWithPermission.Contains(u.Id)).ToListAsync();
return users;
Note that using .Result on an async method is not advised, because it can lead to deadlocks. Instead use await and make your method async.
Also note that depending on your setup, if ApplicationUser inherits from IdentityUser and the identity system is correctly configured, GetUsersInRoleAsync will already return ApplicationUser objects and you only need to cast them to the correct type:
// Get a list of users in the role
var usersWithPermission = _userManager.GetUsersInRoleAsync("Admin").Result;
var users = usersWithPermission.OfType<ApplicationUser>();
return users;

Searching Active Directory B2C by custom property on User

We are using B2C and storing customer numbers as a Extension field on users. A single user can have one or more customers and they are stored in a comma separated string.
What I am doing now is highly inefficient:
1. Get all Users
2. Get extension properties on each user
3. Check if they have the desired extension property and if it contains the customer I want.
4. Build a list of the users I want.
Adclient is IActiveDirectoryClient
var users = (await GetAllElementsInPagedCollection(await AdClient.Users.ExecuteAsync())).ToList();
var customersUsers = users.Where(user => user.AccountEnabled.HasValue && user.AccountEnabled.Value).Where(user =>
{
var extendedProperty = ((User) user).GetExtendedProperties().FirstOrDefault(extProp => extProp.Key == customersExtendedProperty.Name).Value?.ToString();
return extendedProperty != null && extendedProperty.Contains(customerId);
}).ToList();
I want to be able to do this in one query to ActiveDirectory using the AdClient. If I try this I get errors that the methods are not supported, which makes sense as I am assuming a query is being built behind the scenes to query Active Directory.
Edit - additional info:
I was able to query Graph API like this:
var authContext = await ActiveDirectoryClientFactory.GetAuthenticationContext(AuthConfiguration.Tenant,
AuthConfiguration.GraphUrl, AuthConfiguration.ClientId, AuthConfiguration.ClientSecret);
var url = $"https://graph.windows.net:443/hansaborgb2c.onmicrosoft.com/users?api-version=1.6&$filter={customersExtendedProperty.Name} eq '{customerId}'";
var users = await _graphApiHttpService.GetAll<User>(url, authContext.AccessToken);
However, in my example I need to use substringof to filter, but this is not supported by Azure Graph API.
I am not using that library, but we are doing a very similar search using the Graph API. I have constructed a filter that will look for users that match two extension attribute values I am looking for. The filter looks like this:
var filter = $"$filter={idpExtensionAttribute} eq '{userType.ToString()}' and {emailExtensionAttribute} eq '{emailAddress}'";
We have also used REST calls via PowerShell to the Graph API that will return the desired users. The URI with the associated filter looks like this:
https://graph.windows.net/$AzureADDomain/users?`$filter=extension_d2fbadd878984184ad5eab619d33d016_idp eq '$idp' and extension_d2fbadd878984184ad5eab619d33d016_email eq '$email'&api-version=1.6
Both of these options will return any users that match the filter criteria.
I would use normal DirectorySearcher Class from System.DirectoryServices
private void Search()
{
// GetDefaultDomain as start point is optional, you can also pass a specific
// root object like new DirectoryEntry ("LDAP://OU=myOrganisation,DC=myCompany,DC=com");
// not sure if GetDefaultDomain() works in B2C though :(
var results = FindUser("extPropName", "ValueYouAreLookingFor", GetDefaultDomain());
foreach (SearchResult sr in results)
{
// query the other properties you want for example Accountname
Console.WriteLine(sr.Properties["sAMAccountName"][0].ToString());
}
Console.ReadKey();
}
private DirectoryEntry GetDefaultDomain()
{ // Find the default domain
using (var dom = new DirectoryEntry("LDAP://rootDSE"))
{
return new DirectoryEntry("LDAP://" + dom.Properties["defaultNamingContext"][0].ToString());
}
}
private SearchResultCollection FindUser(string extPropName, string searchValue, DirectoryEntry startNode)
{
using (DirectorySearcher dsSearcher = new DirectorySearcher(startNode))
{
dsSearcher.Filter = $"(&(objectClass=user)({extPropName}={searchValue}))";
return dsSearcher.FindAll();
}
}
I came across this post looking to retrieve all users with a specific custom attribute value. Here's the implementation I ended up with:
var filter = $"{userOrganizationName} eq '{organizationName}'";
// Get all users (one page)
var result = await graphClient.Users
.Request()
.Filter(filter)
.Select($"id,surname,givenName,mail,{userOrganizationName}")
.GetAsync();
Where userOrganizationName is the b2c_extension full attribute name.

Accessing the properties of an IEnumerable

I'm using TweetInvi to grab a bunch of tweets that match a specified hashtag. I do this with the following:
var matchingTweets = Search.SearchTweets(hashtag);
This returns an IEnumerable (named ITweet, interface of Tweet), however I cannot create a List<> of Tweets, because Tweet is a static type.
I made, instead, a list of objects, using:
List<object> matches = matchingTweets.Cast<object>().ToList();
However, although each member of the matchingTweets IEnumerable has a number of properties, I cannot access them using:
long tweetID = matches[i].<property>;
Using matches[i].ToString() returns the tweet content, so how can I effectively cast the results in matchingTweets to a list, and subsequently access the properties of those list members? I would ideally like to avoid using dynamic.
In your example above you were trying to grab the ID from the tweet. ITweet implements ITweetIdentifier which contains the Id property. You can literally just access it by:
var matchingTweets = Search.SearchTweets(hashtag);
//Grab the first 5 tweets from the results.
var firstFiveTweets = matchingTweets.Take(5).ToList();
//if you only want the ids and not the entire object
var firstFiveTweetIds = matchingTweets.Take(5).Select(t => t.Id).ToList();
//Iterate through and do stuff
foreach (var tweet in matchingTweets)
{
//These are just examples of the properties accessible to you...
if(tweet.Favorited)
{
var text = tweet.FullText;
}
if(tweet.RetweetCount > 100)
{
//TODO: Handle popular tweets...
}
}
//Get item at specific index
matchingTweets.ElementAt(index);
I don't know exactly what you want to do with all the info, but since the SearchTweets returns a IEnumerable of ITweets you have access to anything an ITweet has defined.
I highly recommend looking through their wiki. It's pretty well organized and gives you clear examples of some basic tasks.
It makes sense you cannot access the properties. You cast it into object so you can only access the objects properties and methods (that like you said might have been overridden).
It should be fine to just access it like this:
List<ITweet> tweets = matchingTweets.Take(5).ToList();
What you can do is project it to a new object of yours:
var tweets = matchingTweets.Select(item => new {
property1 = item.property1,
property2 = item.property2
})
.Take(5).ToList();
Then you will be able to access what you need. Now, if you need to share this data outside the scope of that function create a DTO object and initialize it instead of the anonymous type.
Depending on the size of the project and amount of effort usually it is in any case a good practice to create a layer of DTO objects when you interact with an external service like this. Then if their models changed you can contain your changes only to the DTOs.
If all you want are the ids of the first 5 then:
var ids = matchingTweets.Take(5).Select(item => item.id).ToList();

Does IList<> guarantee order of added items in C#

I am using Seed() method of Configuration.cs class for filling data in database when using Update-Database command.
Among other things I am creating list of EventCategory objects like this:
private IList<EventCategory> CreateEventCategoriesTestDefinition()
{
eventCategories = new List<EventCategory>();
var eventCategoryRecruitment = new EventCategory("Recruitment");
eventCategories.Add(eventCategoryRecruitment);
var eventCategoryInternship = new EventCategory("Internship");
eventCategories.Add(eventCategoryInternship);
var eventCategoryTrainingPrograms = new EventCategory("Training Programs");
eventCategoryTrainingPrograms.Events
.Add(new Event("Managerial Training Program 2012-2014", eventCategoryTrainingPrograms));
eventCategories.Add(eventCategoryTrainingPrograms);
var eventCategoryEmployee = new EventCategory("Employee & Team Potential");
eventCategories.Add(eventCategoryEmployee);
return eventCategories;
}
Adding element by element. eventCategory is just a private property:
private IList<EventCategory> eventCategories;
From Seed() method I am calling CreateEventCategoriesTestDefinition()
Almost everything is good but when I go to database to check data I have noticed that data in EventCategory table doesn't have correct order:
As you can see it on a picture Internshipand Training Programs switched positions comparing to order of adding inCreateEventCategoriesTestDefinition() method.
Does anybody knows what is happening here? Why order of adding is not preserved? I know it should be perserved in List<>, but is not the same for IList<>?
Or this is maybe has something to do with EntityFramework?
If you are relying upon the database for your sorting order then either.
Turn auto-id incrementation off and specify your own ID
EventCategory(int id, string name)
If you have to use database identity then instead try using a sort order (int) column for your objects
EventCategory(string name, int sortOrder)
Either way, you cannot guarantee that you'll get a sorted List persisted to the database. I cant say for certain your use case here, but you shouldn't reply on SQL to sort your objects for you, but when querying the database use linq to order them before binding to a view.
e.g.
var mySortedCategories = dbContext.EventCategories.OrderBy(x => x.Name).ToList();

Categories

Resources