Exemplo n.º 1
0
        //private BulkInviteResults SubmitToGraphBatch(GraphBatch batch, BulkInviteSubmission submission)
        //{
        //    //** NOTE: this method was last tested against the beta API, circa Q3 CY17 **
        //    var res = new BulkInviteResults();
        //    var batchEndPoint = string.Format("{0}/beta/$batch", Settings.GraphResource);
        //    var serverResponse = CallGraph(batchEndPoint, batch, false, null, _userId);

        //    if (serverResponse.Successful)
        //    {
        //        res.InvitationResults = JsonConvert.DeserializeObject<GraphBatchResponse>(serverResponse.ResponseContent);
        //        if (submission.GroupList.Length > 0)
        //        {
        //            foreach (var item in res.InvitationResults.Responses)
        //            {
        //                var groupsAdded = AddUserToGroup(item.Body.InvitedUser.Id, submission.GroupList.ToList());
        //                if (!groupsAdded.Success)
        //                {
        //                    var resErrors = string.Join(", ", groupsAdded.Responses.Where(r => !r.Successful).Select(r => r.Message));
        //                    res.ErrorMessage += string.Format("\n\rOne or more groups failed while assigning to user \"{0}\" ({1})", item.Body.InvitedUserEmailAddress, resErrors);
        //                }
        //            }
        //        }
        //    }
        //    else
        //    {
        //        res.ErrorMessage = serverResponse.Message;
        //    }
        //    return res;
        //}

        private async Task <BulkInviteResults> SubmitLocally(GraphBatch batch, BulkInviteSubmission submission)
        {
            var res = new BulkInviteResults(submission.Id);

            GraphInvitation itemRes;
            var             mailTemplate = await TemplateUtilities.GetTemplate(submission.InviteTemplateId);

            try
            {
                foreach (var item in batch.Requests)
                {
                    itemRes = await SendGraphInvitationAsync(item.Body, submission.GroupList.ToList(), null, mailTemplate, _accessToken);

                    if (itemRes.Status != "Error")
                    {
                        submission.ItemsProcessed += 1;
                        submission = await BulkInviteSubmission.UpdateItem(submission);
                    }
                    item.Request.Status = itemRes.Status;
                    await GuestRequestRules.UpdateAsync(item.Request);

                    res.InvitationResults.Responses.Add(new BulkResponse
                    {
                        Status = itemRes.Status,
                        Body   = itemRes,
                        Id     = itemRes.id
                    });
                }

                await BulkInviteResults.AddItem(res);

                return(res);
            }
            catch (Exception ex)
            {
                res.ErrorMessage = ex.Message;
                await BulkInviteResults.AddItem(res);

                return(res);
            }
        }
Exemplo n.º 2
0
        internal static async Task <Dictionary <string, string> > GetUserIdsBatched(HttpClient httpClient, string accessToken, string[] userPrincipalNames)
        {
            Dictionary <string, string> returnValue = new Dictionary <string, string>();

            Dictionary <string, string> requests = new Dictionary <string, string>();
            var batch = new GraphBatch();
            int id    = 0;

            foreach (var upn in userPrincipalNames)
            {
                id++;
                batch.Requests.Add(new GraphBatchRequest()
                {
                    Id = id.ToString(), Method = "GET", Url = $"/users/{upn}?$select=Id"
                });
                requests.Add(id.ToString(), upn);
            }
            var stringContent = new StringContent(JsonSerializer.Serialize(batch));

            stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            var result = await GraphHelper.PostAsync <GraphBatchResponse>(httpClient, "v1.0/$batch", stringContent, accessToken);

            if (result.Responses != null && result.Responses.Any())
            {
                foreach (var response in result.Responses)
                {
                    var groupId = requests.First(r => r.Key == response.Id).Value;
                    if (response.Body.TryGetValue("id", out object propertyObject))
                    {
                        var element = (JsonElement)propertyObject;
                        returnValue.Add(groupId, element.GetString());
                    }
                }
            }
            return(returnValue);
        }
Exemplo n.º 3
0
        public async Task <BulkInviteResults> ProcessBulkInvitations(BulkInviteSubmission submission)
        {
            var res = new BulkInviteResults(submission.Id);

            try
            {
                var batch          = new GraphBatch();
                int counter        = 0;
                var inviteEndPoint = string.Format("/{0}/invitations", Settings.GraphApiVersion);
                var headerColl     = new Dictionary <string, string>
                {
                    { "Content-Type", "application/json" }
                };

                var items = await BulkInviteSubmission.GetGuestRequestsPending(submission.Id);

                foreach (var item in items)
                {
                    counter++;
                    // Setup invitation
                    GraphInvitation invitation = new GraphInvitation()
                    {
                        InvitedUserDisplayName  = item.EmailAddress,
                        InvitedUserEmailAddress = item.EmailAddress,
                        InviteRedirectUrl       = _profileUrl,
                        SendInvitationMessage   = (!Settings.UseSMTP),
                        InvitedUserType         = submission.MemberType.ToString()
                    };

                    if (submission.InvitationMessage.Length > 0)
                    {
                        invitation.InvitedUserMessageInfo = new InvitedUserMessageInfo
                        {
                            CustomizedMessageBody = submission.InvitationMessage
                        };
                    }

                    batch.Requests.Add(new BulkInviteRequest
                    {
                        Id             = counter.ToString(),
                        GuestRequestId = item.Id,
                        Request        = item,
                        Method         = "POST",
                        Headers        = headerColl,
                        Url            = inviteEndPoint,
                        Body           = invitation
                    });
                }

                /* NOTE:
                 * This process is designed to leverage the Microsoft Graph batch processor:
                 *     https://developer.microsoft.com/en-us/graph/docs/concepts/json_batching
                 * However, the batch processor is currently (2018) in preview and limited to 20 submissions per request
                 * For the time being, we'll loop the collection and make individual synchronous calls
                 */
                //res = SubmitToGraphBatch(batch, submission, userId);

                res = await SubmitLocally(batch, submission);


                return(res);
            }
            catch (Exception ex)
            {
                var msg = "Error processing bulk invitation";
                res.ErrorMessage = Logging.WriteToAppLog(msg, System.Diagnostics.EventLogEntryType.Error, ex);
                await BulkInviteResults.AddItem(res);

                return(res);
            }
        }