Andrew Truckle
Andrew Truckle

Reputation: 19097

Use batch mode to delete many calendar events with Microsoft Graph

This is how I delete calendar events at the moment (see answer):

Deleting Calendar Events

However, I would like to take this a step further and use batching.

From what I can tell batch features in Microsoft Graph are in beta?

How can I batch all my delete requests and perform one action rather than many? The article refers to using JSON. Eg:

{
  "requests": [
    {
      "id": "1",
      "method": "GET",
      "url": "/me/drive/root:/{file}:/content"
    },
    {
      "id": "2",
      "method": "GET",
      "url": "/me/planner/tasks"
    },
    {
      "id": "3",
      "method": "GET",
      "url": "/groups/{id}/events"
    }
  ]
}

I don't understand how to do it like that from C# with my List<Event> collection.

Upvotes: 1

Views: 1469

Answers (1)

Marek Rycharski
Marek Rycharski

Reputation: 1704

Yes, batching is currently in preview and only available in /beta version. The client SDK that you use in the referenced code is only available for /v1.0. You'd need to construct a REST request in your code, for example:

POST https://graph.microsoft.com/beta/$batch
Accept: application/json
Content-Type: application/json

{
  "requests": [
    {
      "id": "1",
      "method": "DELETE",
      "url": "/me/events/event_id1"
    },
    {
      "id": "2",
      "method": "DELETE",
      "url": "/me/events/event_id2"
    },
  ]
}

How to make a REST request ?

There is a good article here. It provides a simple class:

using System;
using System.IO;
using System.Net;
using System.Text;

public enum HttpVerb
{
    GET,
    POST,
    PUT,
    DELETE
}

namespace HttpUtils
{
  public class RestClient
  {
    public string EndPoint { get; set; }
    public HttpVerb Method { get; set; }
    public string ContentType { get; set; }
    public string PostData { get; set; }

    public RestClient()
    {
      EndPoint = "";
      Method = HttpVerb.GET;
      ContentType = "text/xml";
      PostData = "";
    }
    public RestClient(string endpoint)
    {
      EndPoint = endpoint;
      Method = HttpVerb.GET;
      ContentType = "text/xml";
      PostData = "";
    }
    public RestClient(string endpoint, HttpVerb method)
    {
      EndPoint = endpoint;
      Method = method;
      ContentType = "text/xml";
      PostData = "";
    }

    public RestClient(string endpoint, HttpVerb method, string postData)
    {
      EndPoint = endpoint;
      Method = method;
      ContentType = "text/xml";
      PostData = postData;
    }


    public string MakeRequest()
    {
      return MakeRequest("");
    }

    public string MakeRequest(string parameters)
    {
      var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);

      request.Method = Method.ToString();
      request.ContentLength = 0;
      request.ContentType = ContentType;

      if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST)
      {
        var encoding = new UTF8Encoding();
        var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);
        request.ContentLength = bytes.Length;

        using (var writeStream = request.GetRequestStream())
        {
          writeStream.Write(bytes, 0, bytes.Length);
        }
      }

      using (var response = (HttpWebResponse)request.GetResponse())
      {
        var responseValue = string.Empty;

        if (response.StatusCode != HttpStatusCode.OK)
        {
          var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
          throw new ApplicationException(message);
        }

        // grab the response
        using (var responseStream = response.GetResponseStream())
        {
          if (responseStream != null)
            using (var reader = new StreamReader(responseStream))
            {
              responseValue = reader.ReadToEnd();
            }
        }

        return responseValue;
      }
    }

  } // class

}

So, using this class you can now write REST requests. For example (from the article):

var client = new RestClient();
client.EndPoint = @"http:\\myRestService.com\api\"; ;
client.Method = HttpVerb.POST;
client.PostData = "{postData: value}";
var json = client.MakeRequest();

Upvotes: 2

Related Questions