The CreatorCon Call for Content is officially open! Get started here.

Integrate ServiceNow with SharePoint to get File Info from ShareLink / SharePoint URL/link - Post

Community Alums
Not applicable

Hi Everyone,

In this post I am going to tell you the process to get the file info from Sharepoint with the help of sharelink or sharepoint url using GraphAPI

 

Setting up the integration-

First thing first- we need to ensure we have a user in sharepoint who has access to the Site that we are going to crawl to get the file info.

Step 1- Getting the auth Token

Here we are using POST method to get the auth details, in my case its Bearer token i need to authenticate sharepoint

Endpoint for Bearer token= "https://login.microsoftonline.com/xxxxxxxxxxxxxxxxxxxxxxxxxxx/oauth2/v2.0/token"
  method := "POST"

Body will contain the below info:
grant_type=password
client_id=XXXXXXXXXXXXXXXXXXXXXXXXXXX
client_secret=xxxxxxxxxxxxxxxxxxxxxxxxxxx
scope=https://graph.microsoft.com/.default
username=xxxxxxxxxxxxxxxxxxxxxxxxxxx
password=xxxxxxxxxxxxxxxxxxxxxxxxxxx

Response-

 "token_type": "Bearer",
    "scope": "profile openid email https://graph.microsoft.com/Directory.AccessAsUser.All https://graph.microsoft.com/Files.Read https://graph.microsoft.com/Files.Read.All https://graph.microsoft.com/Group.Read.All https://graph.microsoft.com/Sites.Read.All https://graph.microsoft.com/User.Read https://graph.microsoft.com/User.ReadBasic.All https://graph.microsoft.com/.default",
    "expires_in": 4144,
    "ext_expires_in": 4144,
    "access_token": "eyJ0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

 

Once you have the response we need to use the bearer token to the file info by hitting the endpoint using graphAPI Step 3.

Before doing that we need to encode the ShareLink / sharepoint Link - Source- Sharepoint Client API: Get file name from sharing link 

 

Step 2: Encoding the Share URL-

First thing to understand is the encoding the URL, below is the code to generate the same:

 

var shareLink = "https://tenant.sharepoint.com/:u:/s/Site/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx--Q?e=6TY7g0";
var base64Value = GlideStringUtil.base64Encode(shareLink);
var encodedUrl = "u!" + base64Value.split('=').join('').split('/').join('_').split('+').join('-');
gs.print(encodedUrl);

Result:
u!aHR0cHM6Ly9iY2djbG91ZC5zaGFyZXBvaW50LmNvbxxxxxxxxxxxxxxxxxxxxxxxwSWlZSF9VUkR5TWFAtLVE_ZT02VFk3ZzA

 

One you have the encodedurl use the below endpoint to get the file info:

Endpoint:

Step 3:  GET method to get the File details

Endpoint := "https://graph.microsoft.com/v1.0/shares/u!aHR0cHM6Ly9iY2djbG91ZC5zaGFyZXBvaW50LmNvbxxxxxxxxxxxxxxxxxxxxxxxwSWlZSF9VUkR5TWFAtLVE_ZT02VFk3ZzA/driveItem"
  method := "GET"
"Authorization", "Bearer eyJxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

Note: /driveItem - it is used to get all the info of the file i.e., Name, modification date, created date, webUrl, etc.,

 

Below is the sample response-

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem)/$entity",
    "@microsoft.graph.downloadUrl": "https://tenant.sharepoint.com/sites/Site/_layouts/15/download.aspx?UniqueId=xxxxxxxxxxxxxxxxxxxxxxx&Translate=false&tempauth=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&ApiVersion=2.0",
    "@microsoft.graph.Decorator": "decorator has been deprecated. Refer to folder.decorator",
    "createdBy": {
        "user": {
            "email": "xxxxxxxxxx",
            "displayName": "xxxxxxxxxx"
        }
    },
    "createdDateTime": "2024-03-22T00:03:11Z",
    "eTag": "\"{xxxxxxxxxx},8\"",
    "id": "xxxxxxxxxx",
    "lastModifiedBy": {
        "user": {
            "email": "xxxxxxxxxx",
            "displayName": "xxxxxxxxxx"
        }
    },
    "lastModifiedDateTime": "2024-03-22T00:02:47Z",
    "name": "abc.pdf",
    "parentReference": {
        "driveType": "documentLibrary",
        "driveId": "b!xxxxxxxxxx",
        "id": "xxxxxxxxxx",
        "name": "xxxxxxxxxx",
        "path": "/drives/b!xxxxxxxxxx/root:/xxxxxxxxxx",
        "siteId": "xxxxxxxxxx"
    },
    "webUrl": "https://tenant.sharepoint.com/sites/Site/xxxxxxxxxx/abc.pdf",
    "cTag": "\"c:{xxxxxxxxxx},2\"",
    "file": {
        "hashes": {
            "quickXorHash": "xxxxxxxxxx="
        },
        "mimeType": "application/octet-stream"
    },
    "fileSystemInfo": {
        "createdDateTime": "2024-03-22T00:03:11Z",
        "lastModifiedDateTime": "2024-03-22T00:02:47Z"
    },
    "shared": {
        "scope": "users"
    },
    "size": xxxxxxxxxx
}

Hope it helps in setting up the integration and resolving your issue.

Note: Here we are using Microsoft Graph API to get the file details 

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar Gupta

1 REPLY 1

Meet Mewada
Tera Expert

Thank you very much for this post. This is exactly what i was looking for. Well drafted and explained! 

Thanks again!