Step By Step Procedure To Create (Provision) Office 365 Groups Programmatically Using C# And Microsoft Graph

Sathish Nadarajan
 
Solution Architect
January 10, 2019
 
Rate this article
[Total: 0    Average: 0/5]
 
Views
1968

In this article, let us see how to create a Office 365 Programmatically using C# and the Graph. Let us see the step by step procedures asusual.

1. Create the Azure Active Directory Application. For that, login to the Azure portal corresponding to our Office 365 tenant. Click on the App Registrations.

2. Click on “New application registration”

3. Create an App by providing proper Name and Type as below.

 

 

4. Make note of the Application ID from the below screen. We will be using this later on the code below.

5. Click on the Settings -> Required Permissions and grant the access to Microsoft Graph component.

6. After adding the Microsoft Graph, Grant Permissions

7. Now, click on the Keys and Generate the ClientSecret Key.

8. Copy the Key value, as we cannot retrieve this Key later once we move out of this page. Make a note of this Key as well.

9. So, we have the ClientID, ClientsecretKey, UserName, password of our O365 tenant. With the above details, the below code will create the Office 365 group and the corresponding Team Site.

10. In the below piece of code, I have used the RestSharp to make the API Call. Please refer here for the RestSharp implementation.

 public static void CreateOffice365GroupWithGraph()
          {
              List<KeyValuePair<string, string>> vals = new List<KeyValuePair<string, string>>();
   
              string userName = "sathish@sppals.com";
              string password = "****";
              string tenantName = "sppalsmvp.onmicrosoft.com";
              string authString = "https://login.microsoftonline.com/" + tenantName;
              string resource = "https://graph.microsoft.com";
   
              try
              {
                  AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
   
                  string clientId = "***********";
                  string clientSecret = "**********";
   
                  string url = string.Format("https://login.windows.net/{0}/oauth2/token", tenantName);
   
                  var client = new RestClient("https://login.windows.net/sppalsmvp.onmicrosoft.com/oauth2/token");
                  var request = new RestRequest(Method.POST);
   
                  request.AddHeader("Postman-Token", "93283b8f-f6f9-426d-9250-4a5000b05bbb");
                  request.AddHeader("cache-control", "no-cache");
                  request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
                  request.AddParameter(string.Format("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"grant_type\"\r\n\r\npassword\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\n{0}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\n{1}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"client_id\"\r\n\r\n{2}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"client_secret\"\r\n\r\n{3}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"resource\"\r\n\r\nhttps://graph.microsoft.com\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",userName, password,clientId, clientSecret), ParameterType.RequestBody);
   
                  IRestResponse response = client.Execute(request);
   
                  var jo = JObject.Parse(response.Content);
   
                  string accessToken = Convert.ToString(jo["access_token"]);
   
                  Stream groupLogoStream = new FileStream("C:\\Users\\sathish\\Downloads\\MyLogo.png",
                                  FileMode.Open, FileAccess.Read);
   
                  var group = UnifiedGroupsUtility.CreateUnifiedGroup("DemoGroup", "DemoGroup Created through Program",
                                  "TelenetGroup", accessToken, groupLogo: groupLogoStream);
   
              }
              catch (Exception ex)
              {
                  System.Console.WriteLine("Exception occurred : " + ex.Message);
                  System.Console.ReadLine();
              }
          }

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
[Total: 0    Average: 0/5]
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 13+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment