Hi Ramon,
Here's a code example that shows how to handle policy violations for
Ads in .NET. allOperations hold the set of all AdGroupAdOperations.
List<AdGroupAdOperation> allOperations = new
List<AdGroupAdOperation>();
List<AdGroupAdOperation> operationsToBeRemoved = new
List<AdGroupAdOperation>();
allOperations.AddRange(new AdGroupAdOperation[] { textAdOperation });
try {
// Call the service in validateOnly mode.
service.RequestHeader.validateOnly = true;
retVal = service.mutate(allOperations.ToArray());
} catch (AdWordsApiException ex) {
ApiException innerException = ex.ApiException as ApiException;
if (innerException != null) {
foreach (ApiError error in innerException.errors) {
if (error is PolicyViolationError) {
PolicyViolationError policyError =
(PolicyViolationError)error;
int index = ErrorUtilities.GetOperationIndex(policyError);
if (index == -1) {
throw new Exception(string.Format(
"Could not retrieve operation index for '{0}'.",
policyError.fieldPath));
}
if (policyError.isExemptable) {
List<ExemptionRequest> exemptionRequests = new
List<ExemptionRequest>();
if (allOperations[index].exemptionRequests != null) {
exemptionRequests.AddRange(allOperations[index].exemptionRequests);
}
ExemptionRequest exemptionRequest = new ExemptionRequest();
exemptionRequest.key = policyError.key;
exemptionRequests.Add(exemptionRequest);
allOperations[index].exemptionRequests =
exemptionRequests.ToArray();
} else {
operationsToBeRemoved.Add(allOperations[index]);
}
}
}
// Remove all operations that aren't exemptable.
foreach (AdGroupAdOperation operation in operationsToBeRemoved) {
allOperations.Remove(operation);
}
}
}
// Set valiateOnly to false.
service.RequestHeader.validateOnly = false;
retVal = service.mutate(allOperations.ToArray());
ErrorUtilities.GetOperationIndex looks like follows:
public class ErrorUtilities {
public static int GetOperationIndex(ApiError apiError) {
string[] parts = apiError.fieldPath.Split('.');
if (parts.Length > 0 && parts[0].StartsWith("operations[")) {
int index = 0;
if (int.TryParse(parts[0].Split(new char[] { '[', ']' })[1],
out index)) {
return index;
}
}
return -1;
}
}
Hope this helps. I'll add this as part of the .NET library examples
during the next release.
Cheers,
Anash P. Oommen,
AdWords API Advisor.
On Aug 10, 7:02 pm, RamonPV <[email protected]> wrote:
> I have been trying to find a .net example of a PolicyViolationError
> handling but it has been impossible; The only example that I found is
> coded with PHP(http://code.google.com/p/google-api-adwords-php/source/
> browse/trunk/examples/v201003/HandlePolicyViolationError.php); does
> anybody has an example a PolicyViolationError handling for Ads or
> keywords using .net?. I am using v201003 of the .net Adwords library.
>
> Any help is greatly appreciated...
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
http://adwordsapi.blogspot.com
http://groups.google.com/group/adwords-api
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en