Problem while adding user private contacts by batch request

59 Views Asked by At

I try to add User Contacts by batch request.

After asking Google, I found this: https://learn.microsoft.com/en-us/samples/azure-samples/graphapi-batching/batching-requests-to-msgraph/

I coded a function like the example and got this:

public static void importRelevantContactsToUserWithBatch(Manager manager)
{
        List<Contact> succesful = new List<Contact>();
        List<Contact> failed = new List<Contact>();

        int limit = 1;
        int counter = 0;

        var user = users.Where(x => x.Mail == manager.Email).FirstOrDefault();

        var contacts = getRelevantContactsForManager(manager);

        foreach (var contact in contacts)
        {
            var batchRequestContent = new BatchRequestContent();
            var requestUrl = graphServiceClient.Users[user.Id].Contacts.RequestUrl;
            System.Console.WriteLine($"RequestURL: {requestUrl}");
            var request = new HttpRequestMessage(HttpMethod.Post, requestUrl) 
            {
                Content = new StringContent(JsonConvert.SerializeObject(contact.toGraphContact()), Encoding.UTF8, "application/json")
            };
            System.Console.WriteLine(JsonConvert.SerializeObject(contact.toGraphContact()));
            var requestStep = new BatchRequestStep(counter.ToString(), request, null);
            batchRequestContent.AddBatchRequestStep(requestStep);
            var returnedResponse = graphServiceClient.Batch.Request().PostAsync(batchRequestContent);
            returnedResponse.Wait(-1);
            var response = returnedResponse.Result.GetResponseByIdAsync(counter.ToString());
            response.Wait(-1);

            if (response.Result.IsSuccessStatusCode)
            {
                succesful.Add(contact.toGraphContact());
                System.Console.WriteLine($"{contact.toGraphContact().Id} wurde hinzugefügt");
            }
            else
            {
                failed.Add(contact.toGraphContact());
                System.Console.WriteLine($"{contact.toGraphContact().Id} konnte nicht hinzugefügt werden");
            }

            counter++;
        }   

}

Running this code, I got a 400 BadRequest as response.

Can someone give me a hint where my problem is I can't find it

Thanks in advance

The tenant is a O365, the request uses a application authentication with all rights granted, the deletion of contacts by batching works fine so in my opinion the mistake must be within this lines:

var requestUrl = graphServiceClient.Users[user.Id].Contacts.RequestUrl;
                System.Console.WriteLine($"RequestURL: {requestUrl}");
                var request = new HttpRequestMessage(HttpMethod.Post, requestUrl) 
                {
                    Content = new StringContent(JsonConvert.SerializeObject(contact.toGraphContact()), Encoding.UTF8, "application/json")
                };

Serialized Contact JSON:

{"AssistantName":null,"Birthday":null,"BusinessAddress":{"City":"Friesoythe","CountryOrRegion":"DE","PostalCode":"26169","State":"NIE","Street":"Zeppelinring 9","AdditionalData":null,"ODataType":null},"BusinessHomePage":"","BusinessPhones":null,"Children":null,"CompanyName":"Appel  ","Department":"","DisplayName":"Thomas Appel","EmailAddresses":[{"Address":"[email protected]","Name":"Thomas Appel ([email protected])","AdditionalData":null,"ODataType":null}],"FileAs":null,"Generation":null,"GivenName":"Thomas","HomeAddress":{"City":"Friesoythe","CountryOrRegion":"DE","PostalCode":"26169","State":"NIE","Street":"Zeppelinring 9","AdditionalData":null,"ODataType":null},"HomePhones":null,"ImAddresses":null,"Initials":null,"JobTitle":"","Manager":null,"MiddleName":null,"MobilePhone":null,"NickName":null,"OfficeLocation":"53,0191543°7,8857305","OtherAddress":null,"ParentFolderId":null,"PersonalNotes":null,"Profession":null,"SpouseName":null,"Surname":"Appel","Title":null,"YomiCompanyName":null,"YomiGivenName":null,"YomiSurname":null,"Extensions":null,"ExtensionsNextLink":null,"MultiValueExtendedProperties":null,"MultiValueExtendedPropertiesNextLink":null,"Photo":null,"SingleValueExtendedProperties":null,"SingleValueExtendedPropertiesNextLink":null,"Categories":["proALPHA_Export"],"ChangeKey":null,"CreatedDateTime":null,"LastModifiedDateTime":null,"Id":null,"ODataType":"microsoft.graph.contact","AdditionalData":null}
0

There are 0 best solutions below