using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
// Application-specific reference to the V5 CampaignManagement WSDL.
using adCenter.CampaignManagementService;
namespace MyCreateAdGroups
{
class Program
{
static void Main(string[] args)
{
// Define variables for use in this application.
CampaignManagementServiceClient svcCampMgt;
ApplicationToken appToken;
DeveloperToken devToken;
UserCredentials userCreds;
AddAdGroupsRequest addAdGroupsRequest;
AddAdGroupsResponse addAdGroupsResponse;
int campaignID = 924790; // An application-specific value.
AdGroup[] adGroups = new AdGroup[2];
int i;
try
{
// The user name, password, and developer token
// are passed in as command-line arguments.
if (args.Length != 3)
{
Console.WriteLine("Correct usage:");
Console.Write("MyCreateAdGroups ");
Console.WriteLine("username password devtoken");
Environment.Exit(0);
}
// Create an instance of the CampaignManagement Web service.
svcCampMgt = new CampaignManagementServiceClient();
// Create instances of the tokens and credentials.
appToken = new ApplicationToken();
devToken = new DeveloperToken();
userCreds = new UserCredentials();
// Assign values for the credentials and 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
// doesn't need to be assigned a value.
userCreds.Username = args[0];
userCreds.Password = args[1];
devToken.Value = args[2];
// Create two ad groups.
adGroups[0] = new AdGroup();
adGroups[1] = new AdGroup();
// Specify values for the first ad group.
adGroups[0].Name = "Handwear";
adGroups[0].AdDistribution = AdDistribution.Search;
// Set the ad group to run March 1, 2008
// through December 31, 2012, inclusive.
adGroups[0].StartDate =
new adCenter.CampaignManagementService.Date();
adGroups[0].StartDate.Day = 1;
adGroups[0].StartDate.Month = 3;
adGroups[0].StartDate.Year = 2008;
adGroups[0].EndDate =
new adCenter.CampaignManagementService.Date();
adGroups[0].EndDate.Day = 31;
adGroups[0].EndDate.Month = 12;
adGroups[0].EndDate.Year = 2012;
adGroups[0].LanguageAndRegion =
LanguageAndRegion.EnglishUnitedStates;
adGroups[0].Status = null;
adGroups[0].NegativeKeywords = null;
// Specify values for the second ad group.
adGroups[1].Name = "Headwear";
adGroups[1].AdDistribution = AdDistribution.Search;
// Set the ad group to run March 1, 2008
// through December 31, 2012, inclusive.
adGroups[1].StartDate =
new adCenter.CampaignManagementService.Date();
adGroups[1].StartDate.Day = 1;
adGroups[1].StartDate.Month = 3;
adGroups[1].StartDate.Year = 2008;
adGroups[1].EndDate =
new adCenter.CampaignManagementService.Date();
adGroups[1].EndDate.Day = 31;
adGroups[1].EndDate.Month = 12;
adGroups[1].EndDate.Year = 2012;
adGroups[1].LanguageAndRegion =
LanguageAndRegion.EnglishUnitedStates;
adGroups[1].Status = null;
adGroups[1].NegativeKeywords = null;
addAdGroupsRequest = new AddAdGroupsRequest(
appToken,
null, // The customer account ID
// isn't used in this
// example.
devToken,
userCreds,
campaignID,
adGroups);
addAdGroupsResponse =
svcCampMgt.AddAdGroups(addAdGroupsRequest);
// Display the tracking ID.
Console.WriteLine(
"AddAdGroups TrackingId: {0}",
addAdGroupsResponse.ApiCallTrackingData.TrackingId);
// Display the ad group IDs for the successfully
// created ad groups.
for (i = 0;
i < addAdGroupsResponse.AdGroupIds.Length;
i++)
{
Console.Write("Ad group named {0} ",
adGroups[i].Name);
Console.Write("was created with ");
Console.WriteLine(" ID {0}.",
addAdGroupsResponse.AdGroupIds[i]);
}
}
// Exception handling.
// Capture Microsoft adCenter API exceptions.
catch (FaultException<ApiFaultDetail> fault)
{
ApiFaultDetail detail = fault.Detail;
// Display the 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.Code);
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);
}
}
}
}
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 How to Create Ads in C# (V5.1) and How to Create Keywords in C# (V5.1), respectively.