PDA

View Full Version : .net


liveland
21-08-08, 22:12
Hi,

I have been playing around with the API with in .NET using Visual Studio 2008, this has been somewhat difficult, as there is very little information available on how to do this, however I now seem to be making a bit of progress. The code below may help others having similar problems in how to get started :).

However I seem to have hit a problem with the getCategoryTree.

I thought setting up a Tree and setting the Id to say 4, would cause the getCategoryTree to return a Tree for Category 4.

However it doesn't seem to have this effect, as it always appears to return the 'top' tree (ie. All Categories with a ParentId of 0).

I created the following test code:

//Setup Web Service
ApiService awService = new ApiService();
UserAuthentication awUser = new UserAuthentication();
awUser.iId = 1234; //Your User Id
awUser.sPassword = ""; //Your V2 Hash
awUser.sType = UserType.affiliate;
awService.UserAuthenticationValue = awUser;

//Setup Category Tree
getCategoryTree testTree = new getCategoryTree();
getCategoryTreeResponse testTreeResponse = new getCategoryTreeResponse();

//Retrieve Category Tree
testTree.iCategoryId = 4;
testTreeResponse = awService.getCategoryTree(testTree);

//Display Category Tree
foreach (Category cat in testTreeResponse.oCategoryTree.oCategory)
{
Response.Write(cat.iId.ToString() + " " + cat.sDescription + "<br>");
}


Please can anyone advise how I return the Categories below in certain Tree, or return the entire Category Tree (so I could then filter it)?

I am also struggling to understand the relationship between categories and products, ie. How do I find all of the products in a category?

I would be grateful for any help anyone could provide.

Thanks
Stephen Livesey

Ollie
22-08-08, 08:39
Hi Stephen,

Welcome to the forum!

I cannot provide any .NET specific advice in regards to your category tree problem but may be able to help out more generally.

When using the category tree model you will always receive the top level nodes despite what you search. When you set iCategoryId to 4 you should find that you will expand the 'Electronics' category. What you will need todo, and I think you are almost there, is add a check within your foreach loop to see if the node you are currently looking at has its 'oCategoryTree' property set. If it does, you will need to expand this too.

You may find that you need to drill down multiple times (upto three) to get all the categorys children so may wish to use a recursive loop or something similar.

E.g (PHP: not tested, just for an example)

public function recurse($tree) {
echo $tree->sName;
if (isset($tree->oCategoryTree)) {
$this->recurse($tree->oCategoryTree);
}

The above calls itself until there are no children left.

Also, all products should be associated with a category. Therefore when you make a getProductList call to the API you need to provide the CategoryId as a refineBy. There is documentation on how to use the refineBys available within the affiliate area.

Hope this helps.

Cheers

indy2005
17-01-09, 11:37
Hi,

There seems to be a problem in .net. If you choose to return the category tree with everything expanded, you get the top level nodes....and no nodes expanded below it. Each individual oCategoryTree item below the top nodes is set to Nothing or Null, when it should be set to a category tree you can expand further.

I know this is supposed to be the behaviour because if you use SoapUI to make a call manually and check the response, you do get a full hierarchihcal set of XML return showing all the top level and then lower categories.

I am starting to think I may simply have to process the raw XML soap response to build this hierarchy, rather than using the nice .NET web service calls. I may hard code the hierarchy in a database, as I am sure it wont change too much.

When I tried the recursive approach...I blew my thousand call limit on one pass of building the hierarchy....I am sure you should be able to return the entire tree in one go.

SM

indy2005
18-01-09, 23:06
Hi,

After 3 days I managed to get the category tree returned in one go. The problem was that even though I was setting bExpandAllBranches to True, you also seem to have to tell it that you have set it to true by setting the bExpandAllBranchesSpecified to true also?

This doesnt make sense to me. Surely bExpandAllBranches to true should be enough. Documentation is not clear at all in this area.

Regards

i

Ollie
19-01-09, 09:04
Hi,

After 3 days I managed to get the category tree returned in one go. The problem was that even though I was setting bExpandAllBranches to True, you also seem to have to tell it that you have set it to true by setting the bExpandAllBranchesSpecified to true also?

This doesnt make sense to me. Surely bExpandAllBranches to true should be enough. Documentation is not clear at all in this area.

Regards

i

Hi there,

The documentation provided for the API is not language specific. It provides an overview of how to use the API and what parameters you need to pass. The 'variablenameSpecified' I believe is related to how you initialize variables using the SOAP API - a way of stating that you wish to pass them. That said, I have very little experience in .NET but would say it is not API related.

If I am indeed wrong, and there is something more seriously wrong with the API then I would be keen to hear back from you and if necessary maybe have a chat regarding how we can fix this.

Cheers

indy2005
20-01-09, 00:01
Hi,

The documentation specifies an argument bExpandAllBranches. I could only get the subtrees to come back if I also set bExpandAllBranchesSpecified to true also. I set it to true...and then have to tell it I set it to true....

.NET code to build a TreeView (note this is a telerik control, but a normal treeview is not too different).




Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim aAffiliateWindowService As New com.productserve.api.ApiService
Dim aUserAuthentication As New com.productserve.api.UserAuthentication
Dim aGetCategoryTree As New com.productserve.api.getCategoryTree
Dim aNode As RadTreeNode
Dim aCategoryTreeResponse As com.productserve.api.getCategoryTreeResponse


aUserAuthentication.iId = XXXXX
aUserAuthentication.sPassword = "XXXX"
aUserAuthentication.sType = UserType.affiliate


aGetCategoryTree.iCategoryId = 0
aGetCategoryTree.bExpandAllBranches = True
aGetCategoryTree.bExpandAllBranchesSpecified = True


aAffiliateWindowService.UserAuthenticationValue = aUserAuthentication

tvCategory.Nodes.Clear()

Try
aCategoryTreeResponse = aAffiliateWindowService.getCategoryTree(aGetCatego ryTree)

For Each cat As com.productserve.api.Category In aCategoryTreeResponse.oCategoryTree.oCategory
If cat.bAdult = False Then

aNode = New RadTreeNode
aNode.Text = cat.sName
aNode.Value = cat.iId

tvCategory.Nodes.Add(aNode)

If Not (cat.oCategoryTree Is Nothing) Then

'aNode.Expanded = True

aNode.ExpandMode = TreeNodeExpandMode.ClientSide

Call Process_SubTree(aNode, cat.oCategoryTree)

End If
End If



Next


Catch ex As System.Web.Services.Protocols.SoapException
Response.Write(ex.Message)
End Try

' Now all the top level categories have been built
' then we need to process the sub categories recursively



End Sub

Public Sub Process_SubTree(ByVal aParentNode As RadTreeNode, ByVal aCatTree As com.productserve.api.CategoryTree)

Dim aNode As RadTreeNode

For Each cat As com.productserve.api.Category In aCatTree.oCategory


aNode = New RadTreeNode
aNode.Text = cat.sName
aNode.Value = cat.iId

aParentNode.Nodes.Add(aNode)



If Not (cat.oCategoryTree Is Nothing) Then

If cat.bAdult = False Then

aNode.ExpandMode = TreeNodeExpandMode.ClientSide

Call Process_SubTree(aNode, cat.oCategoryTree)

End If

End If

Next


End Sub