Export (0) Print
Expand All

Add Ads in C#

The following example shows how to create ads within an ad group using the Campaign Management AddAds 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 CreateAds.BingAds.CampaignManagement;


namespace CreateAds
{
    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 owns the ad group that the ads
        // will be added to.

        private static long m_accountId = <AccountIDGoesHere>;

        // Specify the ad group to add the ads to.

        private static long m_adGroupId = <AdGroupGoesHere>;


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

            try
            {
                service = new CampaignManagementServiceClient();

                adIds = CreateAds(m_accountId, m_adGroupId);

                if (adIds != 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 ads to the specified ad group.

        static long[] CreateAds(long accountId, long adGroupId)
        {
            AddAdsRequest request = new AddAdsRequest();
            AddAdsResponse response = null;

            // Specify the ads.

            Ad[] ads = new[] {
                new TextAd {
                    DestinationUrl = "http://www.alpineskihouse.com/winterglovesale",
                    DisplayUrl = "AlipineSkiHouse.com",
                    Text = "Huge Savings on heated gloves.",
                    Title = "Winter Glove Sale",
                },
                new TextAd {
                    DestinationUrl = "http://www.alpineskihouse.com/winterglovesale",
                    DisplayUrl = "AlipineSkiHouse.com",
                    Text = "All {keyword:ladies heated gloves} {param1:at least 10% off}.",
                    Title = "{keyword:Winter Glove} Sale",
                }
            };

            // 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.AdGroupId = adGroupId;
            request.Ads = ads;

            try
            {
                response = service.AddAds(request);

                foreach (long id in response.AdIds)
                {
                    Console.WriteLine("Added ad with ID {0}.", id);
                }
            }
            catch (FaultException<AdApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("AddAds 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<EditorialApiFaultDetail> fault)
            {
                // Log this fault.

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

                foreach (OperationError error in fault.Detail.OperationErrors)
                {
                    if (1030 == error.Code) // CampaignServiceAccountIdHasToBeSpecified
                    {
                        Console.WriteLine("You must specify the account ID in the " +
                            "request header when calling this operation.");
                    }
                    else
                    {
                        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 1313: // CampaignServiceDuplicateAd
                            Console.WriteLine("Ad #{0} duplicates another ad in the " +
                                "batch of ads that you are adding.", 
                                error.Index);
                            break;

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

                foreach (EditorialError error in fault.Detail.EditorialErrors)
                {
                    switch (error.Code)
                    {
                        case 1324: // CampaignServiceTooMuchAdTextInTitle
                            Console.WriteLine("The length of ad #{0}'s title is " +
                                "too long.",
                                error.Index);
                            break;

                        case 1325: // CampaignServiceTooMuchAdTextInText
                            Console.WriteLine("The length of ad #{0}'s text is " + 
                                "too long.",
                                error.Index);
                            break;

                        case 1326: // CampaignServiceTooMuchAdTextInDisplayUrl
                            Console.WriteLine("The length of ad #{0}'s display URL " +
                                "is too long.",
                                error.Index);
                            break;

                        case 1332: // CampaignServiceInvalidAdDisplayUrlFormat
                            Console.WriteLine("The format of ad #{0}'s display URL " +
                                "is not valid.", 
                                error.Index);
                            break;

                        default:
                            Console.WriteLine("Unable to add ad #{0}.", error.Index);
                            Console.WriteLine("Error code: {0} ({1})\nMessage: {2}",
                                error.ErrorCode, error.Code, error.Message);
                            Console.WriteLine("Appealable: {0}\nDisapproved text: {1}\nCountry: {2}\n",
                                error.Appealable, error.DisapprovedText, error.PublisherCountry);
                            break;
                    }
                }

            }

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

Community Additions

Show:
© 2014 Microsoft