Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EmployeeExperience.Communities.PostAsync does not return any response #785

Closed
afowler1 opened this issue Jan 5, 2024 · 2 comments
Closed

Comments

@afowler1
Copy link

afowler1 commented Jan 5, 2024

What's the goal?

Create a Viva Engage Community with the EmployeeExperience.Communities.PostAsync method and retrieve the ID of the group.

How is it failing?

Using the below code, I can see that I successfully created the group (via checking the VivaEngage GUI) but the method returns null, so I am unable to tell within the codebase if the group was successfully created and what ID it was assigned.

var requestBody = new Community
{
    DisplayName = "Financial Advice for Software Engineers2",
    Description = "A community where financial advisors who represent customers from software engineering profession can discuss advice and suggestions for there clients.",
    Privacy = CommunityPrivacy.Public,
    AdditionalData = new Dictionary<string, object>
    {
        {
            "[email protected]" , new List<string>
            {
                "https://graph.microsoft.com/beta/users/7df7eeaf-1d8f-45ac-8abd-2c45dfb11c28",
            }
        },
    },
};

var result = await graphClient.EmployeeExperience.Communities.PostAsync(requestBody);

Checking the Viva Engage GUI, I can see that the group is created and I am the owner but the method still returns null.
I think it may be noteworthy that the method returns a Microsoft.Graph.Beta.Models.Community but the documentation suggests that we would receive a http response indicating a success response.

Below is a screenshot during a breakpoint.
image

Used for testing:

latest Graph Beta SDK from Nuget (v5.60.0-preview)
Windows 11
.NET 6

@timward60
Copy link

timward60 commented Jan 5, 2024

The community creation API is asynchronous so it requires some additional work, not sure if there is a better way, but in the meantime you can try this:

First create the community, this will start a long running operation that creates the community asynchronously.

    var community = new Community
    {
      DisplayName = displayName,
      Description = description,
      Privacy = CommunityPrivacy.Public
    };

    var nativeResponseHandler = new NativeResponseHandler();
    await _graphServiceClient.EmployeeExperience.Communities.PostAsync(community, requestConfiguration => requestConfiguration.Options.Add(new ResponseHandlerOption() { ResponseHandler = nativeResponseHandler }));
    using var responseMessage = nativeResponseHandler.Value as HttpResponseMessage;
    if (responseMessage == null || responseMessage?.StatusCode != System.Net.HttpStatusCode.Accepted)
    {
      throw new Exception($"Failed to create community. Status code: {responseMessage?.StatusCode}");
    }

Now that the operation is started we can retrieve it's id from the Location header:

    // Not sure if there is a better way to pass a resource URL to the client.
    // For now just parse the operation id from the URL.
    var location = responseMessage?.Headers.Location!.ToString();
    var startPos = location.IndexOf("('") + "('".Length;
    var length = location.IndexOf("')") - startPos;
    var operationId = location.Substring(startPos, length);

We can now poll the long running operation to wait for the community creation to complete:

    // Now we need to poll the long running operation for status updates.
    string? communityId = null;
    var retryCount = 0;
    while (communityId == null && retryCount <= 5)
    {
      retryCount++;
      await Task.Delay(5000);

      var operation = await _graphServiceClient.EmployeeExperience.EngagementAsyncOperations[operationId].GetAsync();
      if (operation == null)
      {
        continue;
      }

      if (operation.Status.Value == LongRunningOperationStatus.Failed)
      {
        throw new Exception($"Failed to create community: {operation.StatusDetail}");
      }

      if (operation.Status.Value == LongRunningOperationStatus.Succeeded)
      {
        communityId = operation.ResourceId;
        break;
      }
    }

Once it completes successfully, we can get the community:

    if (communityId == null)
    {
      throw new Exception($"Failed to create community. Operation timed out.");
    }

    // Now we have the community id, we can fetch the created community.
    return await _graphServiceClient.EmployeeExperience.Communities[communityId].GetAsync();

@andrueastman
Copy link
Member

Thanks for the response @timward60

As the documentation higlights, the API response is a 202 response code with no body hence why the returned value is null by the sdk.
We'll close this issue for now as the work for a long operation handler is tracked via microsoftgraph/msgraph-sdk-design#83

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants