/// <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 ContentMetadataKeyHierarchy service. ContentMetadataKeyHierarchyService contentMetadataKeyHierarchyService = (ContentMetadataKeyHierarchyService) user.GetService( DfpService.v201505.ContentMetadataKeyHierarchyService); // Set the ID of the content metadata key hierarchy to update. long contentMetadataKeyHierarchyId = long.Parse(_T("INSERT_CONTENT_METADATA_KEY_HIERARCHY_ID_HERE")); // Set the ID of the custom targeting key to be added as a hierarchy level long customTargetingKeyId = long.Parse(_T("INSERT_CUSTOM_TARGETING_KEY_ID_HERE")); // Create a statement to get content metadata key hierarchies. StatementBuilder statementBuilder = new StatementBuilder() .Where("WHERE id = :id") .OrderBy("id ASC") .Limit(1) .AddValue("id", contentMetadataKeyHierarchyId); try { ContentMetadataKeyHierarchyPage page = contentMetadataKeyHierarchyService .getContentMetadataKeyHierarchiesByStatement(statementBuilder.ToStatement()); ContentMetadataKeyHierarchy contentMetadataKeyHierarchy = page.results[0]; // Update the content metadata key hierarchy by adding a hierarchy level. ContentMetadataKeyHierarchyLevel[] hierarchyLevels = contentMetadataKeyHierarchy .hierarchyLevels; ContentMetadataKeyHierarchyLevel hierarchyLevel = new ContentMetadataKeyHierarchyLevel(); hierarchyLevel.customTargetingKeyId = customTargetingKeyId; hierarchyLevel.hierarchyLevel = hierarchyLevels.Length + 1; List<ContentMetadataKeyHierarchyLevel> updatedHieratchyLevels = new List<ContentMetadataKeyHierarchyLevel>(); updatedHieratchyLevels.AddRange(hierarchyLevels); updatedHieratchyLevels.Add(hierarchyLevel); contentMetadataKeyHierarchy.hierarchyLevels = updatedHieratchyLevels.ToArray(); // Update the content hierarchy on the server. ContentMetadataKeyHierarchy[] contentMetadataKeyHierarchies = contentMetadataKeyHierarchyService.updateContentMetadataKeyHierarchies( new ContentMetadataKeyHierarchy[] {contentMetadataKeyHierarchy}); foreach (ContentMetadataKeyHierarchy updatedContentMetadataKeyHierarchy in contentMetadataKeyHierarchies) { Console.WriteLine("Content metadata key hierarchy with ID \"{0}\", name " + "\"{1}\" was updated.", updatedContentMetadataKeyHierarchy.id, updatedContentMetadataKeyHierarchy.name); } } catch (Exception e) { Console.WriteLine("Failed to update content metadata key hierarchies. Exception " + "says \"{0}\"", e.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 ContentMetadataKeyHierarchy service. ContentMetadataKeyHierarchyService contentMetadataKeyHierarchyService = (ContentMetadataKeyHierarchyService) user.GetService( DfpService.v201505.ContentMetadataKeyHierarchyService); // Set the IDs of the custom targeting keys for the hierarchy. long customTargetingKeyId1 = long.Parse(_T("INSERT_LEVEL_ONE_CUSTOM_TARGETING_KEY_ID_HERE")); long customTargetingKeyId2 = long.Parse(_T("INSERT_LEVEL_TWO_CUSTOM_TARGETING_KEY_ID_HERE")); List<ContentMetadataKeyHierarchyLevel> hierarchyLevels = new List<ContentMetadataKeyHierarchyLevel>(); ContentMetadataKeyHierarchyLevel hierarchyLevel1 = new ContentMetadataKeyHierarchyLevel(); hierarchyLevel1.customTargetingKeyId = customTargetingKeyId1; hierarchyLevel1.hierarchyLevel = 1; hierarchyLevels.Add(hierarchyLevel1); ContentMetadataKeyHierarchyLevel hierarchyLevel2 = new ContentMetadataKeyHierarchyLevel(); hierarchyLevel2.customTargetingKeyId = customTargetingKeyId2; hierarchyLevel2.hierarchyLevel = 2; hierarchyLevels.Add(hierarchyLevel2); ContentMetadataKeyHierarchy contentMetadataKeyHierarchy = new ContentMetadataKeyHierarchy(); contentMetadataKeyHierarchy.name = "Content hierarchy #" + new Random().Next(int.MaxValue); contentMetadataKeyHierarchy.hierarchyLevels = hierarchyLevels.ToArray(); try { // Create the content metadata key hierarchy on the server. ContentMetadataKeyHierarchy[] contentMetadataKeyHierarchies = contentMetadataKeyHierarchyService.createContentMetadataKeyHierarchies( new ContentMetadataKeyHierarchy[] {contentMetadataKeyHierarchy}); foreach (ContentMetadataKeyHierarchy createdContentMetadataKeyHierarchy in contentMetadataKeyHierarchies) { Console.WriteLine("A content metadata key hierarchy with ID \"{0}\", name " + "\"{1}\" and {2} levels was created.", createdContentMetadataKeyHierarchy.id, createdContentMetadataKeyHierarchy.name, createdContentMetadataKeyHierarchy.hierarchyLevels.Length); } } catch (Exception ex) { Console.WriteLine("Failed to create content metadata key hierarchies. Exception says " + "\"{0}\"", ex.Message); } }