/// <summary> /// Run the code example. /// </summary> /// <param name="user">The DFP user object running the code example.</param> public override void Run(DfpUser user) { // Get the CustomFieldService. CustomFieldService customFieldService = (CustomFieldService)user.GetService( DfpService.v201403.CustomFieldService); // Create a statement to get all custom fields. StatementBuilder statementBuilder = new StatementBuilder() .OrderBy("id ASC") .Limit(StatementBuilder.SUGGESTED_PAGE_LIMIT); // Sets default for page. CustomFieldPage page = new CustomFieldPage(); try { do { // Get custom fields by statement. page = customFieldService.getCustomFieldsByStatement(statementBuilder.ToStatement()); if (page.results != null) { int i = page.startIndex; foreach (CustomField customField in page.results) { if (customField is DropDownCustomField) { List <String> dropDownCustomFieldStrings = new List <String>(); DropDownCustomField dropDownCustomField = (DropDownCustomField)customField; if (dropDownCustomField.options != null) { foreach (CustomFieldOption customFieldOption in dropDownCustomField.options) { dropDownCustomFieldStrings.Add(customFieldOption.displayName); } } Console.WriteLine("{0}) Drop-down custom field with ID \"{1}\", name \"{2}\", " + "and options {{{3}}} was found.", i, customField.id, customField.name, string.Join(", ", dropDownCustomFieldStrings.ToArray())); } else { Console.WriteLine("{0}) Custom field with ID \"{1}\" and name \"{2}\" was found.", i, customField.id, customField.name); } i++; } } statementBuilder.IncreaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT); } while (statementBuilder.GetOffset() < page.totalResultSetSize); Console.WriteLine("Number of results found: " + page.totalResultSetSize); } catch (Exception ex) { Console.WriteLine("Failed to get all custom fields. Exception says \"{0}\"", ex.Message); } }
/// <summary> /// Run the code example. /// </summary> /// <param name="user">The DFP user object running the code example.</param> public override void Run(DfpUser user) { // Get the CustomFieldService. CustomFieldService customFieldService = (CustomFieldService)user.GetService( DfpService.v201311.CustomFieldService); // Sets defaults for page and filter. CustomFieldPage page = new CustomFieldPage(); Statement filterStatement = new Statement(); int offset = 0; try { do { // Create a statement to get all custom fields. filterStatement.query = "LIMIT 500 OFFSET " + offset; // Get custom fields by statement. page = customFieldService.getCustomFieldsByStatement(filterStatement); if (page.results != null) { int i = page.startIndex; foreach (CustomField customField in page.results) { if (customField is DropDownCustomField) { List <String> dropDownCustomFieldStrings = new List <String>(); DropDownCustomField dropDownCustomField = (DropDownCustomField)customField; if (dropDownCustomField.options != null) { foreach (CustomFieldOption customFieldOption in dropDownCustomField.options) { dropDownCustomFieldStrings.Add(customFieldOption.displayName); } } Console.WriteLine("{0}) Drop-down custom field with ID \"{1}\", name \"{2}\", " + "and options {{{3}}} was found.", i, customField.id, customField.name, string.Join(", ", dropDownCustomFieldStrings.ToArray())); } else { Console.WriteLine("{0}) Custom field with ID \"{1}\" and name \"{2}\" was found.", i, customField.id, customField.name); } i++; } } offset += 500; } while (page.results != null && page.results.Length == 500); Console.WriteLine("Number of results found: " + page.totalResultSetSize); } catch (Exception ex) { Console.WriteLine("Failed to get all custom fields. Exception says \"{0}\"", ex.Message); } }
/// <summary> /// Run the code example. /// </summary> /// <param name="user">The DFP user object running the code example.</param> public override void Run(DfpUser user) { // Get the CustomFieldService. CustomFieldService customFieldService = (CustomFieldService)user.GetService( DfpService.v201311.CustomFieldService); // Get the LineItemService. LineItemService lineItemService = (LineItemService)user.GetService( DfpService.v201311.LineItemService); // Set the IDs of the custom fields, custom field option, and line item. long customFieldId = long.Parse(_T("INSERT_STRING_CUSTOM_FIELD_ID_HERE")); long dropDownCustomFieldId = long.Parse(_T("INSERT_DROP_DOWN_CUSTOM_FIELD_ID_HERE")); long customFieldOptionId = long.Parse(_T("INSERT_CUSTOM_FIELD_OPTION_ID_HERE")); long lineItemId = long.Parse(_T("INSERT_LINE_ITEM_ID_HERE")); try { // Get the custom field. CustomField customField = customFieldService.getCustomField(customFieldId); // Get the drop-down custom field. DropDownCustomField dropDownCustomField = (DropDownCustomField)customFieldService.getCustomField(dropDownCustomFieldId); // Get the line item. LineItem lineItem = lineItemService.getLineItem(lineItemId); // Create custom field values. List <BaseCustomFieldValue> customFieldValues = new List <BaseCustomFieldValue>(); TextValue textValue = new TextValue(); textValue.value = "Custom field value"; CustomFieldValue customFieldValue = new CustomFieldValue(); customFieldValue.customFieldId = customFieldId; customFieldValue.value = textValue; customFieldValues.Add(customFieldValue); DropDownCustomFieldValue dropDownCustomFieldValue = new DropDownCustomFieldValue(); dropDownCustomFieldValue.customFieldId = dropDownCustomFieldId; dropDownCustomFieldValue.customFieldOptionId = customFieldOptionId; customFieldValues.Add(dropDownCustomFieldValue); // Only add existing custom field values for different custom fields than // the ones you are setting. if (lineItem.customFieldValues != null) { foreach (BaseCustomFieldValue oldCustomFieldValue in lineItem.customFieldValues) { if (!(oldCustomFieldValue.customFieldId == customFieldId) && !(oldCustomFieldValue.customFieldId == dropDownCustomFieldId)) { customFieldValues.Add(oldCustomFieldValue); } } } lineItem.customFieldValues = customFieldValues.ToArray(); // Update the line item on the server. LineItem[] lineItems = lineItemService.updateLineItems(new LineItem[] { lineItem }); if (lineItems != null) { foreach (LineItem updatedLineItem in lineItems) { List <String> customFieldValueStrings = new List <String>(); foreach (BaseCustomFieldValue baseCustomFieldValue in lineItem.customFieldValues) { if (baseCustomFieldValue is CustomFieldValue && ((CustomFieldValue)baseCustomFieldValue).value is TextValue) { customFieldValueStrings.Add("{ID: '" + baseCustomFieldValue.customFieldId + "', value: '" + ((TextValue)((CustomFieldValue)baseCustomFieldValue).value).value + "'}"); } else if (baseCustomFieldValue is DropDownCustomFieldValue) { customFieldValueStrings.Add("{ID: '" + baseCustomFieldValue.customFieldId + "', custom field option ID: '" + ((DropDownCustomFieldValue)baseCustomFieldValue).customFieldOptionId + "'}"); } } Console.WriteLine("A line item with ID \"{0}\" set with custom field values \"{1}\".", updatedLineItem.id, string.Join(", ", customFieldValueStrings.ToArray())); } } else { Console.WriteLine("No line items were updated."); } } catch (Exception ex) { Console.WriteLine("Failed to update line items. Exception says \"{0}\"", ex.Message); } }