Sharepoint - How can I get the office 365 group id for a modern site I've just provisioned?
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[] { "[email protected]" };
// 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