Using webapi to find contacts based on custom field

lock
push_pin
done
Answered
9

Can anyone guide me to an example using the webapi to query contacts based on a custom field? ie. find contact where myCustomId = 1234

 

18 Sep 2023 | 11:36 AM

All Replies (9)

Hi Snorre,

Assuming you know that in the APIs companies are contacts and contacts are persons... 

The Custom Objects page, specifically the searching custom objects section, in docs details how to translate a custom field (user-defined field (udef) or extra field) into a search criteria field.

With that, here is a raw HTTP GET request to select a company ID and name, where the custom field x_hassupportagreement == 0.

GET https://{{env}}.superoffice.com/{{tenant}}/api/v1/contact?$select=contactId,name&$filter=contactExtra/x_hassupportagreement eq 0 HTTP/1.1
Authorization: Bearer {{token}}
Content-Type: application/json
Accept: application/json
 
If the custom field is a udef, and the progid is "SuperOffice:3", the it would appear as:
GET https://{{env}}.superoffice.com/{{tenant}}/api/v1/contact?$select=contactId,name&$filter=contactUdef/SuperOffice:3 eq 0 HTTP/1.1
Authorization: Bearer {{token}}
Content-Type: application/json
Accept: application/json
 
For more details on search API, see the Search API pages on docs.
 
If you are using one of the proxy clients, let us know and we can change the examples accordingly.
 
 
 
 
18 Sep 2023 | 01:05 PM
Thanks a lot! If you could add a version using the webapi client it would be great.
19 Sep 2023 | 04:27 AM

using raw http request, when requesting this GET:

 https://sod2.superoffice.com/Cust42245/api/v1/person?$select=personId,name&$filter=personExtra/x_personid%20eq%201234

I receive 200 and this, which I assume means "no hits"

{
  "odata.metadata": "https://sod2.superoffice.com:443/Cust42245/api/v1/Archive//$metadata",
  "odata.nextLink": null,
  "value": []
}

PersonID is a short text field, can that be the problem?

 

19 Sep 2023 | 05:51 AM
I managed to make this work. there must have been something wrong with the extra field because the value suddenly disappeared from superoffice. When entereing it again the GET worked as expected. Still struggling to make it work using the webapi client library though :)
19 Sep 2023 | 06:15 AM

I was hoping this would work, but it throws the exception "Forbidden Forbidden".

        ArchiveListItem[] results = await archiveAgent.GetArchiveListByColumnsAsync(
            "ContactPersonDynamicSelectionV2",
            new []{"personId","firstName"},
            new[]
            {
                new ArchiveOrderByInfo() 
                {
                    Name = "personId", 
                    Direction = OrderBySortType.ASC 
                }
            },
            new[]
            {
                new ArchiveRestrictionInfo()
                {
                    Name = "personExtra/x_personid",
                    Operator = "=",
                    Values = new [] { "1234" }
                }
            },
            new[] { "person" },
            0,
            int.MaxValue);
19 Sep 2023 | 08:44 AM

Hi Snorre,

Only use the selection providers if you have a comprehensive understanding of the SuperOffice Find API

You can simplify by using the Person archive provider instead.

EDIT: When using the SuperOffice.WebApi nuget package, make sure to se IsActive to true.

19 Sep 2023 | 12:01 PM
great. but the link you provided didn't have any example using the webapi client. Is that possible?
19 Sep 2023 | 12:14 PM

Example:

 

var archiveAgent = new ArchiveAgent(config);

var provider = "Person";
var entities = new[] { "person" };
var personId = new[] { "12345"  };

var columns = new string[] { "personId", "firstName" };

var restrictions = new ArchiveRestrictionInfo[]
{
    new ArchiveRestrictionInfo()
    {
        Name = "personExtra/x_personid",
        Operator = "=",
        Values = personId,
        IsActive = true
    }
};

var sortOrders = new ArchiveOrderByInfo[] 
{ 
    new ArchiveOrderByInfo() 
    { 
        Name = "personId", 
        Direction = OrderBySortType.ASC 
    } 
};

results = await archiveAgent.GetArchiveListByColumnsAsync(
    provider, 
    columns, 
    sortOrders, 
    restrictions, 
    entities, 
    0, 
    int.MaxValue);

Hope this helps.

19 Sep 2023 | 12:29 PM
Great! I got the providerName confused. You should add an example like this to the top of the Search api doc. :) Thanks.
19 Sep 2023 | 12:48 PM

Add reply