0

I am building a ASP.NET Core website, where the user signs in, and is able to save things like the theme of the page, which then triggers some js code:

var data = {
    userName: userName,
    key: "theme",
    value: localStorage.getItem("theme")
}

var pendingRequest;

if (pendingRequest) {
    pendingRequest.abort()
    pendingRequest = null;
}

pendingRequest = $.post('/Account/setExtension', data, function (data) {
    pendingRequest = null;
    $('#settingsModal').modal('hide');
});

which calls the controller:

        [HttpPost]
    public void setExtension(string userName, string key, string value)
    {
        Dictionary<string, object> addData = new Dictionary<string, object>
        {
            {key, value }
        };
        GraphHelper.updateExtension(userName, "com.user.roamingSettings", addData).ConfigureAwait(false);
    }

which calls the checks if the extension exists, and then decides to create it, or update the existing extension:

        public static async Task<bool> extensionExistsAsync(string userName, string extensionName)
    {
        try
        {
            var graphClient = GetAuthenticatedClient();

            // if extension doesnt exist
            bool extensionFound = false;
            var extensions = await graphClient.Users[userName].Extensions.Request().GetAsync();

            foreach (var extension in extensions)
            {
                if (extension.Id == extensionName)
                {
                    extensionFound = true;
                }
            }
            return extensionFound;

        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message.ToString());
            throw;
        }
    }

The problem is, the code just stops running on this line:

var extensions = await graphClient.Users[userName].Extensions.Request().GetAsync();

It doesn't throw or anything. Stepping through it line by line, it returns all the way to the assignment, and the output window is empty when it stops. Why is this? How can I get an extension by name, or get all extensions to see which ones exist, either by graph api, or calls?

Luke Vanzweden
  • 466
  • 3
  • 15
  • Please share the code of `GetAuthenticatedClient()`. – Allen Wu Dec 17 '20 at 04:14
  • Have you tested in [graph explorer](https://aka.ms/ge) with the call `https://graph.microsoft.com/v1.0/users/{userid}/extensions`? – Shiva Keshav Varma Dec 17 '20 at 12:21
  • @Shiva-MSFTIdentity Yes, I am able to make calls both with graph explorer and postman just fine, but doing it the way above, it just stops. – Luke Vanzweden Dec 17 '20 at 13:04
  • Hi, if the posted answer resolves your question, please mark it as the answer by clicking the check mark. Doing so helps others find answers to their questions. See https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Shiva Keshav Varma Dec 17 '20 at 13:33
  • This ended up solving it: https://stackoverflow.com/questions/10343632/httpclient-getasync-never-returns-when-using-await-async – Luke Vanzweden Dec 18 '20 at 16:16

1 Answers1

1

When ever you use the below call

var extensions = await graphClient.Users[userName].Extensions.Request().GetAsync();

you will be getting UserExtensionsCollectionPage object which gives the list of extensions of a user.

This page doesn't have Id property, the extension objects that are present in this UserExtensionsCollectionPage object have it. So use the below code to print the id and type of the Extensions.

var extensions = await graphClient.Users["a1ee289f-4cab-4fc3-885f-d4cbefb48895"].Extensions.Request().GetAsync();
foreach(var data in extensions.CurrentPage)
{
     Console.WriteLine("Id: " + data.Id + "Type: " + data.ODataType );
}

This will give you the data as below.

enter image description here

Shiva Keshav Varma
  • 3,398
  • 2
  • 9
  • 13