Skip to main content
Published: September 11 2008, 4:40:00 PMUpdated: August 09 2022, 1:32:48 AM

How can I get all the categories for my store?

Summary  

GetStore returns the store categories hierarchically.  Since the categories can be nested very differently from level to level, you essentially need to use recursive logic to retrieve all of them.


Detailed Description  

Here is a sample C# code using the .NET SDK to retrieve all the store categories. 

using eBay.Service.Core.Sdk;
using eBay.Service.Core.Soap;
using eBay.Service.Call;
using eBay.Service.Util;

namespace NETSDKSample
{
   
public class SDKSample
   
{
       
private void GetStore()
       
{ 
            //set your credentials for the call 
           
ApiContext context = new ApiContext();
            context.ApiCredential.ApiAccount.Developer = "devID";
            
context.ApiCredential.ApiAccount.Application = "appID";
           
  context.ApiCredential.ApiAccount.Certificate = "certID";
            
context.ApiCredential.eBayToken = "token";
           
// Set the URL
           
context.SoapApiServerUrl = "https://api.sandbox.ebay.com/wsapi";
           
// Set logging
           
context.ApiLogManager = new ApiLogManager();
           
context.ApiLogManager.ApiLoggerList.Add(new eBay.Service.Util.FileLogger("Messages.log", true, true, true));
           
context.ApiLogManager.EnableLogging = true;
           
// Set the version
           
context.Version = "571";
           
//create call
           
GetStoreCall call = new GetStoreCall(context);
           
//get just the store categories
           
call.CategoryStructureOnly = true;
           
call.Execute();

            //iterate through the top level categories
           
foreach (StoreCustomCategoryType cat in call.Store.CustomCategories)
           
{
               
GetChildCategories(cat);
           
}

        }

        private void GetChildCategories(StoreCustomCategoryType cat)
       
{
           
//get the category name, ID and whether it is a leaf
           
long id = cat.CategoryID;
           
string name = cat.Name;
           
bool leaf = (cat.ChildCategory.Count == 0);
           
Console.WriteLine("id = " + id + " name = " + name + " leaf= " + leaf);

            //condition to end the recursion
           
if (leaf)
           
{
               
return;
           
}

            //continue the recursion for each of the child categories
           
foreach (StoreCustomCategoryType childcat in cat.ChildCategory)
           
{
               
GetChildCategories(childcat);
           
}
       
}

    }

}



Additional Information

Documentation:  GetStore

 

How well did this answer your question?
Answers others found helpful

Got thoughts? Click the feedback button – your insights help us improve!