/// <summary> /// Updates attribute value of the feed item. In order to update a FeedItemAttributeValue /// you must update the FeedItem. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The customer ID for which the call is made.</param> /// <param name="feedId">ID of the feed containing the feed item to be updated.</param> /// <param name="feedItemId">ID of the feed item to be updated.</param> /// <param name="flightPlaceholderField">the placeholder type for the attribute to be /// updated.</param> /// <param name="attributeValue">String value with which to update the /// FeedAttributeValue.</param> private void UpdateFeedItem(GoogleAdsClient client, long customerId, long feedId, long feedItemId, string flightPlaceholderField, string attributeValue) { // Get the FeedItemServiceClient. FeedItemServiceClient feedItemService = client.GetService(Services.V2.FeedItemService); // Gets the feed resource name. string feedResourceName = ResourceNames.Feed(customerId, feedId); // Gets a map of the placeholder values and feed attributes. Dictionary <FlightPlaceholderField, FeedAttribute> feedAttributes = GetFeed(client, customerId, feedResourceName); // Gets the ID of the attribute to update. This is needed to specify which // FeedItemAttributeValue will be updated in the given FeedItem. FlightPlaceholderField placeholderField = (FlightPlaceholderField)Enum.Parse( typeof(FlightPlaceholderField), flightPlaceholderField); long attributeId = feedAttributes[placeholderField].Id.Value; // Gets the feed item resource name. string feedItemResourceName = ResourceNames.FeedItem(customerId, feedId, feedItemId); // Retrieves the feed item and its associated attributes based on its resource name. FeedItem feedItem = GetFeedItem(client, customerId, feedItemResourceName); // Creates the updated FeedItemAttributeValue. FeedItemAttributeValue feedItemAttributeValue = new FeedItemAttributeValue() { FeedAttributeId = attributeId, StringValue = attributeValue }; // Creates a new FeedItem from the existing FeedItem. Any FeedItemAttributeValues that are // not included in the updated FeedItem will be removed from the FeedItem, which is why you // must create the FeedItem from the existing FeedItem and set the field(s) that will be // updated. feedItem.AttributeValues[GetAttributeIndex(feedItem, feedItemAttributeValue)] = feedItemAttributeValue; // Creates the operation. FeedItemOperation operation = new FeedItemOperation() { Update = feedItem, UpdateMask = FieldMasks.AllSetFieldsOf(feedItem) }; // Updates the feed item. MutateFeedItemsResponse response = feedItemService.MutateFeedItems(customerId.ToString(), new[] { operation }); foreach (MutateFeedItemResult result in response.Results) { Console.WriteLine($"Updated feed item with resource name '{result.ResourceName}'."); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="client">The Google Ads API client.</param> /// <param name="customerId">The Google Ads customer ID for which the call is made.</param> /// <param name="feedId">The Feed ID to which the feed item belongs.</param> /// <param name="feedItemId">The ID of the feed item to be updated.</param> /// <param name="flightPlaceholderFieldName"> /// The flight placeholder field name for the attribute to be removed. /// </param> public void Run(GoogleAdsClient client, long customerId, long feedId, long feedItemId, string flightPlaceholderFieldName) { // Creates the Feed Item service client. FeedItemServiceClient feedItemServiceClient = client.GetService( Services.V10.FeedItemService); try { // Gets a map of the placeholder values to feed attributes. Dictionary <FlightPlaceholderField, FeedAttribute> placeholdersToFeedAttributesMap = GetFeed(client, customerId, feedId); // Removes the attribute from the feed item. FlightPlaceholderField flightPlaceholderField = (FlightPlaceholderField)Enum.Parse(typeof(FlightPlaceholderField), flightPlaceholderFieldName, true); FeedItem feedItem = RemoveAttributeValueFromFeedItem(client, customerId, feedId, feedItemId, placeholdersToFeedAttributesMap, flightPlaceholderField); // [START remove_flights_feed_item_attribute_value_1] Creates the operation. FeedItemOperation operation = new FeedItemOperation { Update = feedItem, UpdateMask = FieldMasks.AllSetFieldsOf(feedItem) }; // Updates the feed item and prints the results. MutateFeedItemsResponse response = feedItemServiceClient.MutateFeedItems (customerId.ToString(), new[] { operation }); foreach (MutateFeedItemResult result in response.Results) { Console.WriteLine("Updated feed item with resource name " + $"'{result.ResourceName}'."); } // [END remove_flights_feed_item_attribute_value_1] } catch (GoogleAdsException e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine($"Failure: {e.Failure}"); Console.WriteLine($"Request ID: {e.RequestId}"); throw; } catch (Exception e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); throw; } }
/// <summary> /// Removes an attribute value from the specified feed item. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The Google Ads customer ID that has the flights feed.</param> /// <param name="feedId">The feed ID that contains the target feed item.</param> /// <param name="feedItemId">The feed item ID that will be updated.</param> /// <param name="placeholdersToFeedAttributesMap">A mapping of FlightPlaceholderFields to /// FeedAttributes.</param> /// <param name="flightPlaceholderFieldName">The attributed field name to remove.</param> /// <returns>The modified feed item.</returns> /// <exception cref="ArgumentException">If the specified attribute was not found in the /// feed item.</exception> private FeedItem RemoveAttributeValueFromFeedItem(GoogleAdsClient client, long customerId, long feedId, long feedItemId, Dictionary <FlightPlaceholderField, FeedAttribute> placeholdersToFeedAttributesMap, FlightPlaceholderField flightPlaceholderFieldName) { // Gets the ID of the FeedAttribute for the placeholder field. long attributeId = placeholdersToFeedAttributesMap[flightPlaceholderFieldName].Id.Value; // Retrieves the feed item and its associated attributes based on its resource name. FeedItem feedItem = GetFeedItem(client, customerId, feedId, feedItemId); //Creates the FeedItemAttributeValue that will be updated. FeedItemAttributeValue feedItemAttributeValue = new FeedItemAttributeValue { FeedAttributeId = attributeId }; // Gets the index of the attribute value that will be removed. // int attributeIndex = GetAttributeIndex(feedItem, feedItemAttributeValue); int attributeIndex = feedItem.AttributeValues .Select((item, index) => new { item, index }) .Where(itemIndexPair => itemIndexPair.item.FeedAttributeId.Value == feedItemAttributeValue.FeedAttributeId.Value) .Select(itemIndexPair => itemIndexPair.index + 1) .FirstOrDefault() - 1; if (attributeIndex == -1) { throw new ArgumentException("No matching feed attribute found for " + $"value '{feedItemAttributeValue}'."); } // Returns the feed item with the removed FeedItemAttributeValue. Any // FeedItemAttributeValues that are not included in the updated FeedItem will be removed // from the FeedItem, which is why you must create the FeedItem from the existing // FeedItem and set the field(s) that will be removed. feedItem.AttributeValues.RemoveAt(attributeIndex); return(feedItem); }
/// <summary> /// Updates attribute value of the feed item. In order to update a FeedItemAttributeValue /// you must update the FeedItem. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The customer ID for which the call is made.</param> /// <param name="feedId">ID of the feed containing the feed item to be updated.</param> /// <param name="feedItemId">ID of the feed item to be updated.</param> /// <param name="flightPlaceholderFieldName">the placeholder type for the attribute to be /// updated.</param> /// <param name="attributeValue">String value with which to update the /// FeedAttributeValue.</param> private void UpdateFeedItem(GoogleAdsClient client, long customerId, long feedId, long feedItemId, string flightPlaceholderFieldName, string attributeValue) { // Get the FeedItemServiceClient. FeedItemServiceClient feedItemService = client.GetService(Services.V5.FeedItemService); // Gets the feed resource name. string feedResourceName = ResourceNames.Feed(customerId, feedId); // Gets a map of the placeholder values and feed attributes. Dictionary <FlightPlaceholderField, FeedAttribute> feedAttributes = GetFeed(client, customerId, feedResourceName); // Gets the ID of the attribute to update. This is needed to specify which // FeedItemAttributeValue will be updated in the given FeedItem. FlightPlaceholderField placeholderField = (FlightPlaceholderField)Enum.Parse( typeof(FlightPlaceholderField), flightPlaceholderFieldName); long attributeId = feedAttributes[placeholderField].Id.Value; // Gets the feed item resource name. string feedItemResourceName = ResourceNames.FeedItem(customerId, feedId, feedItemId); // Retrieves the feed item and its associated attributes based on its resource name. FeedItem feedItem = GetFeedItem(client, customerId, feedItemResourceName); // Creates the updated FeedItemAttributeValue. FeedItemAttributeValue feedItemAttributeValue = new FeedItemAttributeValue() { FeedAttributeId = attributeId, StringValue = attributeValue }; // Creates a new FeedItem from the existing FeedItem. Any FeedItemAttributeValues that // are not included in the updated FeedItem will be removed from the FeedItem, which is // why you must create the FeedItem from the existing FeedItem and set the field(s) // that will be updated. int attributeIndex = feedItem.AttributeValues .Select((item, index) => new { item, index }) .Where(itemIndexPair => itemIndexPair.item.FeedAttributeId.Value == feedItemAttributeValue.FeedAttributeId.Value) .Select(itemIndexPair => itemIndexPair.index + 1) .FirstOrDefault() - 1; if (attributeIndex == -1) { throw new ArgumentException("No matching feed attribute found for " + $"value '{feedItemAttributeValue}'."); } feedItem.AttributeValues[attributeIndex] = feedItemAttributeValue; // Creates the operation. FeedItemOperation operation = new FeedItemOperation() { Update = feedItem, UpdateMask = FieldMasks.AllSetFieldsOf(feedItem) }; // Updates the feed item. MutateFeedItemsResponse response = feedItemService.MutateFeedItems(customerId.ToString(), new[] { operation }); foreach (MutateFeedItemResult result in response.Results) { Console.WriteLine($"Updated feed item with resource name '{result.ResourceName}'."); } }