Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > Wrapper libraries >

Contact Groups (.NET Wrapper)

Started by David Stubbins -   in Wrapper libraries

Hi,

I'm trying to use the .NET wrapper library to add a batch of contacts to a contact group - either when creating a new contact group, or to an existing one.

My code looks as follows:

XeroApi.Model.ContactGroups groups = new XeroApi.Model.ContactGroups();
XeroApi.Model.ContactGroup grp = new XeroApi.Model.ContactGroup();

grp.Name = "455455";
grp.Status = "ACTIVE";
grp.ContactGroupID = new System.Guid("a32811b6-270b-493e-a845-be34acf379c3");
grp.Contacts = new XeroApi.Model.Contacts();

XeroApi.Model.Contact contact = new XeroApi.Model.Contact();
contact.ContactID = new Guid("BAB779BC-C5FB-42CB-A888-953E8309711C");
contact.Name = "7-Eleven";
grp.Contacts.Add(contact);

contact = new XeroApi.Model.Contact();
contact.ContactID = new Guid("699F0091-B127-4796-9F15-41A2F42ABEB2");
contact.Name = "ABC Furniture";
grp.Contacts.Add(contact);

groups.Add(grp);

var result = repository.UpdateOrCreate(groups);

I have even tried:

var result = repository.UpdateOrCreate<XeroApi.Model.ContactGroup>(groups);

But nothing seems to work. I have managed to get a contact group added through the API, but even when passing through the contact group id alongside contacts with ids, nothing seems to happen.

Any help would very much be appreciated

Thanks
Your using the old wrapper and contact groups has not been added to the old wrapper
https://github.com/XeroAPI/Xero-Net

Kind Regards
Henzard Kruger
Certified Advisor and API developer
Picahoo cc - 0711304241 - henzard@picahoo.co.za
 

Henzard Kruger  

Hi all,

I have a small issue not being able to get the list of Contacts in a specific Contact Group (using the ContactGroup GUID when issuing the query) .

I am using the latest Xero.API.SDK.Minimal V 2.2.8 under VB Dotnet

I have used the Xero wrapper for a number of years successfully, and never encountered any issues until now, I am sure I must be doing something wrong, but cannot figure it out.

The below line of code will retrieve the ContactGroup in question, but it has 0 Contacts inside and this is not the case as there are two contacts under this Contact Group when I look in Xero.

Dim myXeroContactGroup = xSession.ContactGroups.Where("ContactGroupID = Guid(""" & ContactGroupID & """)").Find

My understanding is that when using the GUID of the ContactGroup it should trigger the return of the list of Contacts, but this is not happening when I inspect the myXeroContactGroup element in the debugger.

Any advice is highly welcome, thank you

 

Edward Re  

Did you ever get a workaround for this? I have the same issue. It seems it is impossible to add contacts to a contactgroup using the Xero SDK wrapper
 

David Hodge  

Hi David,
Edward and I solved may of his issues. Maybe you can tell us exactly what problem your facing. C# / VB is it New wrapper old wrapper. ASP / MVC. All this will help alot is solving your issue.
 

Henzard Kruger  

hi Guys I am still struggling with this issue of not being able to add a Customer to a Group even though I can read all the Customers who are associated with a specific group. I am using the latest wrapper by the way.
 

Edward Re  

Hi Edward,

It's a easy fix https://developer.xero.com/documentation/api/contactgroups#PUT
You get the contact group object and then use the add contact function on the group. We have not chatted in a while so if you don't get it working I will be happy to help.
 

Henzard Kruger  

Hi Henzard

sure, always happy to have your help/consultancy, as I am travelling Interstate (Australia) this week lets agree to chat next week.

thanks again
 

Edward Re  

Hi Henzard,

I am using c# with the wrapper Xero.API.SDK.Minimal V 2.2.8 & as it doesn't add contacts to contactgroup correctly, I also tried Xero.API.SDK v2.2.8, but still to no avail.

Adding contacts by themselves works fine. However when I try & add them to contactgroup, it doesn't work. There is no error. I will post some code.

As far as I can tell, it should be as simple as this:
contactGroup.Contacts.Add(newContact);
contactGroup = Api.ContactGroups.Update(contactGroup);
Where newContact is either a contact pulled back from Xero or a new contact that has been saved & has a guid from Xero. In the above code, the returned contactGroup will have an empty contacts list but no error is thrown.

Because the API documentation suggests a PUT and the example passes the guid and then a collection of contacts, I also tried this approach using the method below:
internal ContactGroup ContactGroupUpdateContacts(Guid groupid, List<Contact> contacts)
{
var contactGroup = new ContactGroup
{
Id = groupid,
Contacts = contacts
};
return Api.ContactGroups.Update(contactGroup);
}

I have tried many different variations, but nothing I do seems to work, yet there is no error.

Any help is much appreciated.
 

David Hodge  

Hi David,

Just tried again, I have the same behaviour in Visual Basic:-
no error which is good,
I pre fetch the Contact ,
then prefetch the ContactGroup by GUID,
then add the Contact to the ContactGroup ...
but when I look at the Contact in Xero I cannot see it has been added to the group.

Here is the code:-
Dim user As ApiUser = New ApiUser()
user.Name = Environment.MachineName

Dim cert As X509Certificate2 = New X509Certificate2(XeroCertificatePath, XeroCertificatePassword)
Dim myConsumer As Xero.Api.Infrastructure.OAuth.Consumer = New Consumer(XeroConsumerKey, XeroConsumerSecret)
Dim MyPrivateAuthenticator = New PrivateAuthenticator(cert)

Dim xSession As XeroCoreApi = New XeroCoreApi("https://api.xero.com", New PrivateAuthenticator(cert), myConsumer, user, New DefaultMapper(), New DefaultMapper())
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12


Dim myXeroContactGroup = xSession.ContactGroups.Find("509d6038-80c3-43b2-bb32-c1a7c7a1608f") 'this can fetch the group using the GUID
'Dim myXeroContactGroup = xSession.ContactGroups.Where("Name=""" & ContactGroupName & """").Find 'this can fetch using the Group Name

myXeroContactGroup.Contacts.Add(uContact) 'add the fetched Contact to the Contact Group

xSession.Update(myXeroContactGroup.Contacts) 'update the session
 

Edward Re  

Edward,

To me, it looks like we've discovered a bug in the wrapper. Henzard, what do you think?
 

David Hodge  

Hi David,

Good idea to ask Henzard as he is a guru in this matters.

We should also be asking whoever supports the Xero Wrapper, does anyone know how to let Xero officially know of the potential bug?
 

Edward Re  

I tried adding a bug on github when I saw a similar issue. Somebody then provided this link to a test in the source code:
https://github.com/XeroAPI/Xero-Net/blob/master/CoreTests/Integration/ContactGroups/Add_Contact.cs#L11-L16
Now it works. I'm so glad I can move on from this part.

That link highlights the code that works for me. I would post it here, but the a validation error raises when I paste the C# code. Very strange indeed.

That should work though

---------------------
Edward, try this:
Try this:

Api.ContactGroups(contactgroup.Id).Add(contact)

Let me know how you get on. The forum won't let me post any new ones so I have to edit a previous entry of mine
 

David Hodge  

still not working for me! :-(
 

Edward Re