Click to Rate and Give Feedback
MSDN
MSDN Library
Live Services SDK
 Create Ad Groups in Java

  Switch on low bandwidth view
Microsoft Advertising APIs
Create Ad Groups in Java

The following Java example shows how to create ad groups by using the Campaign Management Web service. This example assumes that you have already determined which campaign ID will be used for the ad groups; you must substitute your campaign ID for the campaignID variable that is assigned 70350999 in the following code.

For information about creating a campaign and retrieving the campaign ID, see Create Campaigns in Java.

// This example assumes that you are using Axis 1.4 and
// the Xerxes parser. Additionally, it requires
// pre-compiling the proxy classes by using wsdl2java.
// If you are using a different environment, update it as needed.

// Application-specific name of package.
// If you place this code in a package, invoke the code
// at the command line by typing
// java java_adCenter.MyCreateAdGroups
// along with the parameters used for the credentials and developer token.
// If you choose to not place this code in a package, invoke the code
// at the command line by typing
// java MyCreateAdGroups
// along with the parameters used for the credentials and developer token.
package java_adCenter;

import java.io.*;
import java.util.*;
import java.net.URL;
import com.microsoft.adapi.*;
import com.microsoft.adcenter.v6.*;

public class MyCreateAdGroups
{
    public static void main(String[] args)
    {
        
        // Application-specific value.
        long accountId = 489; 
        long campaignId = 70350999;

        // The Microsoft adCenter API namespace,
        // which is used in the SOAP header.
        String namespace = 
            "https://adcenter.microsoft.com/v6";

        // This example URL is for the sandbox environment.
        // Update the URL as needed when using the production environment.
        String url = "https://sandboxapi.adcenter.microsoft.com/Api/Advertiser/v6/CampaignManagement/CampaignManagementService.svc?wsdl";
        // The following is the production environment URL.
        // String url = "https://adcenterapi.microsoft.com/Api/Advertiser/v6/CampaignManagement/CampaignManagementService.svc?wsdl";

        ICampaignManagementService campaignManagement = null;

        CampaignManagementServiceLocator campaignServiceLocator = null;

        BasicHttpBinding_ICampaignManagementServiceStub stub = null;

        AddAdGroupsRequest addAdGroupsRequest = null;
        AddAdGroupsResponse addAdGroupsResponse = null;

        AdGroup[] adGroups = null;
        
        com.microsoft.adcenter.v6.Date startDate = null, endDate = null;

        try
        {
            
            if (args.length != 3)
            {
                // This application expects the user name, password, and
                // developer token to be passed in as command-line arguments.
                System.out.println("Correct usage:");
                System.out.print("java MyCreateAdGroups ");
                System.out.println("user_name password developertoken");
                System.exit(-1);
            }

            campaignServiceLocator = new
              CampaignManagementServiceLocator();

            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);

            campaignManagement =
             campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService();

            stub = (BasicHttpBinding_ICampaignManagementServiceStub)
                          campaignManagement;

            // Assign values for the credentials and the developer token.
            // args[0] is the user name.
            // args[1] is the password.
            // args[2] is the developer token.
            // The application token is reserved for future use and 
            // does not need to be assigned a value.

            stub.setHeader(namespace,
                     "ApplicationToken", "");
            stub.setHeader(namespace,
                     "DeveloperToken", args[2]);
            stub.setHeader(namespace,
                     "UserName", args[0]);
            stub.setHeader(namespace,
                     "Password", args[1]);
            stub.setHeader(namespace,
                     "CustomerAccountId", accountId);

            // This example creates two ad groups.
            adGroups = new AdGroup[2];
            adGroups[0] = new AdGroup();
            adGroups[1] = new AdGroup();

            // These ad groups will be for search.
            // Update as needed if other distributions are required.
            String[] adDistribution = new String[1];
            adDistribution[0] = "Search";

            // Set the ad groups to run September 1, 2009
            // through December 31, 2012, inclusive.
            startDate = new com.microsoft.adcenter.v6.Date();
            endDate = new com.microsoft.adcenter.v6.Date();
            startDate.setDay(1);
            startDate.setMonth(9);
            startDate.setYear(2009);
            endDate.setDay(31);
            endDate.setMonth(12);
            endDate.setYear(2012);

            // Specify values for the first ad group.
            adGroups[0].setName("Hand wear");
            adGroups[0].setAdDistribution(adDistribution);
            adGroups[0].setBiddingModel(BiddingModel.Keyword);
            adGroups[0].setPricingModel(PricingModel.Cpc);
            adGroups[0].setExactMatchBid(new Bid(10.25));
            adGroups[0].setStartDate(startDate);
            adGroups[0].setEndDate(endDate);
            adGroups[0].setLanguageAndRegion("EnglishUnitedStates");
            adGroups[0].setNegativeKeywords(null);

            // Specify values for the second ad group.
            adGroups[1].setName("Head wear");
            adGroups[1].setAdDistribution(adDistribution);
            adGroups[1].setBiddingModel(BiddingModel.Keyword);
            adGroups[1].setPricingModel(PricingModel.Cpc);
            adGroups[1].setExactMatchBid(new Bid(5.00));
            adGroups[1].setPhraseMatchBid(new Bid(.75));
            adGroups[1].setStartDate(startDate);
            adGroups[1].setEndDate(endDate);
            adGroups[1].setLanguageAndRegion("EnglishUnitedStates");
            adGroups[1].setNegativeKeywords(null);

            // Create the service operation request object, and then assign values.
            addAdGroupsRequest = new AddAdGroupsRequest();
            addAdGroupsRequest.setCampaignId(campaignId);
            addAdGroupsRequest.setAdGroups(adGroups);
            
            // Make the call to add the campaigns.
            System.out.println(
                "Calling CampaignManagement.AddAdGroup");
            addAdGroupsResponse = 
                campaignManagement.addAdGroups(addAdGroupsRequest);
            
            // Display the TrackingId that was returned 
            // in the response header.
            System.out.println("Successful AddAdGroups call.");
            System.out.println(
                "TrackingId output from response header:");
            System.out.println(
             stub.getResponseHeader
             (
               namespace,
               "TrackingId")
             );

            // Insert a blank line to separate the text that follows.
            System.out.println();  

            // Display the IDs for the ad groups that were created.
            for (int i = 0; i < addAdGroupsResponse.getAdGroupIds().length; i++)
            {
                System.out.print(
                  String.format("Ad group named %s ",
                  adGroups[i].getName()));
                System.out.print("was created with ");
                System.out.println(
                     String.format("ad group ID %s.",
                     addAdGroupsResponse.getAdGroupIds()[i]));
            }
        }

        // Exception handling.
        
        catch (ApiFaultDetail fault)
        {
            System.out.println("ApiFaultDetail exception encountered.");
            System.out.println(String.format("Tracking ID: %s",
                      fault.getTrackingId()));

            // Display service operation error information.
            for (int i = 0; i < fault.getOperationErrors().length; i++)
            {
                System.out.println("Operation error encountered:");
                System.out.println(String.format("\tMessage: %s",
                        fault.getOperationErrors()[i].getMessage()));
                System.out.println(String.format("\tDetails: %s",
                        fault.getOperationErrors()[i].getDetails()));
                System.out.println(String.format("\tErrorCode: %s",
                        fault.getOperationErrors()[i].getErrorCode()));
                System.out.println(String.format("\tCode: %s",
                        fault.getOperationErrors()[i].getCode()));
            }

            // Display batch error information.
            for (int i = 0; i < fault.getBatchErrors().length; i++)
            {
                System.out.println(String.format("Batch error encountered for array index %s.",
                        fault.getBatchErrors()[i].getIndex()));
                System.out.println(String.format("\tMessage: %s",
                        fault.getBatchErrors()[i].getMessage()));
                System.out.println(String.format("\tDetails: %s",
                        fault.getBatchErrors()[i].getDetails()));
                System.out.println(String.format("\tErrorCode: %s",
                        fault.getBatchErrors()[i].getErrorCode()));
                System.out.println(String.format("\tCode: %s",
                        fault.getBatchErrors()[i].getCode()));
            }

            // Exit the application or take other action.
            System.exit(-1);
        }
        
        catch (AdApiFaultDetail fault)
        {
            System.out.println("AdApiFaultDetail exception encountered.");
            System.out.println(String.format("Tracking ID: %s",
                      fault.getTrackingId()));

            // Display API error information.
            for (int i = 0; i < fault.getErrors().length; i++)
            {
                System.out.println("Error encountered:");
                System.out.println(String.format("\tMessage: %s",
                        fault.getErrors()[i].getMessage()));
                System.out.println(String.format("\tDetail: %s",
                        fault.getErrors()[i].getDetail()));
                System.out.println(String.format("\tErrorCode: %s",
                        fault.getErrors()[i].getErrorCode()));
                System.out.println(String.format("\tCode: %s",
                        fault.getErrors()[i].getCode()));
            }

            // Exit the application or take other action.
            System.exit(-1);
        }

        // Capture exceptions on the client that are unrelated to
        // the adCenter API.
        catch (Exception e)
        {
            System.out.print("Error encountered: ");
            e.printStackTrace();
            
            // Exit the application or take other action.
            System.exit(-1);
        }

    }
}

Each successfully created ad group is assigned an ad group ID by adCenter. The ad group ID is stored in the Id element of the AdGroup object.

When you have received the ad group IDs, you should store them for future use. For information about why storing the IDs is recommended, see Creating an Efficient Microsoft adCenter Application. If you don't store them, you can retrieve them by calling the GetAdGroupsByCampaignId service operation. You can retrieve the properties associated with an ad group by calling the GetAdGroupsByIds service operation. Calling GetAdGroupsByCampaignId or GetAdGroupsByIds reduces your API quota. For more information, see Quotas in Microsoft adCenter.

Now that you have successfully created your ad groups, the next step is to create ads and keywords. For examples of these tasks, see Create Ads in Java and Create Keywords in Java, respectively.

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker