/// <summary>
        /// This is Preview Code.
        /// Create Power BI Group using, graph API.
        /// This code will be changed shortly when the new PowerBI REST API for group creation is implemented
        /// </summary>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public static async Task <O365Group> CreateGroup(string groupName)
        {
            var newGroup = new O365Group
            {
                DisplayName     = groupName,
                Description     = "Autogenerated by Migration Sample",
                MailNickname    = groupName,
                MailEnabled     = true,
                Visibility      = "Private",
                SecurityEnabled = false,
                GroupTypes      = new List <string> {
                    "Unified"
                }
            };

            System.Net.WebRequest request = System.Net.WebRequest.Create(
                String.Format("{0}/groups",
                              GraphUrlWithVersion)) as System.Net.HttpWebRequest;

            request.Method = "POST";
            request.Headers.Add("Authorization", String.Format("Bearer {0}", AzureTokenManager.GetGraphToken()));
            request.ContentType = "application/json";
            byte[] postBytes     = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(newGroup));
            Stream requestStream = request.GetRequestStream();

            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();

            O365Group createdGroup = new O365Group();

            using (var response = await request.GetResponseAsync() as System.Net.HttpWebResponse)
            {
                //Get reader from response stream
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    //Deserialize JSON string
                    createdGroup = JsonConvert.DeserializeObject <O365Group>(reader.ReadToEnd());
                }
            }

            if (!await AddToGroup(createdGroup.Id, AzureTokenManager.GetGraphToken(), AzureTokenManager.GetGraphUserUniqueId()))
            {
                await DeleteGroup(createdGroup.Id);

                return(null);
            }

            return(createdGroup);
        }
        /// <summary>
        /// This is Preview Code.
        /// Delete Power BI Group using, graph API.
        /// This code will be changed shortly when the new PowerBI REST API for group creation is implemented
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public static async Task DeleteGroup(string groupId)
        {
            System.Net.WebRequest request = System.Net.WebRequest.Create(
                String.Format("{0}/groups/{1}",
                              GraphUrlWithVersion,
                              groupId)) as System.Net.HttpWebRequest;

            request.Method = "DELETE";
            request.Headers.Add("Authorization", String.Format("Bearer {0}", AzureTokenManager.GetGraphToken()));

            using (var response = await request.GetResponseAsync() as System.Net.HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.NoContent)
                {
                    throw new Exception($"Failed to delete group: {groupId}");
                }
            }
        }
 public string GetAzureToken()
 {
     return(AzureTokenManager.GetAzureToken(Context.Environment));
 }
 public void SetAzureToken(AuthenticationResult authenticationResult)
 {
     AzureTokenManager.SetAzureToken(authenticationResult, Context.Environment);
 }
        private static PowerBIClient CreatePowerBIClient()
        {
            var credentials = new TokenCredentials(AzureTokenManager.GetPowerBISaaSToken());

            return(new PowerBIClient(new Uri($"{ConfigurationManager.AppSettings["powerbi-service-apiEndpointUri"]}"), credentials));
        }