Google.Api.Ads.AdWords.Examples.CSharp.v201306.GetAccountHierarchy.Run C# (CSharp) Method

Run() public method

Runs the code example.
public Run ( Google.Api.Ads.AdWords.Lib.AdWordsUser user ) : void
user Google.Api.Ads.AdWords.Lib.AdWordsUser The AdWords user.
return void
        public void Run(AdWordsUser user)
        {
            // Get the ManagedCustomerService.
              ManagedCustomerService managedCustomerService = (ManagedCustomerService) user.GetService(
              AdWordsService.v201306.ManagedCustomerService);
              managedCustomerService.RequestHeader.clientCustomerId = null;

              // Create selector.
              Selector selector = new Selector();
              selector.fields = new String[] {"Login", "CustomerId", "Name"};

              try {
            // Get results.
            ManagedCustomerPage page = managedCustomerService.get(selector);

            // Display serviced account graph.
            if (page.entries != null) {
              // Create map from customerId to customer node.
              Dictionary<long, ManagedCustomerTreeNode> customerIdToCustomerNode =
              new Dictionary<long, ManagedCustomerTreeNode>();

              // Create account tree nodes for each customer.
              foreach (ManagedCustomer customer in page.entries) {
            ManagedCustomerTreeNode node = new ManagedCustomerTreeNode();
            node.Account = customer;
            customerIdToCustomerNode.Add(customer.customerId, node);
              }

              // For each link, connect nodes in tree.
              if (page.links != null) {
            foreach (ManagedCustomerLink link in page.links) {
              ManagedCustomerTreeNode managerNode =
                  customerIdToCustomerNode[link.managerCustomerId];
              ManagedCustomerTreeNode childNode = customerIdToCustomerNode[link.clientCustomerId];
              childNode.ParentNode = managerNode;
              if (managerNode != null) {
                managerNode.ChildAccounts.Add(childNode);
              }
            }
              }

              // Find the root account node in the tree.
              ManagedCustomerTreeNode rootNode = null;
              foreach (ManagedCustomer account in page.entries) {
            if (customerIdToCustomerNode[account.customerId].ParentNode == null) {
              rootNode = customerIdToCustomerNode[account.customerId];
              break;
            }
              }

              // Display account tree.
              Console.WriteLine("Login, CustomerId, Name");
              Console.WriteLine(rootNode.ToTreeString(0, new StringBuilder()));
            } else {
              Console.WriteLine("No serviced accounts were found.");
            }
              } catch (Exception ex) {
            throw new System.ApplicationException("Failed to create ad groups.", ex);
              }
        }

Usage Example

 /// <summary>
 /// Main method, to run this code example as a standalone application.
 /// </summary>
 /// <param name="args">The command line arguments.</param>
 public static void Main(string[] args)
 {
     GetAccountHierarchy codeExample = new GetAccountHierarchy();
       Console.WriteLine(codeExample.Description);
       try {
     codeExample.Run(new AdWordsUser());
       } catch (Exception ex) {
     Console.WriteLine("An exception occurred while running this code example. {0}",
     ExampleUtilities.FormatException(ex));
       }
 }
GetAccountHierarchy