Sharepoint + Graph API: Create Share Link doesn't work

Jürgen Drachta 25 Reputation points
2024-01-09T22:05:47.86+00:00

I try to create a share link of a directory on sharepoint via the Graph API, but I always get an "The operation failed because the object does not support sharing." error.

What I did

  • Registered an app, according to the docs.
  • Get a token without a user involved.
  • Granted the app Files.ReadWrite.All, Sites.ReadWrite.All , Directory.ReadWriteAll permissions.
  • Ran the following cURL
curl \
--request POST 'https://graph.microsoft.com/v1.0/sites/{site-id}/drive/items/{item-id}/createLink' \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--data '{"type": "edit", "recipients": [{"email": "******@gmail.com"}], "sendNotification": true}'

but I receive the following error

{"error":{"code":"notSupported","message":"The operation failed because the object does not support sharing.","innerError":{"code":"objectNotSupported","date":"2024-01-09T21:57:21","request-id":"xxxx-xxxx-xxxx","client-request-id":"xxxxx-xxx-xxxx-xxx"}}}

Via the UI creating share links work nicely without any problems, this is what puzzles me.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,562 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,229 questions
0 comments No comments
{count} votes

Accepted answer
  1. Diah.Karim-MSFT 335 Reputation points Microsoft External Staff
    2024-01-10T02:59:49.09+00:00

    Hi Jurgen,

    Maybe you can follow below steps to simulate creating the share link

    Steps:
    1. Get the drive Id
    Use this endpoint https://graph.microsoft.com/v1.0/sites/1cpzfr.sharepoint.com,f0f575b8-513a-4d29-aced-944665acb461,60b419f0-5b77-4e7e-bb8b-d0c29288316b/drives
    
    2. Copy the the drive id (for e.g id : b!uHX18DpRKU2s7ZRGZay0YfAZtGB3W35Ou4vQwpKIMWs3eEZky_dsSKJQhHoBQ80i) from the response object
    Then request another call from this endpoint https://graph.microsoft.com/v1.0/sites/1cpzfr.sharepoint.com,f0f575b8-513a-4d29-aced-944665acb461,60b419f0-5b77-4e7e-bb8b-d0c29288316b/drives/b!uHX18DpRKU2s7ZRGZay0YfAZtGB3W35Ou4vQwpKIMWs3eEZky_dsSKJQhHoBQ80i/items?$filter=id ne ' ' 
    This to get either folder id or files id, you can see it from response object
    
    3. Copy the id (for my case i'm copying folder id : 01GJ2FQ63XXLW55ZYBN5BLL2VMNIEA5UWM)
    
    4. Now request a create link endpoint 
    https://graph.microsoft.com/v1.0/sites/1cpzfr.sharepoint.com,f0f575b8-513a-4d29-aced-944665acb461,60b419f0-5b77-4e7e-bb8b-d0c29288316b/drives/b!uHX18DpRKU2s7ZRGZay0YfAZtGB3W35Ou4vQwpKIMWs3eEZky_dsSKJQhHoBQ80i/items/01GJ2FQ63XXLW55ZYBN5BLL2VMNIEA5UWM/createLink
    with Http Method POST and below payload or request body. Btw u can remove the password attribute to open share without entering password
    {
      "type": "view",
      "scope": "anonymous",
      "retainInheritedPermissions": false
    }
    

    Here is the expected response. The webUrl is the shared link

    ShareLink

    Hope this helps.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,451 Reputation points Microsoft External Staff
    2024-01-10T03:01:07+00:00

    Hi @Jürgen Drachta

    As the message said, the object does not support sharing. Currently the graph api only support us create sharelink for items. I feel regretful to inform you that it turns out to be a by-design one. You could refer to the following document

    https://learn.microsoft.com/en-us/graph/api/driveitem-createlink?view=graph-rest-1.0&tabs=http#http-request

    It is highly recommended that you can vote this ticket. Many features of our current products are designed and upgraded based on customers’ feedback. With requirements like this increase, the problem may well be released in the future. Thanks for your understanding.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Jürgen Drachta 25 Reputation points
    2024-01-10T12:06:33.69+00:00

    Thanks for your answers!

    WIth help of your answers, I could solve my problem!

    The key was the following:

    • I needed to use the following request `https://graph.microsoft.com/beta/sites/<site-id>/drives/<drive-id>/items/<item-id>/createLink, where
    • <item-id> is found out via https://graph.microsoft.com/v1.0/sites/<site-id>/drives/<drive-id>/root /children and not via https://graph.microsoft.com/v1.0/sites/<site-d>/lists/<list-id>/items/.

    Then everything works as expected, eventhough sendNotification=true seems to be a bit buggy, as it doesn't send out mails. But this is something I can live with ;-)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.