Google.Api.Ads.AdWords.Examples.CSharp.v201306.GetAllVideosAndImages.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 MediaService.
              MediaService mediaService = (MediaService) user.GetService(
              AdWordsService.v201306.MediaService);

              // Create a selector.
              Selector selector = new Selector();
              selector.fields = new string[] {"MediaId", "Width", "Height", "MimeType"};

              // Set the filter.
              Predicate predicate = new Predicate();
              predicate.@operator = PredicateOperator.IN;
              predicate.field = "Type";
              predicate.values = new string[] {MediaMediaType.VIDEO.ToString(),
              MediaMediaType.IMAGE.ToString()};

              selector.predicates = new Predicate[] {predicate};

              // Set selector paging.
              selector.paging = new Paging();

              int offset = 0;
              int pageSize = 500;

              MediaPage page = new MediaPage();

              try {
            do {
              selector.paging.startIndex = offset;
              selector.paging.numberResults = pageSize;

              page = mediaService.get(selector);

              if (page != null && page.entries != null) {
            int i = offset;

            foreach (Media media in page.entries) {
              if (media is Video) {
                Video video = (Video) media;
                Console.WriteLine("{0}) Video with id \"{1}\" and name \"{2}\" was found.",
                    i, video.mediaId, video.name);
              } else if (media is Image) {
                Image image = (Image) media;
                Dictionary<MediaSize, Dimensions> dimensions =
                    CreateMediaDimensionMap(image.dimensions);
                Console.WriteLine("{0}) Image with id '{1}', dimensions '{2}x{3}', and MIME type " +
                    "'{4}' was found.", i, image.mediaId, dimensions[MediaSize.FULL].width,
                    dimensions[MediaSize.FULL].height, image.mimeType);
              }
              i++;
            }
              }
              offset += pageSize;
            } while (offset < page.totalNumEntries);
            Console.WriteLine("Number of images and videos found: {0}", page.totalNumEntries);
              } catch (Exception ex) {
            throw new System.ApplicationException("Failed to get images and videos.", 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)
 {
     GetAllVideosAndImages codeExample = new GetAllVideosAndImages();
       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));
       }
 }