The following Perl example shows how to create campaigns by using the Campaign Management Web service. This example assumes that you have already determined which account will be used for the campaigns; you must substitute your account identifier for the $accountId variable in the following code.
After you have successfully created a campaign, you can create ad groups. For an example that shows how to create ad groups, see Create Ad Groups in Perl.
package adCenterAPI;
# For release code, use the following SOAP::Lite statement.
#use SOAP::Lite ( +maptype => {} );
# For debugging code, use the following SOAP::Lite statement.
use SOAP::Lite ( +trace => all, maptype => {} );
use strict;
eval
{
# Use either the sandbox or the production URI.
# This example is for the sandbox URI.
my $URI = "https://sandboxapi.adcenter.microsoft.com/Api/Advertiser/V6";
# The following commented-out line contains the production URI.
#my $URI = "https://adcenterapi.microsoft.com/Api/Advertiser/V6";
# The Microsoft adCenter API namespace.
my $xmlns = "https://adcenter.microsoft.com/v6";
# The proxy for the Campaign Management Web service.
my $campaignPROXY =
$URI."/CampaignManagement/CampaignManagementService.svc?wsdl";
# The common data-type namespace.
my $namespaceArrays =
"http://schemas.microsoft.com/2003/10/Serialization/Arrays";
# The service operation that will be called.
my $action = 'AddCampaigns';
# The Web service.
my $campaignManagementService = SOAP::Lite->new(
uri => $URI,
proxy => $campaignPROXY,
on_action => ( sub { return $action } ));
$campaignManagementService->autotype(0);
$campaignManagementService->multirefinplace(1);
$campaignManagementService->readable(1);
my $username = "username";
my $password = "password";
my $devToken = "devtoken";
my $appToken = "apptoken";
my $customerAccountId = 1234;
my $customerId = 5678;
my $accountId = 123456;
# Assemble the service operation parameters.
my @header = CreateV6Header(
$username,
$password,
$devToken,
$appToken,
$customerAccountId,
$customerId);
# Create an array of campaigns.
my @Campaigns = SOAP::Data->name(Campaigns => \SOAP::Data->value
(
SOAP::Data->name(Campaign => \SOAP::Data->value
(
SOAP::Data->name(BudgetType => 'MonthlyBudgetSpendUntilDepleted'),
SOAP::Data->name(ConversionTrackingEnabled => 'false'),
SOAP::Data->name(DailyBudget => 50),
SOAP::Data->name(DaylightSaving => 'true'),
SOAP::Data->name(Description => 'Winter Clothing Products'),
SOAP::Data->name(MonthlyBudget => 5000),
SOAP::Data->name(Name => 'Winter Clothing'),
SOAP::Data->name(NegativeKeywords => \SOAP::Data->value
(
SOAP::Data->name('a:string' => 'dog'),
SOAP::Data->name('a:string' => 'cat')
)
)->attr({'xmlns:i' => $namespaceArrays}),
SOAP::Data->name(TimeZone => 'PacificTimeUSCanadaTijuana')
)),
SOAP::Data->name(Campaign => \SOAP::Data->value
(
SOAP::Data->name(BudgetType => 'DailyBudgetWithMaximumMonthlySpend'),
SOAP::Data->name(ConversionTrackingEnabled => 'true'),
SOAP::Data->name(DailyBudget => 2000),
SOAP::Data->name(DaylightSaving => 'true'),
SOAP::Data->name(Description => 'Athletic gear for winter'),
SOAP::Data->name(MonthlyBudget => 40000),
SOAP::Data->name(Name => 'Winter Athletic Gear'),
SOAP::Data->name(NegativeSiteUrls => \SOAP::Data->value
(
SOAP::Data->name('a:string' => 'http://www.contoso.com')
)
)->attr({'xmlns:i' => $namespaceArrays}),
SOAP::Data->name(TimeZone => 'EasternTimeUSCanada')
))
))->attr({'xmlns:i' => 'http://www.w3.org/2001/XMLSchema-instance'});
my @params =
(
@header,
SOAP::Data->name("AccountId" => $accountId),
@Campaigns
);
my $method = SOAP::Data->name
(
$action.'Request'
)-> attr({xmlns => $xmlns});
my $response =
$campaignManagementService->call
(
$method => @params
);
# Check for errors.
if ($response->fault)
{
print "$action failed.\n";
# Display the adCenter tracking ID.
my $trackingId = $response->valueof
(
'//AdApiFaultDetail/TrackingId'
);
print "TrackingId: $trackingId\n";
# Display any operation errors.
my @operationErrors = $response->valueof
(
'//AdApiFaultDetail/Errors/AdApiError'
);
foreach my $operationError (@operationErrors)
{
print "Operation error ($operationError->{Code}) encountered. ";
print "$operationError->{Message}\n";
}
# Display any batch errors.
my @batchErrors = $response->valueof
(
'//ApiFaultDetail/BatchErrors/BatchError'
);
foreach my $batchError (@batchErrors)
{
print "Campaign index $batchError->{Index}\n";
print "Batch error ($batchError->{Code}) encountered.\n";
print "$batchError->{Message}\n";
print "$batchError->{Details}\n";
}
}
else
{
print "$action succeeded.\n";
# Display the adCenter tracking identifier.
my $trackingId = $response->valueof
(
'//TrackingId'
);
print "TrackingId: $trackingId\n";
# Display the campaign identifiers.
my @ids = $response->valueof
(
'//CampaignIds/long'
);
print "The following campaign IDs were returned by $action:\n";
foreach my $longval (@ids)
{
print "Campaign ID: $longval\n";
}
}
sub CreateV6Header
{
# Method parameters.
my (
$username,
$password,
$devtoken,
$apptoken,
$customeraccountid,
$customerid) = @_;
# Initialize the application token.
my $AppToken = SOAP::Header->name
(
"ApplicationToken"=>$apptoken
)->attr({xmlns => $xmlns});
# Initialize the developer token.
my $DevToken =SOAP::Header->name
(
"DeveloperToken"=>$devtoken
)->attr({xmlns => $xmlns});
# Initialize the user name.
my $UserName =SOAP::Header->name
(
"UserName"=>$username
)->attr({xmlns => $xmlns});
# Initialize the password.
my $Password =SOAP::Header->name
(
"Password"=>$password
)->attr({xmlns => $xmlns});
# Initialize the customer account ID.
my $CustomerAccountId =SOAP::Header->name
(
"CustomerAccountId"=>$customeraccountid
)->attr({xmlns => $xmlns});
# Initialize the customer ID.
my $CustomerId =SOAP::Header->name
(
"CustomerId"=>$customerid
)->attr({xmlns => $xmlns});
# Assemble the header parameters.
my @headerParams =
(
$AppToken,
$CustomerAccountId,
$CustomerId,
$DevToken,
$Password,
$UserName,
);
return @headerParams;
}
};
warn $@ if $@;
Each successfully created campaign is assigned a campaign identifier by adCenter. The campaign identifiers are returned in the CampaignIds element of the AddCampaignsResponse object.
When you have received the campaign identifiers, you should locally 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 the campaign identifiers, you can retrieve them by calling the GetCampaignsByAccountId service operation. You can retrieve the elements that are associated with a campaign by calling the GetCampaignsByIds service operation. Calling GetCampaignsByAccountId or GetCampaignsByIds consumes some of your API quota. For more information, see Quotas in Microsoft adCenter.
Now that you have successfully created your campaigns, the next step is to create ad groups. For an example, see Create Ad Groups in Perl.

See Also