patterncsharpMinor
Order Retrieval System with a SharePoint Backend
Viewed 0 times
sharepointbackendorderwithsystemretrieval
Problem
I have an ordering system that uses SharePoint. Below I've included a small snapshot of the system that is used in a WPF form when the user clicks a button to refresh the order listing. The outcome is a populated WPF datagrid. Note that this code is using C# 6.0 as well.
Everything works well, and it's quite quick, but I feel there's lots of room in my skill-set for improvements - happy for all guidance that can be offered!
Refresh Orders code
```
private void RefreshOrders(string loadStatus)
{
Orders.LoadStatus = loadStatus;
var collListItem = Orders.LatestOrders;
if (!collListItem.Any())
{
MessageBox.Show(
"No orders are currently within the queue.",
"Information Center",
MessageBoxButton.OK,
MessageBoxImage.Information);
return;
}
if (collListItem == null)
{
MessageBox.Show(
"Order processor was unable to reach the SharePoint server to download orders.",
"SharePoint Currently Unavailable",
MessageBoxButton.OK,
MessageBoxImage.Asterisk);
return;
}
var customers = new ObservableCollection();
foreach (var customer in
collListItem.Select(
item =>
new
{
Persona = item["Persona"].ToString(),
CustomerName = item["Title"].ToString(),
Email = item["Customer_x0020_E_x002d_mail"].ToString(),
Organization = item["Organization"].ToString(),
PhoneNumber = item["Customer_x0020_Phone_x0020_Numbe"].ToString(),
Street = item["Customer_x0020_Street"].ToString(),
Suburb = item["Customer_x0020_Suburb"].ToString(),
Postcode = item["Customer_x0020_Postcode"].ToString(),
OrderDate = Convert.ToDateTime(item["Created"], new CultureInfo("hr-HR")).Date ,
DispatchedDate = Convert.ToDateTime(item["Modified"], new Cult
Everything works well, and it's quite quick, but I feel there's lots of room in my skill-set for improvements - happy for all guidance that can be offered!
Refresh Orders code
```
private void RefreshOrders(string loadStatus)
{
Orders.LoadStatus = loadStatus;
var collListItem = Orders.LatestOrders;
if (!collListItem.Any())
{
MessageBox.Show(
"No orders are currently within the queue.",
"Information Center",
MessageBoxButton.OK,
MessageBoxImage.Information);
return;
}
if (collListItem == null)
{
MessageBox.Show(
"Order processor was unable to reach the SharePoint server to download orders.",
"SharePoint Currently Unavailable",
MessageBoxButton.OK,
MessageBoxImage.Asterisk);
return;
}
var customers = new ObservableCollection();
foreach (var customer in
collListItem.Select(
item =>
new
{
Persona = item["Persona"].ToString(),
CustomerName = item["Title"].ToString(),
Email = item["Customer_x0020_E_x002d_mail"].ToString(),
Organization = item["Organization"].ToString(),
PhoneNumber = item["Customer_x0020_Phone_x0020_Numbe"].ToString(),
Street = item["Customer_x0020_Street"].ToString(),
Suburb = item["Customer_x0020_Suburb"].ToString(),
Postcode = item["Customer_x0020_Postcode"].ToString(),
OrderDate = Convert.ToDateTime(item["Created"], new CultureInfo("hr-HR")).Date ,
DispatchedDate = Convert.ToDateTime(item["Modified"], new Cult
Solution
Have you tested this? You've used the words 'quick' and 'SharePoint' in the same question which, in my experience, is almost always a lie :)
You have a problem here:
That second if statement can never be hit! If
A few quick notes on your code:
Update
You have a problem here:
private void RefreshOrders(string loadStatus)
{
Orders.LoadStatus = loadStatus;
var collListItem = Orders.LatestOrders;
if (!collListItem.Any())
{
// omitted.
}
if (collListItem == null)
{That second if statement can never be hit! If
collListItem is null, then the call to Any() will throw a null reference exception.A few quick notes on your code:
- LoadStatus should probably be an enum
- Put the column names in named constants
- A property shouldn't do non trivial work -
CachedItemsshould be retrieved through a method
- You could remove the foreach loop to add to customers observable collection and use the constructor which takes an IEnumerable
Update
var customerQuery = collListItem.Select(
item =>
new
{
Persona = item["Persona"].ToString(),
CustomerName = item["Title"].ToString(),
Email = item["Customer_x0020_E_x002d_mail"].ToString(),
Organization = item["Organization"].ToString(),
PhoneNumber = item["Customer_x0020_Phone_x0020_Numbe"].ToString(),
Street = item["Customer_x0020_Street"].ToString(),
Suburb = item["Customer_x0020_Suburb"].ToString(),
Postcode = item["Customer_x0020_Postcode"].ToString(),
OrderDate = Convert.ToDateTime(item["Created"], new CultureInfo("hr-HR")).Date ,
DispatchedDate = Convert.ToDateTime(item["Modified"], new CultureInfo("hr-HR")).Date,
Status = item["Status"].ToString()
})
.Distinct()
.Select(
r =>
new Customer
{
Persona = r.Persona,
CustomerName = r.CustomerName,
Email = r.Email,
Organization = r.Organization,
PhoneNumber = r.PhoneNumber,
Street = r.Street,
Suburb = r.Suburb,
Postcode = r.Postcode,
OrderDate = r.OrderDate.Date,
DispatchedDate = r.DispatchedDate,
Status = r.Status
}));
var customers = new ObservableCollection(customerQuery);Code Snippets
private void RefreshOrders(string loadStatus)
{
Orders.LoadStatus = loadStatus;
var collListItem = Orders.LatestOrders;
if (!collListItem.Any())
{
// omitted.
}
if (collListItem == null)
{var customerQuery = collListItem.Select(
item =>
new
{
Persona = item["Persona"].ToString(),
CustomerName = item["Title"].ToString(),
Email = item["Customer_x0020_E_x002d_mail"].ToString(),
Organization = item["Organization"].ToString(),
PhoneNumber = item["Customer_x0020_Phone_x0020_Numbe"].ToString(),
Street = item["Customer_x0020_Street"].ToString(),
Suburb = item["Customer_x0020_Suburb"].ToString(),
Postcode = item["Customer_x0020_Postcode"].ToString(),
OrderDate = Convert.ToDateTime(item["Created"], new CultureInfo("hr-HR")).Date ,
DispatchedDate = Convert.ToDateTime(item["Modified"], new CultureInfo("hr-HR")).Date,
Status = item["Status"].ToString()
})
.Distinct()
.Select(
r =>
new Customer
{
Persona = r.Persona,
CustomerName = r.CustomerName,
Email = r.Email,
Organization = r.Organization,
PhoneNumber = r.PhoneNumber,
Street = r.Street,
Suburb = r.Suburb,
Postcode = r.Postcode,
OrderDate = r.OrderDate.Date,
DispatchedDate = r.DispatchedDate,
Status = r.Status
}));
var customers = new ObservableCollection<Customer>(customerQuery);Context
StackExchange Code Review Q#101936, answer score: 3
Revisions (0)
No revisions yet.