Manage Site Content Type Using Microsoft Graph Content Type APIs

Introduction

 
Microsoft graph is the doorway to information and knowledge in Microsoft 365. It gives a brought together programmability model that you can use to get to the enormous amount  of information in Microsoft 365. Recently, one more bundle for ContentType APIs has been added in Microsoft Graph Beta.
 
This article will cover Content-Type APIs to Get, Add, Update, Delete activity for Content Type in SharePoint Site.
 

How to enroll application in azure Portal to access graph API Endpoint?

 
If we need to access or get the information across Microsoft 365 cloud applications, we should enlist our application in the App Registration Portal in Azure Portal, since all administrations are coordinated with the Microsoft Identity Platform to give secure sign-in, provide access token for application and approval for their administrations.

To Register App in Azure Portal, Please follow article which is provided by Microsoft Soft.
 

How to generate an access Token to access resources using Microsoft Graph?

 
I am under the understanding that you have registered your app and you have Client ID/ Application ID, Tenant ID, and Secrete key in hand.
  1. End Point  
  2. Get https://login.microsoftonline.com/<Tenent Id>/oauth2/v2.0/token  
  3.   
  4. Body  
  5. grant_type:client_credentials  
  6. client_id: <Application Id> 
  7. client_secret: <Client Secrete> 
  8. scope:https://graph.microsoft.com/.default  
Response as below,
  1. {  
  2.     "token_type""Bearer",  
  3.     "expires_in": 3599,  
  4.     "ext_expires_in": 3599,  
  5.     "access_token""eyJ0eXAiOiJKV1QiLCJub25jZSI6ImwtemtxV05pOUt1NHhhbFJLOWh3ZUNxX2J6UUJzM0J6WTRjbkRTaVctMDQiLCJhbGciOiJSUzI1NiIsIng1dCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1hFZyIsImtpZCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1hFZyJ9.eyJhdWQiOiJodHRwczovL2dyYXBoLm1pY3Jvc29mdC5jb20iLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC8zZDI0ZTM2OS0xMjhhLTQxOWYtYTQ1Ny02NTk3MzgzMzlkZjMvIiwiaWF0IjoxNjE5NzgxMTIwLCJuYmYiOjE2MTk3ODExMjAsImV4cCI6MTYxOTc4NTAyMCwiYWlvIjoiRTJaZ1lNallXQ25QVUhxTVI2clBhb0tRazRnekFBPT0iLCJhcHBfZGlzcGxheW5hbWUiOiJDb250ZW50VHlwZUFwcCIsImFwcGlkIjoiMjQ2NTI5MzQtZDIzMy00ZDQ0LWI1MTEtOTVkYWExMzNmZmJkIiwiYXBwaWRhY3IiOiIxIiwiaWRwIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvM2QyNGUzNjktMTI4YS00MTlmLWE0NTctNjU5NzM4MzM5ZGYzLyIsImlkdHlwIjoiYXBwIiwib2lkIjoiZmQwNWEzYzctMDE3Yi00ZDY3LTg0MGItZjBhOWExODdhZDgzIiwicmgiOiIwLkFYQUFhZU1rUFlvU24wR2tWMldYT0RPZDh6UXBaU1F6MGtSTnRSR1YycUV6Xzcxd0FBQS4iLCJyb2xlcyI6WyJTaXRlcy5SZWFkLkFsbCIsIlNpdGVzLlJlYWRXcml0ZS5BbGwiXSwic3ViIjoiZmQwNWEzYzctMDE3Yi00ZDY3LTg0MGItZjBhOWExODdhZDgzIiwidGVuYW50X3JlZ2lvbl9zY29wZSI6IkFTIiwidGlkIjoiM2QyNGUzNjktMTI4YS00MTlmLWE0NTctNjU5NzM4MzM5ZGYzIiwidXRpIjoiV21mUlBLWWJWRXVLVjdWSWpndjdBUSIsInZlciI6IjEuMCIsInhtc190Y2R0IjoxNjE1NjExMDE0fQ.Djs_iSdUTySvT-kCsFTFbZUbwXcPoAAHhJPzKRV9uy-IavXFAhjJzYpHHPmSqi6qzZbwn-cPVZVs4ics4LrD64ILStblft_1f8r"  
  6. }  
Permission

Permission should be selected in-app which is registered,

Manage Site Content Type using Microsoft Graph Content Type APIs

How to create content Type using Microsoft Graph in SharePoint list?

 
The accompanying model tells the best way to make another conventional content type.

SharePoint Content Type IDs
  1. Post https://graph.microsoft.com/beta/sites/<Site Id>/contentTypes      
  2.       
  3. Header      
  4. {      
  5. "Content-Type":"application/json"      
  6. "Authorization":"Bearer <Access Token>"      
  7. }      
  8.       
  9. Body       
  10. {      
  11.     "name""Document Set for Article",      
  12.     "description""Demo Document Set",      
  13.     "base": {      
  14.         "name""Document Set for article",      
  15.         "id""0x0120D520"      
  16.     },      
  17.     "group""Document Set Content Types"       
  18. }   
Site ID will be your SharePoint site ID.
  1. <TenentName>.sharepoint.com,9f1b3366-1a6b-49d3-920a-2f38c873cb1b,20a80558-c545-4b79-8f75-dba806ea176f
    <Domain>.sharepoint.com,<Site ID>,<Web Id>
Click Here to get Site ID

Response as below,
  1. {  
  2.     "@odata.context""https://graph.microsoft.com/beta/$metadata#sites('dev1802.sharepoint.com%2C9f1b3366-1a6b-49d3-920a-2f38c873cb1b%2C20a80558-c545-4b79-8f75-dba806ea176f')/contentTypes/$entity",  
  3.     "@odata.etag""\"0\"",  
  4.     "id""0x0120D52000AB0EC6EBAFE94441A54312CE6D50FB8A",  
  5.     "isBuiltIn"false,  
  6.     "description""custom docset",  
  7.     "group""Document Set Content Types",  
  8.     "hidden"false,  
  9.     "name""docSet",  
  10.     "parentId""0x0120D520",  
  11.     "readOnly"false,  
  12.     "sealed"false,  
  13.     "documentSet": {  
  14.         "shouldPrefixNameToFile"true,  
  15.         "welcomePageUrl""https://dev1802.sharepoint.com/sites/CodeChallenge/_cts/docSet/docsethomepage.aspx",  
  16.         "allowedContentTypes": [  
  17.             {  
  18.                 "id""0x0101",  
  19.                 "name""Document"  
  20.             }  
  21.         ]  
  22.     },  
  23.     "base": {  
  24.         "id""0x0120D520",  
  25.         "description""Create a document set when you want to manage multiple documents as a single work product.",  
  26.         "group""Document Set Content Types",  
  27.         "hidden"false,  
  28.         "name""Document Set",  
  29.         "readOnly"false,  
  30.         "sealed"false  
  31.     }  
  32. }  
How to delete content type using Microsoft Graph from site ?
  1. DELETE  
  2. https://graph.microsoft.com/beta/sites/dev1802.sharepoint.com,9f1b3366-1a6b-49d3-920a-2f38c873cb1b,20a80558-c545-4b79-8f75-dba806ea176f/contentTypes/<ContentTypeID>  
  3.   
  4. Header        
  5. {        
  6.    "Content-Type":"application/json"        
  7.    "Authorization":"Bearer <Access Token>"        
  8. }    
Response as below,
  1. HTTP/1.1 204 No Content  
How to update existing content type using microsoft graph in site?
  1. Patch   
  2. https://graph.microsoft.com/beta/sites/dev1802.sharepoint.com,9f1b3366-1a6b-49d3-920a-2f38c873cb1b,20a80558-c545-4b79-8f75-dba806ea176f/contentTypes/0x0120D52000E6EC7560131BA84BA3BA471AAB7180B3  
  3.   
  4. Header          
  5. {          
  6.    "Content-Type":"application/json"          
  7.    "Authorization":"Bearer <Access Token>"          
  8. }    
  9.   
  10. Body  
  11. {  
  12.     "name""updatedCt",  
  13.     "documentSet": {  
  14.         "shouldPrefixNameToFile"true,  
  15.         "allowedContentTypes": [{  
  16.             "id""0x0101",  
  17.             "name""Document"  
  18.         }],  
  19.         "defaultContents": [{  
  20.                 "fileName""a.txt",  
  21.                 "contentType": {  
  22.                     "id""0x0101"  
  23.                 }  
  24.             },  
  25.             {  
  26.                 "fileName""b.txt",  
  27.                 "contentType": {  
  28.                     "id""0x0101"  
  29.                 }  
  30.             }  
  31.         ],  
  32.         "sharedColumns": [{  
  33.                 "name""Description",  
  34.                 "id""cbb92da4-fd46-4c7d-af6c-3128c2a5576e"  
  35.             },  
  36.             {  
  37.                 "name""Address",  
  38.                 "id""fc2e188e-ba91-48c9-9dd3-16431afddd50"  
  39.             }  
  40.         ],  
  41.         "welcomePageColumns": [{  
  42.             "name""Address",  
  43.             "id""fc2e188e-ba91-48c9-9dd3-16431afddd50"  
  44.         }]  
  45.     }  
  46. }  
Response as below,
  1. HTTP/1.1 200 OK  
  2. Content-type: application/json  
  3.   
  4. {  
  5.     "id""0x0120D52000E6EC7560131BA84BA3BA471AAB7180B3 ",  
  6.     "description""",  
  7.     "eTag""\"7\"",  
  8.     "group""Custom Content Types",  
  9.     "hidden"false,  
  10.     "name""testdoc",  
  11.     "parentId""0x0101",  
  12.     "base": {  
  13.         "id""0x0101",  
  14.         "name""Document"  
  15.     }  
  16. }  
Note
APIs under the /beta version in Microsoft Graph are subject to change. Use of these APIs in production applications is not supported.
HappyCoding