How To Get Purchase Orders From Quickbooks Online Using C#

In this article, we will learn how to get purchase orders from Quickbooks online in .NET MVC web application using SDK.

Before using any Quickbooks online API we need access token, if you don’t know how to get access token then you can find it here.

  • First, we have to create a ServiceContext with Auth tokens and realmId.
  • For that, we need access token and realmId
  • For getting purchase orders, we have to define a QueryService object.
  • QueryService object needs a ServiceContext object as parameter.
  • So we have to create a Quickbooks QueryService using ServiceContext.
  • Here we are getting all purchase orders from Quickbooks online, code is as below.
public ActionResult GetAllPurchaseOrder()
{
  List<PurchaseOrder> PurchaseOrderList = new List<PurchaseOrder>();
  try
  {
    OAuth2RequestValidator oauthValidator = new OAuth2RequestValidator(Access_token);
    // Create a ServiceContext with Auth tokens and realmId
    ServiceContext serviceContext = new ServiceContext(RealmId, IntuitServicesType.QBO, oauthValidator);
    serviceContext.IppConfiguration.MinorVersion.Qbo = "23";
    serviceContext.IppConfiguration.BaseUrl.Qbo = QboBaseUrl;

    // Create a QuickBooks QueryService using ServiceContext
    QueryService<PurchaseOrder> querySvc = new QueryService<PurchaseOrder>(serviceContext);
    PurchaseOrderList = querySvc.ExecuteIdsQuery("SELECT * FROM PurchaseOrder").ToList();

    return View(PurchaseOrderList);
  }
  catch (IdsException ex)
  {
    return View(PurchaseOrderList);
  }
  catch (Exception ex)
  {
    return View(PurchaseOrderList);
  }
}
  • We will get all the purchase orders list in the PurchaseOrderList object.
  • View Code is as below,
@model List<Intuit.Ipp.Data.PurchaseOrder>
@using Intuit.Ipp.Data;

@{
    ViewBag.Title = "GetAllPurchaseOrder";
}

<h2>Quickbooks online Purchase Order List</h2>

<div>
    <table class="table table-bordered">
      
        @foreach (var item in Model)
        {
            <tr style="border: 2px solid black;border-bottom: none;background-color:#a9a9a9">
                <th>QBO Purchase Order ID</th>
                <th>Purchase Order Number</th>
                <th>Purchase Order Date</th>
                <th>Vendor</th>
                <th>Total Amount</th>
            </tr>

            <tr style="border: 2px solid black;border-bottom: none;border-top: none;">
                <td>@item.Id</td>
                <td>@item.DocNumber</td>
                <td>@item.TxnDate.ToString("MM/dd/yyyy")</td>
                <td>@item.VendorRef.name</td>
                <td>@item.TotalAmt</td>
            </tr>
            <tr style="border: 2px solid black;border-top: none;">
                @*<td>Items Details</td>*@
                <td colspan="5">
                    <table class="table table-bordered">
                        <tr>
                            <td colspan="5" style="text-align:center;"><label>Items Details</label></td>
                        </tr>
                        <tr>
                            <th>Line Id</th>
                            <th>Item Name</th>
                            <th>Qty</th>
                            <th>Total Amount</th>
                            <th>Description</th>
                        </tr>
                        @foreach (var LineItem in item.Line)
                        {
                            <tr>
                                <td>@LineItem.Id</td>
                                @{
                                    ItemBasedExpenseLineDetail Expenseitem = (ItemBasedExpenseLineDetail)LineItem.AnyIntuitObject;
                                }
                                <td>@Expenseitem.ItemRef.name</td>
                                <td>
                                    @if (Expenseitem.QtySpecified)
                                    {
                                        @Expenseitem.Qty
                                    }
                                </td>
                                <td>
                                    @if (LineItem.AmountSpecified)
                                    {
                                        @LineItem.Amount
                                    }
                                </td>
                                <td>@LineItem.Description</td>
                            </tr>
                        }
                    </table>
                </td>
            </tr>
        }

    </table>
</div>
  • We can also write a query according to our requirements, as like below
string EXISTING_PURCHASEORDER_QUERYBYID = string.Format("select * from PurchaseOrder where id = '{0}'", "155");
PurchaseOrder objPurchaseOrderFound = queryService.ExecuteIdsQuery(EXISTING_PURCHASEORDER_QUERYBYID).FirstOrDefault<PurchaseOrder>();
  • it will return a purchase order by id.
  • We will get a purchase order which id=”155” in the objPurchaseOrderFound object.

So that’s how we can get purchase orders or query purchase orders from Quickbooks online.

Output:

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories