Export (0) Print
Expand All

Add Campaigns in C#

The following example shows how to create campaigns within an account using the Campaign Management AddCampaigns service operation.

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

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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

// BingAds.CampaignManagement is the application-defined namespace that 
// this example used when it added the Campaign Management service reference. 

using CreateCampaigns.BingAds.CampaignManagement;


namespace CreateCampaigns
{
    class Program
    {
        private static CampaignManagementServiceClient service = null;

        // Specify your credentials.

        private static string m_password = "<PasswordGoesHere> ";
        private static string m_username = "<UsernameGoesHere>";
        private static string m_token = "<DevTokenGoesHere>";

        // Specify the account that will own the campaigns.

        private static long m_accountId = <AccountIdGoesHere>;


        static void Main()
        {
            long[] campaignIds = null;

            try
            {
                service = new CampaignManagementServiceClient();

                campaignIds = CreateCampaigns(m_accountId);

                if (campaignIds != null)
                {
                    // Do something with the IDs.
                }

                service.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);

                if (null != e.InnerException)
                {
                    Console.WriteLine("\n" + e.InnerException.Message);
                }

                if (service != null)
                {
                    service.Abort();
                }
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);

                if (service != null)
                {
                    service.Abort();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                if (service != null)
                {
                    service.Abort();
                }
            }
        }


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

        static long[] CreateCampaigns(long accountId)
        {
            AddCampaignsRequest request = new AddCampaignsRequest();
            AddCampaignsResponse response = null;
            int i = 0;  // Index into campaigns.

            // Specify the campaigns.

            Campaign[] campaigns = new[] {
                new Campaign {
                    Name = "Winter Clothing",
                    Description = "Winter clothing line.",
                    BudgetType = BudgetLimitType.MonthlyBudgetSpendUntilDepleted,
                    MonthlyBudget = 1000.00,
                    TimeZone = "PacificTimeUSCanadaTijuana",
                    DaylightSaving = true,
                    ConversionTrackingEnabled = false
                },
                new Campaign {
                    Name = "Spring Clothing",
                    Description = "Spring clothing line.",
                    BudgetType = BudgetLimitType.DailyBudgetStandard,
                    DailyBudget = 20.00,
                    TimeZone = "PacificTimeUSCanadaTijuana",
                    DaylightSaving = true,
                    ConversionTrackingEnabled = false
                }
            };

            // Set the request header information.

            request.CustomerAccountId = accountId.ToString();
            request.DeveloperToken = m_token;
            request.UserName = m_username;
            request.Password = m_password;

            // Set the request information.

            request.AccountId = accountId;
            request.Campaigns = campaigns;

            try
            {
                response = service.AddCampaigns(request);

                foreach (long id in response.CampaignIds)
                {
                    Console.WriteLine("Added campaign \"{0}\" with ID {1}.", 
                        campaigns[i++].Name, id);
                }
            }
            catch (FaultException<AdApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("AddCampaigns failed with the following faults:\n");

                foreach (AdApiError error in fault.Detail.Errors)
                {
                    Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                        error.ErrorCode, error.Code, error.Message, error.Detail);
                }

            }
            catch (FaultException<ApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("AddCampaigns failed with the following faults " +
                    "(no campaigns were added):\n");

                foreach (OperationError error in fault.Detail.OperationErrors)
                {
                    Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                        error.ErrorCode, error.Code, error.Message, error.Details);
                }

                foreach (BatchError error in fault.Detail.BatchErrors)
                {
                    switch (error.Code)
                    {
                        case 1014: // CampaignServiceTimeZoneValueInvalid
                            Console.WriteLine("The time zone that you specified for " +
                                "campaign \"{0}\" is not valid.\n",
                                campaigns[error.Index].Name);
                            break;

                        case 1105: // CampaignServiceInvalidMonthlyBudget
                            Console.WriteLine("The monthly budget that you specified for " +
                                "campaign \"{0}\" is not valid.\n",
                                campaigns[error.Index].Name);
                            break;

                        case 1106: // CampaignServiceInvalidDailyBudget
                            Console.WriteLine("The daily budget that you specified for " +
                                "campaign \"{0}\" is not valid.\n",
                                campaigns[error.Index].Name);
                            break;

                        case 1108: // CampaignServiceInvalidConversionTrackingEnabled
                            Console.WriteLine("You must specify whether you want to enable " +
                                "conversion tracking for campaign \"{0}\"\n",
                                campaigns[error.Index].Name);
                            break;

                        case 1109: // CampaignServiceTimeZoneNotEnabled
                            Console.WriteLine("You must specify the time zone where campaign " +
                                "\"{0}\" operates.\n",
                                campaigns[error.Index].Name);
                            break;

                        case 1110: // CampaignServiceDaylightSavingNotEnabled
                            Console.WriteLine("You must specify whether to automatically " +
                                "adjust for daylight-saving time for campaign \"{0}\".\n",
                                campaigns[error.Index].Name);
                            break;

                        case 1115: // CampaignServiceCannotCreateDuplicateCampaign
                            Console.WriteLine("A campaign named \"{0}\" already exists. " +
                                "Please specify a new campaign name.\n",
                                campaigns[error.Index].Name);
                            break;

                        default:
                            Console.WriteLine("Unable to add campaign, {0}", campaigns[error.Index].Name);
                            Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                                error.ErrorCode, error.Code, error.Message, error.Details);
                            break;
                    }
                }

            }

            return (null == response) ? null : response.CampaignIds;
        }
    }
}
        


Community Additions

Show:
© 2014 Microsoft