/// <summary> /// Adds a set of keywords to a shared set. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="sharedSetId">The shared set id.</param> /// <param name="keywordTexts">The keywords to be added to the shared set.</param> /// <returns>The newly added set of shared criteria.</returns> public SharedCriterion[] AddKeywordsToSharedSet(AdWordsUser user, long sharedSetId, string[] keywordTexts) { // Get the SharedCriterionService. SharedCriterionService sharedSetService = (SharedCriterionService) user.GetService(AdWordsService.v201406.SharedCriterionService); List <SharedCriterionOperation> operations = new List <SharedCriterionOperation>(); foreach (string keywordText in keywordTexts) { Keyword keyword = new Keyword(); keyword.text = keywordText; keyword.matchType = KeywordMatchType.BROAD; SharedCriterion sharedCriterion = new SharedCriterion(); sharedCriterion.criterion = keyword; sharedCriterion.negative = true; sharedCriterion.sharedSetId = sharedSetId; SharedCriterionOperation operation = new SharedCriterionOperation(); operation.@operator = Operator.ADD; operation.operand = sharedCriterion; operations.Add(operation); } SharedCriterionReturnValue retval = sharedSetService.mutate(operations.ToArray()); return(retval.value); }
/// <summary> /// Adds a set of keywords to a shared set. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="sharedSetId">The shared set id.</param> /// <param name="keywordTexts">The keywords to be added to the shared set.</param> /// <returns>The newly added set of shared criteria.</returns> public SharedCriterion[] AddKeywordsToSharedSet(AdWordsUser user, long sharedSetId, string[] keywordTexts) { using (SharedCriterionService sharedCriterionService = (SharedCriterionService) user.GetService(AdWordsService.v201802.SharedCriterionService)) { List <SharedCriterionOperation> operations = new List <SharedCriterionOperation>(); foreach (string keywordText in keywordTexts) { Keyword keyword = new Keyword { text = keywordText, matchType = KeywordMatchType.BROAD }; SharedCriterion sharedCriterion = new SharedCriterion { criterion = keyword, negative = true, sharedSetId = sharedSetId }; SharedCriterionOperation operation = new SharedCriterionOperation { @operator = Operator.ADD, operand = sharedCriterion }; operations.Add(operation); } SharedCriterionReturnValue retval = sharedCriterionService.mutate(operations.ToArray()); return(retval.value); } }
/// <summary> /// Removes a list of shared criteria. /// </summary> /// <param name="user">The user that owns the shared criteria.</param> /// <param name="sharedCriteria">The list shared criteria to be removed.</param> private void RemoveSharedCriteria(AdWordsUser user, List <SharedCriterion> sharedCriteria) { if (sharedCriteria.Count == 0) { Console.WriteLine("No shared criteria to remove."); return; } using (SharedCriterionService sharedCriterionService = (SharedCriterionService)user.GetService(AdWordsService.v201802 .SharedCriterionService)) { List <SharedCriterionOperation> operations = new List <SharedCriterionOperation>(); foreach (SharedCriterion sharedCriterion in sharedCriteria) { operations.Add(new SharedCriterionOperation() { @operator = Operator.REMOVE, operand = new SharedCriterion() { sharedSetId = sharedCriterion.sharedSetId, criterion = new Criterion() { id = sharedCriterion.criterion.id } } }); } try { SharedCriterionReturnValue sharedCriterionReturnValue = sharedCriterionService.mutate(operations.ToArray()); foreach (SharedCriterion removedCriterion in sharedCriterionReturnValue.value) { Console.WriteLine( "Shared criterion ID {0} was successfully removed from shared " + "set ID {1}.", removedCriterion.criterion.id, removedCriterion.sharedSetId); } } catch (Exception e) { throw new Exception("Failed to remove shared criteria.", e); } } }