Quickbooks SDK: Step by Step guide to read Items using QBFC in C#

Quickbooks SDK: Step by Step guide to read Items using QBFC in C#

Published Date: 2/23/2018

1. First of all we need to add a reference to Interop.QBFC13.dll and add the Interop.QBFC13 namespace to the top of the page.

 using Interop.QBFC13;

 

2. To request data from QuickBooks we create a QBSessionManager and open a connection. For this request to work, we need to have QuickBooks open and logged in to a company file.

QBSessionManager _MySessionManager = new QBSessionManager();
_MySessionManager.OpenConnection2("", "Your App Name", ENConnectionType.ctLocalQBD);
_MySessionManager.BeginSession("", ENOpenMode.omDontCare);

 

3. We create a IMsgSetRequest. We will need to append our request to this message set.

 IMsgSetRequest itemRequestset = _MySessionManager.CreateMsgSetRequest("US", 13, 0);

 

4. We then append our request to the above Message Set, in this example we want to get a list of all items from QuickBooks, so we add a AppendItemQueryRq().

 IItemQuery itemQuery = itemRequestset.AppendItemQueryRq();

 

5. We add "0" as OwnerID to give us all the items. Read more about OwnerID in the QBSDK Programmer’s Guide that comes with QuickBooks SDK.

 itemQuery.OwnerIDList.Add("0");

 

 

6. We then send the request to QuickBooks and we get our response in the form of a list. In this example we have performed only one request AppendItemQueryRq so the first item in “position 0” of the itemResponseList is the object we need. We can add more requests and get the response in the list.

IMsgSetResponse responseItemRq = _MySessionManager.DoRequests(itemRequestset);
IResponseList itemResponseList = responseItemRq.ResponseList;
IResponse itemResponse = itemResponseList.GetAt(0);

 

7. If you are sending more than one request, you can check your response type with the follow code.

 ENResponseType responseType = (ENResponseType)itemResponse.Type.GetValue();

 

8. Here we have performed only one request and we know that the response will always be a list of Items, so we convert the itemResponse.Detail to a IORItemRetList.

 IORItemRetList QBItemList = (IORItemRetList)itemResponse.Detail;

 

9. We then iterate our item list.

 for (int i = 0; i <= QBItemList.Count - 1; i++) {

 

10. Every inventory item is handled differently in QuickBooks, so here we check the type of the item whether it’s a InventoryItem, NonInventoryItem or AssemblyItem. While accessing inventory field values, If a field does not have a value, it will return null, so be sure to check for nulls first.

if (QBItemList.GetAt(i).ItemNonInventoryRet != null)
{
IItemNonInventoryRet inventoryItem = QBItemList.GetAt(i).ItemNonInventoryRet;
string ListID = inventoryItem.ListID != null ? inventoryItem.ListID.GetValue() : "";
string ItemCode = inventoryItem.Name != null ? inventoryItem.Name.GetValue() : "";
string Description = inventoryItem.FullName.GetValue() != null ? inventoryItem.FullName.GetValue() : "";
string MPN = inventoryItem.ManufacturerPartNumber != null ? inventoryItem.ManufacturerPartNumber.GetValue() : "";
}
else if (QBItemList.GetAt(i).ItemInventoryRet != null)
{
IItemInventoryRet inventoryItem = QBItemList.GetAt(i).ItemInventoryRet;
string ListID = inventoryItem.ListID != null ? inventoryItem.ListID.GetValue() : "";
string ItemCode = inventoryItem.Name != null ? inventoryItem.Name.GetValue() : "";
string Description = inventoryItem.SalesDesc != null ? inventoryItem.SalesDesc.GetValue() : "";
string MPN = inventoryItem.ManufacturerPartNumber != null ? inventoryItem.ManufacturerPartNumber.GetValue() : "";
string Price = inventoryItem.SalesPrice.GetValue();
}
else if (QBItemList.GetAt(i).ItemInventoryAssemblyRet != null)
{
IItemInventoryAssemblyRet inventoryItem = QBItemList.GetAt(i).ItemInventoryAssemblyRet;
customFieldsList = string ListID = inventoryItem.ListID != null ? inventoryItem.ListID.GetValue() : "";
string ItemCode = inventoryItem.Name != null ? inventoryItem.Name.GetValue() : "";
string Description = inventoryItem.SalesDesc.GetValue() != null ? inventoryItem.SalesDesc.GetValue() : "";
string MPN = inventoryItem.ManufacturerPartNumber != null ? inventoryItem.ManufacturerPartNumber.GetValue() : "";
}
}

If you like this, please share and like our page.

Author: Ahmad Faiq Ateel

By: Ahmad Faiq Ateel
Ahmad Faiq Ateel

Published: 2/23/2018