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

Visual Studio Toolbox: UX Design and Code Generation for Angular Apps
January 10, 2019
@SharePoint Microsoft SharePoint
January 10, 2019

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

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 = "[email protected]";
             string password = "****";
             string tenantName = "";
             string authString = "" + tenantName;
             string resource = "";
                 AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
                 string clientId = "***********";
                 string clientSecret = "**********";
                 string url = string.Format("{0}/oauth2/token", tenantName);
                 var client = new RestClient("");
                 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", "------WebKitFormBoundary7MA4YWxkTrZu0gWrnContent-Disposition: form-data; name="grant_type"rnrnpasswordrn------WebKitFormBoundary7MA4YWxkTrZu0gWrnContent-Disposition: form-data; name="username"rnrn{0}rn------WebKitFormBoundary7MA4YWxkTrZu0gWrnContent-Disposition: form-data; name="password"rnrn{1}rn------WebKitFormBoundary7MA4YWxkTrZu0gWrnContent-Disposition: form-data; name="client_id"rnrn{2}rn------WebKitFormBoundary7MA4YWxkTrZu0gWrnContent-Disposition: form-data; name="client_secret"rnrn{3}rn------WebKitFormBoundary7MA4YWxkTrZu0gWrnContent-Disposition: form-data; name="resource"rnrn",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:UserssathishDownloadsMyLogo.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);

Happy Coding,

Sathish Nadarajan.

News Reporter
News Reporter
Head of Operations (Banking), Director IT Governance, Teamlead Microsoft, Service Delivery Manager. Interested in Office 365, LAMP, IT Security and much more!