Erskan
Erskan

Reputation: 118

Get extended property Microsoft graph javascript api

I have assigned extended property to my primary calendar and im trying to retrieve that but im not getting it to work.

The docs im following is graph api and javascript sdk

function postUserCalendar(emailAddress) {
var rand = guid();
const values = {
  MultiValueExtendedProperties: [{
    PropertyId: 'StringArray ' + '{' + rand + '}' + ' Name Palette',
    Value: ["Green", "Aqua", "Blue"]
  }]
};
getAccessToken(function(accessToken) {
  if (accessToken) {
    var client = MicrosoftGraph.Client.init({
      authProvider: (done) => {
        done(null, accessToken);
      }
    });
    client
      .api('me/calendars/myCalendarId')
      .header('Accept','application/json')
      .patch({
        message: values
      }, (err, res) => {
        console.log(err);
        console.log(res);
      });
  } else {
    var error = {
      responseText: "Cound not retrieve access token"
    };

  }
 });
}

Above is my function to set MultiValueExtendedProperties. Then i have a function where i try to retrieve those properties.

  function getUserCalendar(emailAddress, callback) {
getAccessToken(function(accessToken) {
  if (accessToken) {
    // Create a Graph client
    var client = MicrosoftGraph.Client.init({
      authProvider: (done) => {
        // Just return the token
        done(null, accessToken);
      }
    });
    client
      .api('/me/calendars/myCalendarId')
      .header('X-AnchorMailbox', emailAddress)
      .expand('multiValueExtendedProperties')
      .filter('id eq {c56fe371-87fb-87a8-1727-9b2b272b9f76}')
      .get((err, res) => {
        if (err) {
          callback(null, err);
        } else {
          callback(res.value);
        }
      });
  } else {
    var error = {
      responseText: 'Could not retrieve access token'
    };
    callback(null, error);
  }
 });
}

The get request has a status of 200 but i get a jquery error "a is not defined"

If someone can point me in the right direction i would appreciate it.

Thanks!

Upvotes: 2

Views: 554

Answers (1)

Jason Johnston
Jason Johnston

Reputation: 17692

There's a few mistakes in your code that are causing problems.

Patching the calendar

Here you pass {message: values} as the first parameter to patch, which causes incorrect JSON to be passed in the payload. You should just pass values itself, which will serialize correctly:

client
    .api('me/calendars/myCalendarId')
    .header('Accept','application/json')
    .patch(values, (err, res) => {
        console.log(err);
        console.log(res);
     });

Getting the calendar with property expanded

Here you don't want to use the filter method from the SDK. That adds a filter clause to the GET, instead of adding a filter to the expand, which is what you want. Instead, embed the filter in the string passed to expand, like so:

client
    .api('/me/calendars/myCalendarId')
    .header('X-AnchorMailbox', emailAddress)
    .expand("multiValueExtendedProperties($filter=id%20eq%20'StringArray%20{66f5a359-4659-4830-9070-00050ec6ac6e}%20Name%20Palette')")
    .get((err, res) => {
        if (err) {
            callback(null, err);
        } else {
            callback(res);
        }
    });

Consider using open extensions instead

We recommend using open extensions for storing custom data instead of legacy extended properties. Open extensions are more flexible and easier to work with. Unless there's some compelling reason you need it to be a legacy property (like backwards-compat with an existing solution, etc.), I'd recommend open extensions.

Upvotes: 4

Related Questions