7

Since team sites will not support "site inboxes" for team sites anymore I need to create an "Office 365 Group" (don't confuse this with SP groups/AD Groups), since this is the MS silver bullet of the month.

I'm currently using a clientcontext for all my csom magic, but here, "Provisioning a "modern" team site using the PnP CSOM core component" they want an accessToken. I can't figure out how to create one, presumably from the clientcontext?

Any tutorials how to create these groups without the accesstoken or how do I create one?

2
  • I've just tried this example in your link. And it created a new Modern group. After running Connect-PnPMicrosoftGraph - it will ask for login and password. Then this command gets the token: Get-PnPAccessToken. Oct 16, 2017 at 2:03
  • +1 for "since this is the MS silver bullet of the month"
    – Evariste
    Apr 18, 2018 at 9:52

1 Answer 1

4

You first need to create and register an Azure AD app in the Azure portal. That app needs the delegated Group.ReadWrite.All permission from Microsoft Graph API.

Then you need to get the client ID and client secret of that app. We will need to use that in our application.

Once you have that, you can try and modify the below code:

static async Task MainAsync()
{

    string userName = "[email protected]";
    string password = "password";

    List<KeyValuePair<string, string>> vals = new List<KeyValuePair<string, string>>();

    string tenantName = "tenantname.onmicrosoft.com";
    string authString = "https://login.microsoftonline.com/" + tenantName;
    string resource = "https://graph.microsoft.com";

    AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);

    // Config for OAuth client credentials  

    string clientId = "client-id";
    string key = "client-secret";

    vals.Add(new KeyValuePair<string, string>("client_id", clientId));
    vals.Add(new KeyValuePair<string, string>("resource", resource));
    vals.Add(new KeyValuePair<string, string>("username", userName));
    vals.Add(new KeyValuePair<string, string>("password", password));
    vals.Add(new KeyValuePair<string, string>("grant_type", "password"));
    vals.Add(new KeyValuePair<string, string>("client_secret", key));

    string url = string.Format("https://login.windows.net/{0}/oauth2/token", tenantName);

    using (HttpClient httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
        HttpContent content = new FormUrlEncodedContent(vals);
        HttpResponseMessage hrm = httpClient.PostAsync(url, content).Result;

        AuthenticationResponse authenticationResponse = null;
        if (hrm.IsSuccessStatusCode)
        {
            Stream data = await hrm.Content.ReadAsStreamAsync();
            DataContractJsonSerializer serializer = new
          DataContractJsonSerializer(typeof(AuthenticationResponse));
            authenticationResponse = (AuthenticationResponse)serializer.ReadObject(data);

            var accessToken = authenticationResponse.access_token;

            Stream groupLogoStream = new FileStream("C:\\groupassets\\logo-original.png",
                                    FileMode.Open, FileAccess.Read);
            var group = UnifiedGroupsUtility.CreateUnifiedGroup("displayName", "description",
                                    "mymodernteamsite", accessToken, groupLogo: groupLogoStream);

            // We received a group entity containing information about the group
            string groupUrl = group.SiteUrl;
            string groupId = group.GroupId;


        }
    }

}

Have written elongated blog post here - Create modern team sites with CSOM C# and access token

@admins/moderators - have added link to my blog as it will be too big an answer here. However, have added the necessary and relevant code portion that will be enough for the OP to go ahead. Do let me know if I should add more here.

2
  • Thanks Gautam! This looks awesome. Unfortunately Im in another project now, and I might not be going back, but if I have the chance to test this I will be happy so set it as the correct solution.
    – Cowborg
    Oct 16, 2017 at 13:32
  • I have tested this and can confirm that the code provided on the mentioned blog post works.
    – Danny
    Jul 25, 2018 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.