-
Notifications
You must be signed in to change notification settings - Fork 199
Working with Selectors
When working with the AdWords API, you need to build a Selector to retrieve objects through the get()
methods. The AdWords API .NET library includes various utility methods to help
you build selectors easily.
When building a selector, you can use the object initializer syntax to initialize the selector. For instance, the following selector retrieves the id, name and status of all the campaigns in your account:
Selector selector = new Selector() {
fields = new string[] {
Campaign.Fields.Id, Campaign.Fields.Name, Campaign.Fields.Status
},
paging = Paging.Default
};
When building a selector, you need to specify selector field names that correspond to the fields you wish to retrieve. The autogenerated stub for
each type contains a nested SelectableFields
subclass that lists all the valid selector field values that may be used with
that type. For instance, if you need to retrieve the id, name and status fields of a Campaign, you’d specify
Selector selector = new Selector() {
fields = new string[] {
Campaign.SelectableFields.Id,
Campaign.SelectableFields.Name,
Campaign.SelectableFields.Status
},
...
};
When building selectors, you can specify predicates to filter your results. The Predicate
class has utility extension methods that allows you to easily build
predicates. For instance, the following code snippet retrieves the id and name of all ad groups that belong to campaign ID 1234.
Selector selector = new Selector() {
fields = new string[] { AdGroup.Fields.Id, AdGroup.Fields.Name },
predicates = new Predicate[] {
Predicate.Equals(AdGroup.FilterableFields.CampaignId, 1234)
},
...
};
The supported predicate operators are listed below.
Method Signature | Details |
Equals(string field, long value) |
Adds a filter to select only items where the value of |
NotEquals(string field, string value) |
Adds a filter to select only items where the value of |
In(string field, List<string> values) |
Adds a filter to select only items where the value of |
NotIn(string field, string[] values) |
Adds a filter to select only items where the value of |
GreaterThan(string field, string value) |
Adds a filter to select only items where the value of |
GreaterThanEquals(string field, string value) |
Adds a filter for items where the value of |
LessThan(string field, string value) |
Adds a filter to select only items where the value of |
LessThanEquals(string field, string value) |
Adds a filter to select only items where the value of |
StartsWith(string field, string value) |
Adds a filter to select only items where the value of |
StartsWithIgnoreCase(string field, string value) |
Adds a filter to select only items where the value of |
Contains(string field, string value) |
Adds a filter to select only items where the value of |
ContainsIgnoreCase(string field, string value) |
Adds a filter to select only items where the value of |
DoesNotContain(string field, string value) |
Adds a filter to select only items where the value of |
DoesNotContainIgnoreCase(string field, string value) |
Adds a filter to select only items where the value of |
ContainsAll(string field, string[] values) |
Adds a filter to select only items where the value of |
ContainsNone(string field, string[] values) |
Adds a filter to select only items where the value of |
ContainsAny(string field, string[] values) |
Adds a filter to select only items where the value of |
When retrieving objects, be aware that the number of results may exceed the maximum page size. You should download the results as multiple pages whose size is below the maximum page size supported by AdWords API. The Paging class has utility methods that help you loop through the pages using the recommended page size. The following code snippet shows how this may be done.
Selector selector = new Selector() {
fields = new string[] {
Campaign.Fields.Id, Campaign.Fields.Name, Campaign.Fields.Status
},
paging = Paging.Default
};
do {
// Get the campaigns.
page = campaignService.get(selector);
// Process the results.
if (page != null && page.entries != null) {
int i = selector.paging.startIndex;
foreach (Campaign campaign in page.entries) {
...
}
}
selector.paging.IncreaseOffset();
} while (selector.paging.startIndex < page.totalNumEntries);
The above code snippet sets a default page size using the Paging.Default
method and then and uses the IncreaseOffset()
method to iterate through successive pages until all the items are retrieved.
You can use a custom page size as follows:
Selector selector = new Selector() {
...
paging = new Paging() {
startIndex = 0,
numberResults = YOUR_CUSTOM_PAGE_SIZE
};
};
...
selector.paging.IncreaseOffsetBy(YOUR_CUSTOM_PAGE_SIZE);
You may also sort the results by specifying the ordering field of the selector. The OrderBy class has utility methods to simplify your code. The following selector retrieves the list of AdGroups, and sorts them by their name in ascending order.:
Selector selector = new Selector() {
fields = new string[] { AdGroup.Fields.Id, AdGroup.Fields.Name },
ordering = new OrderBy[] {OrderBy.Asc(AdGroup.Fields.Name)}
...
};
The supported methods are listed below.
Method Signature | Details |
Asc(string field) |
Creates an ascending sorting order to be used with a selector. |
Desc(string field) |
Creates a descending sorting order to be used with a selector. |