I'm provisioning SPO sites based on a request workflow. One of the requirements is to add the workflow author to the O365 group which is provisioned automatically when the site is created. I'm using the PnP library to provision the site but the O365 group manipulation requires MS Graph access.

I'm roughly provisiong the new site like this:

teamContext = await rootContext.CreateSiteAsync(new TeamSiteCollectionCreationInformation
    Alias = request.SiteUrl, // Mandatory
    DisplayName = request.SiteTitle, // Mandatory
    Description = request.SiteDescription, // Optional
    Classification = request.Sensitivity, // Optional
    IsPublic = request.ShouldBePublic // Optional, default true

This all works fine and once the site is created I can get at the O365 group id like this:

private void GetO365SPGroup()
  string graphToken = GetGraphToken();
  List<UnifiedGroupEntity> group = UnifiedGroupsUtility.ListUnifiedGroups(graphToken, request.SiteTitle);
  var myGroup = group.First();
  myGroup.GroupId.Dump("Group Id"); //LINQPad

but this code assumes that there will be only one group matching the given site title. Admittedly I am not a SP dev so I don't know if it's possible to have Unified Groups with the same display name, but since request.SiteTitle not an identifier it makes me uncomfortable.

I tried looking at the AssociatedOwners group:

var ownerGroup = teamContext.Web.AssociatedOwnerGroup;

but the id property here is not the guid of an O365 group, but rather a low-value int which is probably a SharePoint group link identifier instead.

My question is if there is a way to look at the teamContext.Web which I know points to the new site and somehow extract the Id of the O365 group so that I can get that group using the Graph API:

string whatIsThisId="guid here";
var owners = await graphClient.Groups[whatIsThisId].Owners.Request().GetAsync()

To solve your issue, instead of provisioning Modern team sites using CreateSiteAsync method, you should provision your site using the CreateUnifiedGroup method of PnP.

This will return your Office 365 group object and it will also provision a Modern team site in SharePoint along with other O365 artefacts. You will need the graph access token to create this.

When you use this method, it will do the exact same thing as the CreateSiteAsync method with the advantage that you can also set the workflow author to this group. Here, I am adding the author to the owner group, but you can also add the author or other user(s) to the member group. You can also optionally set the group logo by sending a stream or passing a URL string of a file.

You can use the method as:

// add your workflow author in this string array
string[] ownerArray = new string[] { "user@tenant.onmicrosoft.com" };

// pass the displayName, description, alias and other parameters
var group = UnifiedGroupsUtility.CreateUnifiedGroup(request.SiteTitle,
request.SiteDescription, request.SiteUrl ,accessToken ,owners:ownerArray, 
isPrivate:request.ShouldBePublic, groupLogo:null);

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

// Get group based on groupID
var createdGroup = UnifiedGroupsUtility.GetUnifiedGroup(groupId, accessToken);
// Get SharePoint site URL from group id
var siteUrl = UnifiedGroupsUtility.GetUnifiedGroupSiteUrl(groupId, accessToken);

Reference - Provisioning Modern Team sites


To answer the original question, you can get the groupID of a site after it is created using CSOM. If a site is not group connected, the API will return a new GUID.

private static string GetSiteGroupID(ClientContext ctx)
    var spSite = ctx.Site;
    ctx.Load(spSite, s => s.GroupId);

    if (spSite.GroupId == new Guid())
        return null;
        return spSite.GroupId.ToString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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