Microsoft Advertising APIs
Create Ad Groups in C#

The following C# example shows how to create ad groups by using the Campaign Management Web service.

For information about creating a campaign and retrieving the campaign identifier, see Create Campaigns in C#.

static void CreateAdGroups(
    long campaignId,
    string username,
    string password,
    string appToken,
    string devToken,
    string customerAccountId,
    string customerId,
    long accountId)
{
    CampaignManagementServiceClient service;
    AddAdGroupsRequest request;
    AddAdGroupsResponse response;

    // Create two ad groups.
    AdGroup[] adGroups = new AdGroup[2];

    // Specify values for the first ad group.
    adGroups[0] = new AdGroup();
    adGroups[0].Name = "Eyewear";
    adGroups[0].AdDistribution = AdDistribution.Search;
    adGroups[0].BiddingModel = BiddingModel.Keyword;
    adGroups[0].PricingModel = PricingModel.Cpc;

    // Set the ad group to run from today
    // through December 31, 2012, inclusive.
    adGroups[0].StartDate = new Date();
    adGroups[0].StartDate.Day = DateTime.Today.Day;
    adGroups[0].StartDate.Month = DateTime.Today.Month;
    adGroups[0].StartDate.Year = DateTime.Today.Year;
    adGroups[0].EndDate = new Date();
    adGroups[0].EndDate.Day = 31;
    adGroups[0].EndDate.Month = 12;
    adGroups[0].EndDate.Year = 2012;
    adGroups[0].LanguageAndRegion = "EnglishUnitedStates";
    adGroups[0].NegativeKeywords = null;
    adGroups[0].ExactMatchBid = new Bid();
    adGroups[0].ExactMatchBid.Amount = 10.25;
    adGroups[0].NegativeKeywords = new string[]
    {
        "dog eyewear",
        "cat eyewear", 
        "decorative eyewear"
    };

    // Specify values for the second ad group.
    adGroups[1] = new AdGroup();
    adGroups[1].Name = "Headwear";
    adGroups[1].AdDistribution = AdDistribution.Search;
    adGroups[1].BiddingModel = BiddingModel.Keyword;
    adGroups[1].PricingModel = PricingModel.Cpc;
    
    // Set the ad group to run from today
    // through December 31, 2012, inclusive.
    adGroups[1].StartDate = new Date();
    adGroups[1].StartDate.Day = DateTime.Today.Day;
    adGroups[1].StartDate.Month = DateTime.Today.Month;
    adGroups[1].StartDate.Year = DateTime.Today.Year;
    adGroups[1].EndDate = new Date();
    adGroups[1].EndDate.Day = 31;
    adGroups[1].EndDate.Month = 12;
    adGroups[1].EndDate.Year = 2012;
    adGroups[1].LanguageAndRegion = "EnglishUnitedStates";
    adGroups[1].NegativeKeywords = null;
    adGroups[1].ExactMatchBid = new Bid();
    adGroups[1].ExactMatchBid.Amount = 5.00;
    adGroups[1].PhraseMatchBid = new Bid();
    adGroups[1].PhraseMatchBid.Amount = .75;

    // Create an instance of the CampaignManagement Web service.
    service = new CampaignManagementServiceClient();

    request = new AddAdGroupsRequest();

    request.AdGroups = adGroups;
    request.ApplicationToken = appToken;
    request.CampaignId = campaignId;
    request.CustomerAccountId = customerAccountId;
    request.CustomerId = customerId;
    request.DeveloperToken = devToken;
    request.Password = password;
    request.UserName = username;

    try
    {
        response = service.AddAdGroups(request);
    }

    // Exception handling.

    // Capture any generic errors.
    catch (FaultException<AdApiFaultDetail> adApiFault)
    {
        // Get the AdApiFaultDetail object.
        AdApiFaultDetail detail = adApiFault.Detail;
        foreach (AdApiError error in detail.Errors)
        {
            Console.Write("Ad API error");
            Console.WriteLine(" '{0}' ({1}) encountered.",
                              error.Message,
                              error.Code);
        }
    }

    // Capture any service operation exceptions.
    catch (FaultException<ApiFaultDetail> apiFault)
    {
        ApiFaultDetail detail = apiFault.Detail;

        // Display service operation error information.
        foreach (OperationError opError in detail.OperationErrors)
        {
            Console.Write("Operation error");
            Console.WriteLine(" '{0}' ({1}) encountered.",
                              opError.Message,
                              opError.Code);
        }

        // Display batch error information.
        // The batchError.index property is the index to the 
        // campaign's array.
        foreach (BatchError batchError in detail.BatchErrors)
        {
            Console.Write("Batch error");
            Console.Write(" '{0}' ({1}) encountered",
                           batchError.Message,
                           batchError.ErrorCode);
            Console.Write(" for ad group named {0}.",
                          adGroups[batchError.Index].Name);
            Console.WriteLine(" Ad group array index = {0}.",
                              batchError.Index);

        }
    }

    // Capture exceptions on the client that are unrelated to
    // the adCenter API. An example would be an 
    // out-of-memory condition on the client.
    catch (Exception e)
    {
        Console.WriteLine("Error '{0}' encountered.",
            e.Message);
    }

    finally
    {
        service.Close();
    }
}

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

When you have received the ad group identifiers, you should store them for future use. For information about why storing the identifiers 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 C# and Create Keywords in C#, respectively.

See Also

Concepts

Create Ads in C#
Create Keywords in C#
Create Campaigns in C#

Page view tracker