Try to get sharedlink sharepoint in python

BiYO 1 Reputation point
2023-01-10T15:05:02.343+00:00

Hi All,

Actually i write a python script to get shared link from my file.

    url = "https://group.sharepoint.com/sites/team/_api/web/GetFolderByServerRelativeUrl('/sites/team/Shared Documents/test_api/')/files('work.xlsx')"
    headers = {
        'Accept': 'application/json;odata=verbose',
        'APIKEY': 'efezfezJADtf2xkfezfe5wnb1I3CMK7Rnx',
        'Authorization': 'Bearer ' + get_token + '',
    }

    request = requests.request("GET", url=url, headers=headers)
    print(request.json()['d']['LinkingUrl'])

This part of code give me the "LinkingUrl" but user didn't have access to this file.

I would like to get shared link for someone who don't have existing access.

Thank you very much in advance !

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
1,698 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 14,546 Reputation points Microsoft Employee
    2023-01-11T05:40:47.6133333+00:00

    Hi @BiYO

    I will recommend you to use Office365-REST-Python-Client to share files with anonymous users. Please refer to following code

    import json
    
    from office365.sharepoint.client_context import ClientContext
    from office365.sharepoint.sharing.links.kind import SharingLinkKind
    from office365.sharepoint.webs.web import Web
    from tests import test_user_credentials, test_team_site_url
    
    ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
    
    sharing_messages = {
        0: "A value has not been initialized",
        1: "A direct link or canonical URL to an object",
        2: "An organization access link with view permissions to an object",
        3: "An organization access link with edit permissions to an object",
        4: "An anonymous access link with view permissions to an object",
        5: "An anonymous access link with edit permissions to an object",
        6: "A tokenized sharing link where properties can change without affecting link URL"
    }
    
    file_url = "/sites/team/Shared Documents/SharePoint User Guide.docx"
    target_file = ctx.web.get_file_by_server_relative_url(file_url)
    
    # Share a file link
    result = target_file.share_link(SharingLinkKind.AnonymousView).execute_query()
    print(json.dumps(result.value.to_json(), indent=4))
    link_url = result.value.sharingLinkInfo.Url
    
    # Verify a link
    result = Web.get_sharing_link_kind(ctx, link_url).execute_query()
    print(sharing_messages.get(result.value, "Unknown sharing link"))
    
    # Unshare a file link
    target_file.unshare_link(SharingLinkKind.AnonymousView).execute_query()
    
    # Get a file sharing info
    info = target_file.get_sharing_information().execute_query()
    print("AnonymousViewLink:", info.properties.get('AnonymousViewLink'))
    

    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.