Export (0) Print
Expand All

Negative Keywords in Java

This example shows how to associate negative keywords and negative keyword lists with a campaign using the following Campaign Management service operations.

This example has been developed and run within the environment described in Getting Started Using Java with Bing Ads Services.

To download the Bing Ads JARs imported in the sample, see Java Production JARs for Bing Ads API Version 9.

System_CLiX_note Note

This example uses the UserName and Password elements for authentication. For Managing User Authentication with OAuth, replace the UserName and Password elements with the AuthenticationToken, which is your OAuth access token.

import java.rmi.*;
import java.text.*;
import java.util.ArrayList;

import bingads.campaignmanagement.*;
import bingads.campaignmanagement.adapi.*;
import bingads.campaignmanagement.schemas.generic.*;

/**
 *
 */
public class NegativeKeywords {

     private static java.lang.String _namespace = null;
     private static BasicHttpBinding_ICampaignManagementServiceStub _service = null;

     // Specify API credentials.
     
     private static java.lang.String UserName = "<UserNameGoesHere>";
     private static java.lang.String Password = "<PasswordGoesHere>";
     private static java.lang.String DeveloperToken = "<DeveloperTokenGoesHere>";
     private static long AccountId = <AccountIdGoesHere>;

    
     /**
      */
     public static void main(java.lang.String[] args) {
         CampaignManagementServiceLocator locator = null;
         
         try
         {
             locator = new CampaignManagementServiceLocator();
             _namespace = locator.getServiceName().getNamespaceURI();
             _service = (BasicHttpBinding_ICampaignManagementServiceStub) locator.getBasicHttpBinding_ICampaignManagementService();

             // Set the header properties.

             _service.clearHeaders();
             _service.setHeader(_namespace, "CustomerAccountId", AccountId);
             _service.setHeader(_namespace, "DeveloperToken", DeveloperToken);
             _service.setHeader(_namespace, "UserName", UserName);
             _service.setHeader(_namespace, "Password", Password);
            
             // Specify one or more campaigns.

             Campaign campaign = new Campaign();
             campaign.setName("Winter Clothing");
             campaign.setDescription("Winter clothing line.");
             campaign.setBudgetType(BudgetLimitType.MonthlyBudgetSpendUntilDepleted);
             campaign.setMonthlyBudget(1000.00);
             campaign.setTimeZone("PacificTimeUSCanadaTijuana");
             campaign.setDaylightSaving(true);

             // Add the campaign
             long[] campaignIds = AddCampaigns(AccountId, new Campaign[] { campaign });

             // Print the new assigned campaign identifier

             PrintCampaignIdentifiers(campaignIds);

             // You may choose to associate an exclusive set of negative keywords to an individual campaign 
             // or ad group. An exclusive set of negative keywords cannot be shared with other campaigns 
             // or ad groups. This sample only associates negative keywords with a campaign.

             NegativeKeyword negativeKeyword = new NegativeKeyword();
             negativeKeyword.setMatchType(MatchType.Phrase);
             negativeKeyword.setText("auto");
             
             EntityNegativeKeyword entityNegativeKeyword = new EntityNegativeKeyword();
             entityNegativeKeyword.setEntityId(campaignIds[0]);
             entityNegativeKeyword.setEntityType("Campaign");
             entityNegativeKeyword.setNegativeKeywords(new NegativeKeyword[] { negativeKeyword });

             AddNegativeKeywordsToEntitiesResponse addNegativeKeywordsToEntitiesResponse = 
                 AddNegativeKeywordsToEntities(new EntityNegativeKeyword[] { entityNegativeKeyword });
             if (addNegativeKeywordsToEntitiesResponse.getNestedPartialErrors() == null
                 || addNegativeKeywordsToEntitiesResponse.getNestedPartialErrors().length == 0)
             {
                 System.out.println("Added an exclusive set of negative keywords to the Campaign.\n");
                 PrintNegativeKeywordIds(addNegativeKeywordsToEntitiesResponse.getNegativeKeywordIds());
             }
             else
             {
                 PrintNestedPartialErrors(addNegativeKeywordsToEntitiesResponse.getNestedPartialErrors());
             }

             GetNegativeKeywordsByEntityIdsResponse getNegativeKeywordsByEntityIdsResponse = 
                 GetNegativeKeywordsByEntityIds(campaignIds, "Campaign", AccountId);
             if (getNegativeKeywordsByEntityIdsResponse.getPartialErrors() == null
                 || getNegativeKeywordsByEntityIdsResponse.getPartialErrors().length == 0)
             {
                 System.out.println("Retrieved an exclusive set of negative keywords for the Campaign.\n");
                 PrintEntityNegativeKeywords(getNegativeKeywordsByEntityIdsResponse.getEntityNegativeKeywords());
             }
             else
             {
                 PrintPartialErrors(getNegativeKeywordsByEntityIdsResponse.getPartialErrors());
             }
                
             // If you attempt to delete a negative keyword without an identifier the operation will
             // succeed but will return partial errors corresponding to the index of the negative keyword
             // that was not deleted. 
             BatchErrorCollection[] nestedPartialErrors = DeleteNegativeKeywordsFromEntities(
                 new EntityNegativeKeyword[] { entityNegativeKeyword });
             if (nestedPartialErrors == null || nestedPartialErrors.length == 0)
             {
                 System.out.println("Deleted an exclusive set of negative keywords from the Campaign.\n");
             }
             else
             {
                 System.out.println("Attempt to DeleteNegativeKeywordsFromEntities without NegativeKeyword identifier partially fails by design.\n");
                 PrintNestedPartialErrors(nestedPartialErrors);
             }

             // Delete the negative keywords with identifiers that were returned above.
             nestedPartialErrors = DeleteNegativeKeywordsFromEntities(
                 getNegativeKeywordsByEntityIdsResponse.getEntityNegativeKeywords());
             if (nestedPartialErrors == null || nestedPartialErrors.length == 0)
             {
                 System.out.println("Deleted an exclusive set of negative keywords from the Campaign.\n");
             }
             else
             {
                 PrintNestedPartialErrors(nestedPartialErrors);
             }

             // Negative keywords can also be added and deleted from a shared negative keyword list. 
             // The negative keyword list can be shared or associated with multiple campaigns.
             // NegativeKeywordList inherits from SharedList which inherits from SharedEntity.

             NegativeKeywordList negativeKeywordList = new NegativeKeywordList();
             negativeKeywordList.setName("My Negative Keyword List");
             negativeKeywordList.setType("NegativeKeywordList");

             SharedListItem[] negativeKeywords = new SharedListItem[2];
             negativeKeywords[0] = new NegativeKeyword();
             ((NegativeKeyword)negativeKeywords[0]).setText("car");
             ((NegativeKeyword)negativeKeywords[0]).setType("NegativeKeyword");
             ((NegativeKeyword)negativeKeywords[0]).setMatchType(MatchType.Exact);
             negativeKeywords[1] = new NegativeKeyword();
             ((NegativeKeyword)negativeKeywords[1]).setText("car");
             ((NegativeKeyword)negativeKeywords[1]).setType("NegativeKeyword");
             ((NegativeKeyword)negativeKeywords[1]).setMatchType(MatchType.Phrase);

             // You can create a new list for negative keywords with or without negative keywords.
                
             AddSharedEntityResponse addSharedEntityResponse = AddSharedEntity(negativeKeywordList, negativeKeywords);
             long sharedEntityId = addSharedEntityResponse.getSharedEntityId();
             long[] listItemIds = addSharedEntityResponse.getListItemIds();

             System.out.printf("NegativeKeywordList successfully added to account library and assigned identifer %d\n\n", sharedEntityId);

             PrintNegativeKeywordResults(
                 sharedEntityId,
                 negativeKeywords, 
                 listItemIds, 
                 addSharedEntityResponse.getPartialErrors());

             System.out.println("Negative keywords currently in NegativeKeywordList:");
             negativeKeywordList.setId(sharedEntityId);
             negativeKeywords = GetListItemsBySharedList(negativeKeywordList);
             if (negativeKeywords == null || negativeKeywords.length == 0)
             {
                 System.out.println("None\n");
             }
             else
             {
                 PrintNegativeKeywords(negativeKeywords);
             }             

             // To update the list of negative keywords, you must either add or remove from the list
             // using the respective AddListItemsToSharedList or DeleteListItemsFromSharedList operations.
             // To remove the negative keywords from the list pass the negative keyword identifers
             // and negative keyword list (SharedEntity) identifer.

             BatchError[] partialErrors = DeleteListItemsFromSharedList(listItemIds, negativeKeywordList);
             if (partialErrors == null || partialErrors.length == 0)
             {
                 System.out.println("Deleted most recently added negative keywords from negative keyword list.\n");

             }
             else
             {
                 PrintPartialErrors(partialErrors);
             }
                
             System.out.println("Negative keywords currently in NegativeKeywordList:");
             negativeKeywords = GetListItemsBySharedList(negativeKeywordList);
             if (negativeKeywords == null || negativeKeywords.length == 0)
             {
                 System.out.println("None\n");
             }
             else
             {
                 PrintNegativeKeywords(negativeKeywords);
             }
                
             // Whether you created the list with or without negative keywords, more can be added 
             // using the AddListItemsToSharedList operation.

             negativeKeywords = new SharedListItem[2];
             negativeKeywords[0] = new NegativeKeyword();
             ((NegativeKeyword)negativeKeywords[0]).setText("auto");
             ((NegativeKeyword)negativeKeywords[0]).setType("NegativeKeyword");
             ((NegativeKeyword)negativeKeywords[0]).setMatchType(MatchType.Exact);
             negativeKeywords[1] = new NegativeKeyword();
             ((NegativeKeyword)negativeKeywords[1]).setText("auto");
             ((NegativeKeyword)negativeKeywords[1]).setType("NegativeKeyword");
             ((NegativeKeyword)negativeKeywords[1]).setMatchType(MatchType.Phrase);

             AddListItemsToSharedListResponse addListItemsToSharedListResponse = AddListItemsToSharedList(
                 negativeKeywords,
                 negativeKeywordList);
             listItemIds = addListItemsToSharedListResponse.getListItemIds();

             PrintNegativeKeywordResults(
                 sharedEntityId,
                 negativeKeywords, 
                 listItemIds, 
                 addListItemsToSharedListResponse.getPartialErrors());

             System.out.println("Negative keywords currently in NegativeKeywordList:");
             negativeKeywords = GetListItemsBySharedList(negativeKeywordList);
             if (negativeKeywords == null || negativeKeywords.length == 0)
             {
                 System.out.println("None\n");
             }
             else
             {
                 PrintNegativeKeywords(negativeKeywords);
             }

             // You can update the name of the negative keyword list. 

             negativeKeywordList.setName("My Updated Negative Keyword List");
             negativeKeywordList.setType("NegativeKeywordList");
                
             partialErrors = UpdateSharedEntities(new SharedEntity[] { negativeKeywordList });
             if (partialErrors == null || partialErrors.length == 0)
             {
                 System.out.printf("Updated Negative Keyword List Name to %s.\n\n", negativeKeywordList.getName());
             }
             else
             {
                 PrintPartialErrors(partialErrors);
             }

             // Get and print the negative keyword lists and return the list of identifiers.

             final java.lang.String sharedEntityType = "NegativeKeywordList";
             long[] sharedEntityIds = GetAndPrintSharedEntityIdentifiers(sharedEntityType);
                
             // Negative keywords were added to the negative keyword list above. You can associate the 
             // shared list of negative keywords with a campaign with or without negative keywords. 
             // Shared negative keyword lists cannot be associated with an ad group. An ad group can only 
             // be assigned an exclusive set of negative keywords. 

             SharedEntityAssociation[] associations = new SharedEntityAssociation[1];
             associations[0] = new SharedEntityAssociation();
             associations[0].setEntityId(campaignIds[0]);
             associations[0].setEntityType("Campaign");
             associations[0].setSharedEntityId(sharedEntityId);
             associations[0].setSharedEntityType("NegativeKeywordList");

             partialErrors = SetSharedEntityAssociations(associations);
             if (partialErrors == null || partialErrors.length == 0)
             {
                 System.out.printf("Associated CampaignId %d with Negative Keyword List Id %d.\n\n", campaignIds[0], sharedEntityId);
             }
             else
             {
                 PrintPartialErrors(partialErrors);
             }
                
             // Get and print the associations either by Campaign or NegativeKeywordList identifier.
             GetSharedEntityAssociationsByEntityIdsResponse getSharedEntityAssociationsByEntityIdsResponse = 
                 GetSharedEntityAssociationsByEntityIds(new long[] { campaignIds[0] }, "Campaign", "NegativeKeywordList");
             PrintSharedEntityAssociations(getSharedEntityAssociationsByEntityIdsResponse.getAssociations());
             PrintPartialErrors(getSharedEntityAssociationsByEntityIdsResponse.getPartialErrors());
                
             GetSharedEntityAssociationsBySharedEntityIdsResponse getSharedEntityAssociationsBySharedEntityIdsResponse = 
                 GetSharedEntityAssociationsBySharedEntityIds("Campaign", new long[] { sharedEntityIds[sharedEntityIds.length-1] }, "NegativeKeywordList");
             PrintSharedEntityAssociations(getSharedEntityAssociationsBySharedEntityIdsResponse.getAssociations());
             PrintPartialErrors(getSharedEntityAssociationsBySharedEntityIdsResponse.getPartialErrors());
                
             // Explicitly delete the association between the campaign and the negative keyword list.

             partialErrors = DeleteSharedEntityAssociations(associations);
             if (partialErrors == null || partialErrors.length == 0)
             {
                 System.out.println("Deleted NegativeKeywordList associations\n");
             }
             else
             {
                 PrintPartialErrors(partialErrors);
             }
                
             // Delete the campaign and any remaining assocations. 

             DeleteCampaigns(AccountId, new long[] { campaignIds[0] });
             System.out.printf("Deleted CampaignId %d\n\n", campaignIds[0]);

             // DeleteCampaigns does not delete the negative keyword list from the account's library. 
             // Call the DeleteSharedEntities operation to delete the shared entities.

             partialErrors = DeleteSharedEntities(new SharedEntity[] { negativeKeywordList });
             if (partialErrors == null || partialErrors.length == 0)
             {
                 System.out.printf("Deleted Negative Keyword List (SharedEntity) Id %d\n\n", sharedEntityId);
             }
             else
             {
                 PrintPartialErrors(partialErrors);
             }
         }
         // Campaign Management service operations can throw AdApiFaultDetail.
         catch (AdApiFaultDetail fault)
         {
             // Log this fault.

             System.out.println("The operation failed with the following faults:\n");

             // If the AdApiError array is not null, the following are examples of error codes that may be found.
             for (AdApiError error : fault.getErrors())
             {
                 System.out.printf("AdApiError\n");
                 System.out.printf("Code: %d\nError Code: %s\nMessage: %s\n\n", error.getCode(), error.getErrorCode(), error.getMessage());

                 switch (error.getCode())
                 {
                     default:
                         System.out.println("Please see MSDN documentation for more details about the error code output above.");
                         break;
                 }
             }
         }
         // Campaign Management service operations can throw ApiFaultDetail.
         catch (ApiFaultDetail fault)
         {
             // Log this fault.

             System.out.println("The operation failed with the following faults:\n");

             // If the BatchError array is not null, the following are examples of error codes that may be found.
             for (BatchError error : fault.getBatchErrors())
             {
                 System.out.printf("BatchError at Index: %d\n", error.getIndex());
                 System.out.printf("Code: %d\nMessage: %s\n\n", error.getCode(), error.getMessage());

                 switch (error.getCode())
                 {
                     default:
                         System.out.println("Please see MSDN documentation for more details about the error code output above.");
                         break;
                 }
             }

             // If the OperationError array is not null, the following are examples of error codes that may be found.
             for (OperationError error : fault.getOperationErrors())
             {
                 System.out.printf("OperationError\n");
                 System.out.printf("Code: %d\nMessage: %s\n\n", error.getCode(), error.getMessage());

                 switch (error.getCode())
                 {
                     default:
                         System.out.println("Please see MSDN documentation for more details about the error code output above.");
                         break;
                 }
             }
         }
         // Some Campaign Management service operations such as SetAdExtensionsAssociations can throw EditorialApiFaultDetail.
         catch (EditorialApiFaultDetail fault)
         {
             // Log this fault.

             System.out.println("The operation failed with the following faults:\n");

             // If the BatchError array is not null, the following are examples of error codes that may be found.
             for (BatchError error : fault.getBatchErrors())
             {
                 System.out.printf("BatchError at Index: %d\n", error.getIndex());
                 System.out.printf("Code: %d\nMessage: %s\n\n", error.getCode(), error.getMessage());

                 switch (error.getCode())
                 {
                     default:
                         System.out.println("Please see MSDN documentation for more details about the error code output above.");
                         break;
                 }
             }
             
             // If the EditorialError array is not null, the following are examples of error codes that may be found.
             for (EditorialError error : fault.getEditorialErrors())
             {
                 System.out.printf("EditorialError at Index: %d\n\n", error.getIndex());
                 System.out.printf("Code: %d\nMessage: %s\n\n", error.getCode(), error.getMessage());
                 System.out.printf("Appealable: %s\nDisapproved Text: %s\nCountry: %s\n\n", error.getAppealable(), error.getDisapprovedText(), error.getPublisherCountry());
                     
                 switch (error.getCode())
                 {
                     default:
                         System.out.println("Please see MSDN documentation for more details about the error code output above.");
                         break;
                 }
             }

             // If the OperationError array is not null, the following are examples of error codes that may be found.
             for (OperationError error : fault.getOperationErrors())
             {
                 System.out.printf("OperationError\n");
                 System.out.printf("Code: %d\nMessage: %s\n\n", error.getCode(), error.getMessage());

                 switch (error.getCode())
                 {
                     default:
                         System.out.println("Please see MSDN documentation for more details about the error code output above.");
                         break;
                 }
             }
         }
         catch (RemoteException e)
         {
             System.out.println("Service communication error encountered: ");
             System.out.println(e.getMessage());
             e.printStackTrace();
         }
         catch (Exception e)
         {
             // Ignore fault exceptions that we already caught.

             if ( e.getCause() instanceof AdApiFaultDetail ||
                  e.getCause() instanceof EditorialApiFaultDetail ||
                  e.getCause() instanceof ApiFaultDetail )
             {
                 ;
             }
             else
             {
                 System.out.println("Error encountered: ");
                 System.out.println(e.getMessage());
                 e.printStackTrace();
             }
         }
     }

     // Adds one or more campaigns to the specified account.

     static long[] AddCampaigns(long accountId, Campaign[] campaigns) throws RemoteException, Exception
     {
         AddCampaignsRequest request = new AddCampaignsRequest();
         
         // Set the request information.

         request.setAccountId(accountId);
         request.setCampaigns(campaigns);

         return _service.addCampaigns(request).getCampaignIds();
     }
     
     // Deletes one or more campaigns from the specified account.

     static void DeleteCampaigns(long accountId, long[] campaignIds) throws RemoteException, Exception
     {
    	 DeleteCampaignsRequest request = new DeleteCampaignsRequest();
         
         // Set the request information.

         request.setAccountId(accountId);
         request.setCampaignIds(campaignIds);

         _service.deleteCampaigns(request);
     }

     // Prints the campaign identifiers for each campaign added. 

     static void PrintCampaignIdentifiers(long[] campaignIds)
     {
         if (campaignIds == null)
         {
             return;
         }

         for (long id : campaignIds)
         {
             System.out.printf("Campaign successfully added and assigned CampaignId %d\n\n", id);
         }
     }

     // Adds list items such as negative keywords to the corresponding list.

     static AddListItemsToSharedListResponse AddListItemsToSharedList(
            SharedListItem[] listItems, 
            SharedList sharedList) throws RemoteException, Exception
     {
    	 AddListItemsToSharedListRequest request = new AddListItemsToSharedListRequest();
         
         request.setListItems(listItems);
         request.setSharedList(sharedList);
         
         return _service.addListItemsToSharedList(request);
     }

     // Adds a shared entity such as a negative keyword list to the account's library. 
     // Lists in the account's library can be associated with any campaign within the account.

     static AddSharedEntityResponse AddSharedEntity(
            SharedEntity sharedEntity,
            SharedListItem[] listItems) throws RemoteException, Exception
     {
    	 AddSharedEntityRequest request = new AddSharedEntityRequest();
         
         request.setSharedEntity(sharedEntity);
         request.setListItems(listItems);
         
         return _service.addSharedEntity(request);
     }

     // Deletes list items such as negative keywords from the corresponding list. 

     static BatchError[] DeleteListItemsFromSharedList(
            long[] listItemIds, 
            SharedList sharedList) throws RemoteException, Exception
     {
    	 DeleteListItemsFromSharedListRequest request = new DeleteListItemsFromSharedListRequest();
         
         request.setListItemIds(listItemIds);
         request.setSharedList(sharedList);
         
         return _service.deleteListItemsFromSharedList(request).getPartialErrors();
     }

     // Deletes shared entities such as negative keyword lists from the account's library.

     static BatchError[] DeleteSharedEntities(SharedEntity[] sharedEntities) throws RemoteException, Exception
     {
    	 DeleteSharedEntitiesRequest request = new DeleteSharedEntitiesRequest();
         
         request.setSharedEntities(sharedEntities);
         
         return _service.deleteSharedEntities(request).getPartialErrors();
     }

     // Removes the association between a shared entity such as a negative keyword list and an entity such as a campaign. 

     static BatchError[] DeleteSharedEntityAssociations(SharedEntityAssociation[] associations) throws RemoteException, Exception
     {
    	 DeleteSharedEntityAssociationsRequest request = new DeleteSharedEntityAssociationsRequest();
         
         request.setAssociations(associations);
         
         return _service.deleteSharedEntityAssociations(request).getPartialErrors();
     }

     // Gets the list items such as the negative keywords of a negative keyword list. 

     static SharedListItem[] GetListItemsBySharedList(SharedList sharedList) throws RemoteException, Exception
     {
    	 GetListItemsBySharedListRequest request = new GetListItemsBySharedListRequest();
         
         request.setSharedList(sharedList);
         
         return _service.getListItemsBySharedList(request).getListItems();
     }

     // Gets the shared entities such as negative keyword lists from the account's library.  

     static SharedEntity[] GetSharedEntitiesByAccountId(java.lang.String sharedEntityType) throws RemoteException, Exception
     {
    	 GetSharedEntitiesByAccountIdRequest request = new GetSharedEntitiesByAccountIdRequest();
         
         request.setSharedEntityType(sharedEntityType);
         
         return _service.getSharedEntitiesByAccountId(request).getSharedEntities();
     }

     // Gets associations between a campaign and a shared entity such as a negative keyword list. 
     // You can request associations by associated entity identifiers.

     static GetSharedEntityAssociationsByEntityIdsResponse GetSharedEntityAssociationsByEntityIds(
            long[] entityIds,
            java.lang.String entityType, 
            java.lang.String sharedEntityType) throws RemoteException, Exception
     {
    	 GetSharedEntityAssociationsByEntityIdsRequest request = new GetSharedEntityAssociationsByEntityIdsRequest();
         
         request.setEntityIds(entityIds);
         request.setEntityType(entityType);
         request.setSharedEntityType(sharedEntityType);
         
         return _service.getSharedEntityAssociationsByEntityIds(request);
     }

     // Gets associations between a campaign and a shared entity such as a negative keyword list. 
     // You can request associations by shared entity identifiers.

     static GetSharedEntityAssociationsBySharedEntityIdsResponse GetSharedEntityAssociationsBySharedEntityIds(
            java.lang.String entityType, 
            long[] sharedEntityIds,
            java.lang.String sharedEntityType) throws RemoteException, Exception
     {
    	 GetSharedEntityAssociationsBySharedEntityIdsRequest request = new GetSharedEntityAssociationsBySharedEntityIdsRequest();
         
         request.setEntityType(entityType);
         request.setSharedEntityIds(sharedEntityIds);
         request.setSharedEntityType(sharedEntityType);
         
         return _service.getSharedEntityAssociationsBySharedEntityIds(request);
     }

     // Sets the association between a campaign and a shared entity such as a negative keyword list. 

     static BatchError[] SetSharedEntityAssociations(SharedEntityAssociation[] associations) throws RemoteException, Exception
     {
    	 SetSharedEntityAssociationsRequest request = new SetSharedEntityAssociationsRequest();
         
         request.setAssociations(associations);
         
         return _service.setSharedEntityAssociations(request).getPartialErrors();
     }

     // Updates shared entities such as negative keyword lists within the account's library. 

     static BatchError[] UpdateSharedEntities(SharedEntity[] sharedEntities) throws RemoteException, Exception
     {
    	 UpdateSharedEntitiesRequest request = new UpdateSharedEntitiesRequest();
         
         request.setSharedEntities(sharedEntities);
         
         return _service.updateSharedEntities(request).getPartialErrors();
     }

     // Adds negative keywords to the specified campaign or ad group.  

     static AddNegativeKeywordsToEntitiesResponse AddNegativeKeywordsToEntities(EntityNegativeKeyword[] entityNegativeKeywords) throws RemoteException, Exception
     {
    	 AddNegativeKeywordsToEntitiesRequest request = new AddNegativeKeywordsToEntitiesRequest();
         
         request.setEntityNegativeKeywords(entityNegativeKeywords);
         
         return _service.addNegativeKeywordsToEntities(request);
     }

     // Deletes negative keywords from the specified campaign or ad group.  

     static BatchErrorCollection[] DeleteNegativeKeywordsFromEntities(EntityNegativeKeyword[] entityNegativeKeywords) throws RemoteException, Exception
     {
    	 DeleteNegativeKeywordsFromEntitiesRequest request = new DeleteNegativeKeywordsFromEntitiesRequest();
         
         request.setEntityNegativeKeywords(entityNegativeKeywords);
         
         return _service.deleteNegativeKeywordsFromEntities(request).getNestedPartialErrors();
     }

     // Gets the negative keywords that are only associated with the specified campaigns or ad groups.  

     static GetNegativeKeywordsByEntityIdsResponse GetNegativeKeywordsByEntityIds(
            long[] entityIds, 
            java.lang.String entityType, 
            long parentEntityId) throws RemoteException, Exception
     {
    	 GetNegativeKeywordsByEntityIdsRequest request = new GetNegativeKeywordsByEntityIdsRequest();
         
         request.setEntityIds(entityIds);
         request.setEntityType(entityType);
         request.setParentEntityId(parentEntityId);
         
         return _service.getNegativeKeywordsByEntityIds(request);
     }
     
     // Prints the negative keyword identifiers added to each campaign or ad group entity. 
     // The IdCollection items are available by calling AddNegativeKeywordsToEntities.  

     static void PrintNegativeKeywordIds(IdCollection[] idCollections)
     {
         if (idCollections == null)
         {
             return;
         }

         for (int index = 0; index < idCollections.length; index++)
         {
             System.out.printf("NegativeKeyword Ids at entity index %d:\n\n", index);
             for (long id : idCollections[index].getIds())
             {
                 System.out.printf("\tId: %d\n\n", id);
             }
         }
     }

     // Prints the negative keywords  

     static void PrintNegativeKeywords(SharedListItem[] sharedListItems)
     {
         if (sharedListItems == null)
         {
             return;
         }

         for (SharedListItem sharedListItem : sharedListItems)
         {
             if(sharedListItem instanceof NegativeKeyword)
             {
                 System.out.printf("\tNegativeKeyword Text: %s\n", ((NegativeKeyword)sharedListItem).getText());
                 System.out.printf("\tId: %d\n", ((NegativeKeyword)sharedListItem).getId());
                 System.out.printf("\tMatchType: %s\n\n", ((NegativeKeyword)sharedListItem).getMatchType());
             }
         }
     }

     // Prints a list of EntityNegativeKeyword objects  

     static void PrintEntityNegativeKeywords(EntityNegativeKeyword[] entityNegativeKeywords)
     {
         if (entityNegativeKeywords == null)
         {
             return;
         }

         System.out.println("EntityNegativeKeyword items:\n");
         for (EntityNegativeKeyword entityNegativeKeyword : entityNegativeKeywords)
         {
             System.out.printf("\tEntityId: %d\n", entityNegativeKeyword.getEntityId());
             System.out.printf("\tEntityType: %s\n\n", entityNegativeKeyword.getEntityType());
             PrintNegativeKeywords(entityNegativeKeyword.getNegativeKeywords());
         }
     }

     // Prints a list of EntityNegativeKeyword objects  

     static long[] GetAndPrintSharedEntityIdentifiers(java.lang.String sharedEntityType) throws RemoteException, Exception
     {
         SharedEntity[] sharedEntities = GetSharedEntitiesByAccountId(sharedEntityType);
         long[] sharedEntityIds = new long[sharedEntities.length];
         for (int index = 0; index < sharedEntities.length; index++)
         {
             SharedEntity sharedEntity = sharedEntities[index];
             if (sharedEntity.getId() != null)
             {
                 sharedEntityIds[index] = (long)sharedEntity.getId();
                 System.out.printf("SharedEntity[%d] (%s) has SharedEntity Id %d.\n\n",
                                   index,
                                   sharedEntities[index].getName(),
                                   sharedEntities[index].getId());
             }
         }
         return sharedEntityIds;
     }

     // Prints a list of BatchError objects that represent partial errors while managing negative keywords.

     static void PrintPartialErrors(BatchError[] partialErrors)
     {
         if (partialErrors == null || partialErrors.length == 0)
         {
             return;
         }

         System.out.println("BatchError (PartialErrors) items:\n");
         for (BatchError error : partialErrors)
         {
             System.out.printf("\tIndex: %d\n", error.getIndex());
             System.out.printf("\tCode: %d\n", error.getCode());
             System.out.printf("\tErrorCode: %s\n", error.getErrorCode());
             System.out.printf("\tMessage: %s\n\n", error.getMessage());

             // In the case of an EditorialError, more details are available
             if (error.getType().equals("EditorialError") && error.getErrorCode().equals("CampaignServiceEditorialValidationError"))
             {
                 System.out.printf("\tDisapprovedText: %s\n", ((EditorialError)(error)).getDisapprovedText());
                 System.out.printf("\tLocation: %s\n", ((EditorialError)(error)).getLocation());
                 System.out.printf("\tPublisherCountry: %s\n", ((EditorialError)(error)).getPublisherCountry());
                 System.out.printf("\tReasonCode: %d\n\n", ((EditorialError)(error)).getReasonCode());
             }
         }
     }

     // Prints the list item identifiers, as well as any partial errors  

     static void PrintNegativeKeywordResults(
            long sharedListId,
            SharedListItem[] sharedListItems, 
            long[] sharedListItemIds, 
            BatchError[] partialErrors)
     {
         if (sharedListItemIds == null)
         {
             return;
         }

         for (int index = 0; index < sharedListItems.length; index++)
         {
             // Determine if the SharedListItem is a NegativeKeyword.
             if (sharedListItems[index] instanceof NegativeKeyword)
             {
                 // Determine if the corresponding index has a valid identifier
                 if(sharedListItemIds[index] > 0)
                 {
                     System.out.printf("NegativeKeyword[%d] (%s) successfully added to NegativeKeywordList (%d) and assigned Negative Keyword Id %d.\n",
                                   index,
                                   ((NegativeKeyword)(sharedListItems[index])).getText(),
                                   sharedListId,
                                   sharedListItemIds[index]);
                 }
             }
             else
             {
                 System.out.println("SharedListItem is not a NegativeKeyword.");
             }
         }

         System.out.println();

         PrintPartialErrors(partialErrors);
     }

     // Prints a list of SharedEntityAssociation objects.

     static void PrintSharedEntityAssociations(SharedEntityAssociation[] associations)
     {
         if (associations == null || associations.length == 0)
         {
             return;
         }

         System.out.println("SharedEntityAssociation items:\n");
         for (SharedEntityAssociation sharedEntityAssociation : associations)
         {
             System.out.printf("\tEntityId: %d\n", sharedEntityAssociation.getEntityId());
             System.out.printf("\tEntityType: %s\n", sharedEntityAssociation.getEntityType());
             System.out.printf("\tSharedEntityId: %d\n", sharedEntityAssociation.getSharedEntityId());
             System.out.printf("\tSharedEntityType: %s\n\n", sharedEntityAssociation.getSharedEntityType());
         }
     }

     // Prints a list of BatchErrorCollection objects that represent partial errors while managing 
     // negative keywords.

     static void PrintNestedPartialErrors(BatchErrorCollection[] nestedPartialErrors)
     {
         if (nestedPartialErrors == null || nestedPartialErrors.length == 0)
         {
             return;
         }

         System.out.println("BatchErrorCollection (NestedPartialErrors) items:\n");
         for (BatchErrorCollection collection : nestedPartialErrors)
         {
             // The top level list index corresponds to the campaign or ad group index identifier.
             if (collection != null)
             {
                 if (collection.getCode() != null)
                 {
                     System.out.printf("\tIndex: %d\n", collection.getIndex());
                     System.out.printf("\tCode: %d\n", collection.getCode());
                     System.out.printf("\tErrorCode: %s\n", collection.getErrorCode());
                     System.out.printf("\tMessage: %s\n\n", collection.getMessage());
                 }

                 PrintPartialErrors(collection.getBatchErrors());
             }
         }         
     }
 }

Community Additions

ADD
Show:
© 2014 Microsoft