Search code examples
c#microsoft-graph-api

How to work with single-value extended properties?


I have searched everywhere for an instruction, how to work with single-value extended properties, but have not found something like that.

In our application, we want to integrate voting options with MS Graph. Unfortunately, it is not official available yet, so we have to create our own solution. I found this one: https://learn.microsoft.com/en-us/answers/questions/826338/adding-voting-option-in-microsoft-graph-api

Now the question is, how I can work with single-value properties in C#. Are there any classes to work with, maybe SingleValueLegacyExtendedProperty?


Solution

  • In SDK v5, when reading single-value extended property, you have to specify the property in expand query parameter:

    var result = await _client.Me.Messages.GetAsync((requestConfiguration) =>
    {
        requestConfiguration.QueryParameters.Expand = new string[] { "singleValueExtendedProperties($filter=id+eq+'Binary {00062008-0000-0000-C000-000000000046} Id 0x00008520')" };
    });
    
    // get first message with single-value extended property
    var message = result.Value.FirstOrDefault(x => x.SingleValueExtendedProperties != null);
    var votingData = message.SingleValueExtendedProperties[0].Value;
    

    To create a new message with single-value extended property

    var body = new SendMailPostRequestBody
    {
        Message = new Message
        {
            Subject = "Voting",
            Sender = new Recipient
            {
                EmailAddress = new EmailAddress
                {
                    Address = "[email protected]"
                }
            },
            ToRecipients = new List<Recipient>
            {
                 new Recipient
                 {
                     EmailAddress = new EmailAddress { Address = "[email protected]" }
                 }
             },
             Body = new ItemBody
             {
                 ContentType = BodyType.Html,
                 Content = "Please vote"
             },
             SingleValueExtendedProperties = new List<SingleValueLegacyExtendedProperty>
             {
                 new SingleValueLegacyExtendedProperty
                 {
                     Id = "Binary {00062008-0000-0000-C000-000000000046} Id 0x00008520",
                     Value = "data"
                 }
             }
         },
         SaveToSentItems = true
    };
    await _client.Me.SendMail.PostAsync(body);
    

    For older version of SDK like v4 and v3

    var response = await client.Me.Messages.Request()
                .Expand("singleValueExtendedProperties($filter=id+eq+'Binary {00062008-0000-0000-C000-000000000046} Id 0x00008520')")
                .GetAsync();
    
    var message = response.FirstOrDefault(x => x.SingleValueExtendedProperties != null);
    var votingData = message.SingleValueExtendedProperties[0].Value;
    

    New message

    var message = new Message
    {
        Subject = "Voting",
        Sender = new Recipient
        {
            EmailAddress = new EmailAddress
            {
                Address = "[email protected]"
            }
        },
        ToRecipients = new List<Recipient>
        {
            new Recipient
            {
                EmailAddress = new EmailAddress { Address = "[email protected]" }
            }
        },
        Body = new ItemBody
        {
            ContentType = BodyType.Html,
            Content = "Please vote"
        },
        SingleValueExtendedProperties = new MessageSingleValueExtendedPropertiesCollectionPage
        {
            new SingleValueLegacyExtendedProperty
            {
                Id = "Binary {00062008-0000-0000-C000-000000000046} Id 0x00008520",
                Value = "data"
            }
        }
    };
    await client.Me.SendMail(message, true).Request().PostAsync();