Step By Step Procedure to Create (Provision) Office 365 Groups Programmatically using C# and Microsoft Graph


Sathish Nadarajan
SharePoint MVP
Published On :   10 Jan 2019
Visit Count
Today :  6    Total :   302



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.

clip_image002[4]

2. Click on “New application registration”

clip_image004[4]

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

clip_image006[4]

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

clip_image008[4]

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

clip_image009

clip_image011

clip_image013

clip_image015

clip_image017

6. After adding the Microsoft Graph, Grant Permissions

clip_image018

clip_image019[4]

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

clip_image021

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.

clip_image022

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.

Categories