Search code examples
c#oopenumsbing-webmaster-tools

How to set a property whose value is an enum


I'm new to C# and the .NET Framework. I'm building a small console app to "block" URLs from Bing's index because they were accidentally indexed. I'm using the Bing Webmaster API to do this.

I do not understand how to set two properties of the BlockedUrl object (EntityType and RequestType). The BlockedUrl object is passed to AddBlockedUrl when sending the block request.

Setting the property values for Url, Date, and DaysToExpire makes sense - they're given string, DateTime, and DaysToExpire values respectively as their signatures indicate.

Going by the signature of EntityType:

public BlockedUrl.BlockedUrlEntityType EntityType { get; set; }

I don't understand BlockedUrl.BlockedUrlEntityType or how I would work with it. The RequestType property is similar.

My current code is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "https://url/dir/path/";
            var api = new WebmasterApi.WebmasterApiClient();
            var blockedURLObj = new WebmasterApi.BlockedUrl();
            blockedURLObj.Url = url;
            blockedURLObj.Date = new DateTime(2018, 5, 8, 8, 00, 00);
            blockedURLObj.DaysToExpire = 90;
            blockedURLObj.EntityType = "Directory"; //error: "Cannot implicitly convert type 'string' to ConsoleApp1.WebmasterApi.BlockedUrl.BlockedUrlEntityType"
            blockedURLObj.RequestType = "FullRemoval"; //error: "Cannot implicitly convert type 'string' to ConsoleApp1.WebmasterApi.BlockedUrl.BlockedUrlRequestType"

        try
        {
            api.AddBlockedUrl(url, blockedURLObj);
            Console.WriteLine("Success!");
            Console.ReadLine(); 
        }
        catch(Exception e)
        {
            Console.WriteLine(e.ToString());
            Console.ReadLine();
        }
    }
} 

Solution

  • The EntityType property should be set using an enumeration value as dictated here: https://msdn.microsoft.com/en-us/library/hh969362.aspx

    The RequestType proeprty should be set using an enumeration value as dictated here: https://msdn.microsoft.com/en-us/library/hh969383.aspx

    For example:

    blockedURLObj.EntityType = BlockedUrl.BlockedUrlEntityType.Directory;
    blockedURLObj.RequestType = BlockedUrl.BlockedUrlRequestType.FullRemoval;