How To Change The Site Title Of Modern SharePoint Team Site

Introduction

 
This blog will describe the steps of how to change the site title of a Microsoft 365 group connected to a Modern Team site programmatically. Well! It sounds straightforward. Doesn't it? However, it is not quite.
 

What is a Microsoft 365 group connected Team Site

 
When the SharePoint site connects to Microsoft 365 group, the user get the benefits of other office services like planner, teams, conversation, etc. If you want to modernize the site, then connecting the group would be a better idea.

The Microsoft 365 group will be helpful for managing the permission in a better way. 
 

Microsoft Graph

 
Since the SharePoint site is connected to a Microsoft 365 group, if we do some changes in the SPO site using the classic CSOM way the changes will be overwritten by the Microsoft 365 group. If we use the below code to change the title of the SPO, it will be overwritten by the Microsoft 365 group settings after few minutes. (Give it a try!)
  1. string SiteUrl = "https://<Tenant>.sharepoint.com/sites/blogs";  
  2. string realm = TokenHelper.GetRealmFromTargetUrl(new Uri(SiteUrl));  
  3. string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal,  
  4.                       new Uri(SiteUrl).Authority, realm).AccessToken;  
  5. ClientContext ctx = TokenHelper.GetClientContextWithAccessToken(SiteUrl, accessToken);  
  6. ctx.Web.Title = "Testing";  
  7. ctx.Web.Update();  
  8. ctx.ExecuteQuery();  
So, use the Microsoft graph to update title of the group and it will be reflected in the SPO site as well. 
  1. var group = new Group  
  2.             {  
  3.               DisplayName = "Testing",  
  4.               Description = "Description",  
  5.               GroupTypes = new List<String>() {"Unified"}  
  6.             };  
  7.  var result = await this.GraphServiceClientInstance.Groups[groupId]  
  8.             .Request()  
  9.             .UpdateAsync(group);  
Looks fine!  However, we have a limitation here. The MS graph API will take its time to reflect the changes in the SPO site. (Minimum of 10 min to 4 hours). 
 
Alternate Solution
 
We have the below two undocumented SharePoint APIs, to make changes in the title of Modern SPO site.

This change will get reflected in the site immediately after the API call. 
  1. RestOperations: PATCH  
  2.  X-RequestDigest: <formDigest>  
  3.  Url = https://<Tenant>.sharepoint.com/sites/blogs/_api/SP.Directory.DirectorySession/Group('groupID')  
  4.  Body = {__metadata: {type: "SP.Directory.Group"}, displayName: "newName",   
  5.                 description: "newdescription"};  
  6.  RestOperations: POST  
  7.  X-RequestDigest: <formDigest>  
  8.  Url = https://<Tenant>.sharepoint.com/sites/blogs/_api/GroupService/SyncGroupProperties  
Cheers! Happy Coding!