private static FeedItemOperation newSiteLinkFeedItemAddOperation(
            SiteLinksDataHolder siteLinksData, String text, String url)
        {
            // Create the FeedItemAttributeValues for our text values.
            FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue();

            linkTextAttributeValue.feedAttributeId = siteLinksData.LinkTextFeedAttributeId;
            linkTextAttributeValue.stringValue     = text;
            FeedItemAttributeValue linkUrlAttributeValue = new FeedItemAttributeValue();

            linkUrlAttributeValue.feedAttributeId = siteLinksData.LinkUrlFeedAttributeId;
            linkUrlAttributeValue.stringValue     = url;

            // Create the feed item and operation.
            FeedItem item = new FeedItem();

            item.feedId          = siteLinksData.SiteLinksFeedId;
            item.attributeValues =
                new FeedItemAttributeValue[] { linkTextAttributeValue, linkUrlAttributeValue };
            FeedItemOperation operation = new FeedItemOperation();

            operation.operand   = item;
            operation.@operator = Operator.ADD;
            return(operation);
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">Id of the campaign with which sitelinks are associated.
        /// </param>
        public void Run(AdWordsUser user, long campaignId)
        {
            SiteLinksDataHolder siteLinksData = new SiteLinksDataHolder();

            createSiteLinksFeed(user, siteLinksData);
            createSiteLinksFeedItems(user, siteLinksData);
            createSiteLinksFeedMapping(user, siteLinksData);
            createSiteLinksCampaignFeed(user, siteLinksData, campaignId);
        }
        private static void createSiteLinksFeed(
            AdWordsUser user, SiteLinksDataHolder siteLinksData)
        {
            // Get the FeedService.
            FeedService feedService = (FeedService)user.GetService(AdWordsService.v201409.FeedService);

            // Create attributes.
            FeedAttribute textAttribute = new FeedAttribute();

            textAttribute.type = FeedAttributeType.STRING;
            textAttribute.name = "Link Text";
            FeedAttribute urlAttribute = new FeedAttribute();

            urlAttribute.type = FeedAttributeType.URL;
            urlAttribute.name = "Link URL";

            // Create the feed.
            Feed siteLinksFeed = new Feed();

            siteLinksFeed.name       = "Feed For Site Links";
            siteLinksFeed.attributes = new FeedAttribute[] { textAttribute, urlAttribute };
            siteLinksFeed.origin     = FeedOrigin.USER;

            // Create operation.
            FeedOperation operation = new FeedOperation();

            operation.operand   = siteLinksFeed;
            operation.@operator = Operator.ADD;

            // Add the feed.
            FeedReturnValue result = feedService.mutate(new FeedOperation[] { operation });

            Feed savedFeed = result.value[0];

            siteLinksData.SiteLinksFeedId = savedFeed.id;
            FeedAttribute[] savedAttributes = savedFeed.attributes;
            siteLinksData.LinkTextFeedAttributeId = savedAttributes[0].id;
            siteLinksData.LinkUrlFeedAttributeId  = savedAttributes[1].id;
            Console.WriteLine("Feed with name {0} and ID {1} with linkTextAttributeId {2}"
                              + " and linkUrlAttributeId {3} was created.", savedFeed.name, savedFeed.id,
                              savedAttributes[0].id, savedAttributes[1].id);
        }
        private static void createSiteLinksFeedMapping(
            AdWordsUser user, SiteLinksDataHolder siteLinksData)
        {
            // Get the FeedItemService.
            FeedMappingService feedMappingService =
                (FeedMappingService)user.GetService(AdWordsService.v201409.FeedMappingService);

            // Map the FeedAttributeIds to the fieldId constants.
            AttributeFieldMapping linkTextFieldMapping = new AttributeFieldMapping();

            linkTextFieldMapping.feedAttributeId = siteLinksData.LinkTextFeedAttributeId;
            linkTextFieldMapping.fieldId         = PLACEHOLDER_FIELD_SITELINK_LINK_TEXT;
            AttributeFieldMapping linkUrlFieldMapping = new AttributeFieldMapping();

            linkUrlFieldMapping.feedAttributeId = siteLinksData.LinkUrlFeedAttributeId;
            linkUrlFieldMapping.fieldId         = PLACEHOLDER_FIELD_SITELINK_URL;

            // Create the FieldMapping and operation.
            FeedMapping feedMapping = new FeedMapping();

            feedMapping.placeholderType        = PLACEHOLDER_SITELINKS;
            feedMapping.feedId                 = siteLinksData.SiteLinksFeedId;
            feedMapping.attributeFieldMappings =
                new AttributeFieldMapping[] { linkTextFieldMapping, linkUrlFieldMapping };
            FeedMappingOperation operation = new FeedMappingOperation();

            operation.operand   = feedMapping;
            operation.@operator = Operator.ADD;

            // Save the field mapping.
            FeedMappingReturnValue result =
                feedMappingService.mutate(new FeedMappingOperation[] { operation });

            foreach (FeedMapping savedFeedMapping in result.value)
            {
                Console.WriteLine(
                    "Feed mapping with ID {0} and placeholderType {1} was saved for feed with ID {2}.",
                    savedFeedMapping.feedMappingId, savedFeedMapping.placeholderType,
                    savedFeedMapping.feedId);
            }
        }
        private static void createSiteLinksFeedItems(
            AdWordsUser user, SiteLinksDataHolder siteLinksData)
        {
            // Get the FeedItemService.
            FeedItemService feedItemService =
                (FeedItemService)user.GetService(AdWordsService.v201409.FeedItemService);

            // Create operations to add FeedItems.
            FeedItemOperation home =
                newSiteLinkFeedItemAddOperation(siteLinksData,
                                                "Home", "http://www.example.com");
            FeedItemOperation stores =
                newSiteLinkFeedItemAddOperation(siteLinksData,
                                                "Stores", "http://www.example.com/stores");
            FeedItemOperation onSale =
                newSiteLinkFeedItemAddOperation(siteLinksData,
                                                "On Sale", "http://www.example.com/sale");
            FeedItemOperation support =
                newSiteLinkFeedItemAddOperation(siteLinksData,
                                                "Support", "http://www.example.com/support");
            FeedItemOperation products =
                newSiteLinkFeedItemAddOperation(siteLinksData,
                                                "Products", "http://www.example.com/prods");
            FeedItemOperation aboutUs =
                newSiteLinkFeedItemAddOperation(siteLinksData,
                                                "About Us", "http://www.example.com/about");

            FeedItemOperation[] operations =
                new FeedItemOperation[] { home, stores, onSale, support, products, aboutUs };

            FeedItemReturnValue result = feedItemService.mutate(operations);

            foreach (FeedItem item in result.value)
            {
                Console.WriteLine("FeedItem with feedItemId {0} was added.", item.feedItemId);
                siteLinksData.SiteLinkFeedItemIds.Add(item.feedItemId);
            }
        }
        private static void createSiteLinksCampaignFeed(AdWordsUser user,
                                                        SiteLinksDataHolder siteLinksData, long campaignId)
        {
            // Get the CampaignFeedService.
            CampaignFeedService campaignFeedService =
                (CampaignFeedService)user.GetService(AdWordsService.v201409.CampaignFeedService);

            // Map the feed item ids to the campaign using an IN operation.
            RequestContextOperand feedItemRequestContextOperand = new RequestContextOperand();

            feedItemRequestContextOperand.contextType = RequestContextOperandContextType.FEED_ITEM_ID;

            List <FunctionArgumentOperand> feedItemOperands = new List <FunctionArgumentOperand>();

            foreach (long feedItemId in siteLinksData.SiteLinkFeedItemIds)
            {
                ConstantOperand feedItemOperand = new ConstantOperand();
                feedItemOperand.longValue = feedItemId;
                feedItemOperand.type      = ConstantOperandConstantType.LONG;
                feedItemOperands.Add(feedItemOperand);
            }

            Function feedItemfunction = new Function();

            feedItemfunction.lhsOperand = new FunctionArgumentOperand[] { feedItemRequestContextOperand };
            feedItemfunction.@operator  = FunctionOperator.IN;
            feedItemfunction.rhsOperand = feedItemOperands.ToArray();

            // Optional: to target to a platform, define a function and 'AND' it with
            // the feed item ID link:
            RequestContextOperand platformRequestContextOperand = new RequestContextOperand();

            platformRequestContextOperand.contextType = RequestContextOperandContextType.DEVICE_PLATFORM;

            ConstantOperand platformOperand = new ConstantOperand();

            platformOperand.stringValue = "Mobile";
            platformOperand.type        = ConstantOperandConstantType.STRING;

            Function platformFunction = new Function();

            platformFunction.lhsOperand = new FunctionArgumentOperand[] { platformRequestContextOperand };
            platformFunction.@operator  = FunctionOperator.EQUALS;
            platformFunction.rhsOperand = new FunctionArgumentOperand[] { platformOperand };

            // Combine the two functions using an AND operation.
            FunctionOperand feedItemFunctionOperand = new FunctionOperand();

            feedItemFunctionOperand.value = feedItemfunction;

            FunctionOperand platformFunctionOperand = new FunctionOperand();

            platformFunctionOperand.value = platformFunction;

            Function combinedFunction = new Function();

            combinedFunction.@operator  = FunctionOperator.AND;
            combinedFunction.lhsOperand = new FunctionArgumentOperand[] {
                feedItemFunctionOperand, platformFunctionOperand
            };

            CampaignFeed campaignFeed = new CampaignFeed();

            campaignFeed.feedId           = siteLinksData.SiteLinksFeedId;
            campaignFeed.campaignId       = campaignId;
            campaignFeed.matchingFunction = combinedFunction;
            // Specifying placeholder types on the CampaignFeed allows the same feed
            // to be used for different placeholders in different Campaigns.
            campaignFeed.placeholderTypes = new int[] { PLACEHOLDER_SITELINKS };

            CampaignFeedOperation operation = new CampaignFeedOperation();

            operation.operand   = campaignFeed;
            operation.@operator = Operator.ADD;
            CampaignFeedReturnValue result =
                campaignFeedService.mutate(new CampaignFeedOperation[] { operation });

            foreach (CampaignFeed savedCampaignFeed in result.value)
            {
                Console.WriteLine("Campaign with ID {0} was associated with feed with ID {1}",
                                  savedCampaignFeed.campaignId, savedCampaignFeed.feedId);
            }
        }