Hi @Vijay Sutaria ,
Thanks for reaching out to Microsoft Q&A.
You have to enable identity and assign sites.selected API permission to the function app's managed identity.
Enable identity in Function app -> Add sites.selected API permission to the function app's identity -> Grant permission to site using https://dori-uw-1.kuma-moon.com/en-us/graph/api/site-post-permissions?view=graph-rest-1.0&tabs=http… -> generate token for managed identity -> using token call SharePoint.
Use the below script to fetch the objectID:
Connect-MgGraph -Scopes "Application.Read.All"
$clientId = "ManagedIdentityClientID" #Function App's managed identity Client ID
$mi = Get-MgServicePrincipal -Filter "appId eq '$clientId'"
$miObjectId = $mi.Id
$miObjectId
- To assign sites.selected permission to the managed identity
# Connect with required permissions
Connect-MgGraph -Scopes "Application.Read.All","RoleManagement.ReadWrite.Directory","AppRoleAssignment.ReadWrite.All"
# Managed Identity Object ID
$miObjectId = "ManagedIdentityObjectID"
# Microsoft Graph Service Principal
$graphSp = Get-MgServicePrincipal -Filter "appId eq '00000003-0000-0000-c000-000000000000'"
# Don't change the value of appId
# App Role ID you want to assign
$appRoleId = "883ea226-0bf2-4a8f-9f9d-92c9162a727d" # Don't change this value
# Assign app role
New-MgServicePrincipalAppRoleAssignment `
-ServicePrincipalId $miObjectId `
-PrincipalId $miObjectId `
-ResourceId $graphSp.Id `
-AppRoleId $appRoleId
Hope it helps!
Please do not forget to click "Accept the answer” and Yes, this can be beneficial to other community members.
If you have any other questions, let me know in the "comments" and I would be happy to help you.