SharePoint 365 Rest API: Add a link item in a document library

Copper Contributor

Hi

 

I am looking into creating a new link item in a document library using rest api. I have looked around and can not find a solution as yet.

 

Has anyone managed to achieve this?

 

Many thanks

 

Iain

 

New Link item.PNG

 

 

8 Replies

Hey @iainmac,

 

That's a good question. I was looking through the PnP REST library and it doesn't seem like there is a way to do it from there. Maybe because Links in document libraries are not commonly used.

One thing I noticed, though, is if you open the Dev tools(F12 on Chrome) and watch the Network tab, when you add a link to a library, looks like this is the REST call adding it: 

https://<tenant>.sharepoint.com/sites/<siteName>/_api/web/GetFolderByServerRelativeUrl(@a1)/Files/AddUsingPath(decodedUrl=@a2,overwrite=@a3)?@a1="<libraryRelativePath>"&@a2="<linkName>"&@a3=false

 

This is a POST request, and the payload is "Url=<fileLink>". Keep in mind that the libraryPath in the URL is relative, starting with "/sites/...", while the fileLink is absolute, starting with "https://..."

 

I didn't test it manually, but you can try using AJAX to see it the call works. I attached a print of the call in Chrome Dev Tools, blurring some sensitive info. As you can notice in the payload, the link is to the file "myDoc.docs"(oops, typo) which is the folder 'pasta' of the Shared Documents library.

 

If you need any help, let me know.

addlInk.png

 

 

@iainmac You need to upload a text file with an extension of .url and it contents as follows 

 

[InternetShortcut]URL = https://yourlink

 

and then set the the files corresponding List Item "_ShortcutUrl" field to a FieldUrlValue of the link and the filename.

 

Have done this with CSOM, so should be able to do something similar through the REST API

Thanks @Carlos_Marins 

 

I have managed to build on your reply to create a successful url link in a document library

 

Here is the Js code I used, not the cleanest of code - but it works for me.

 

function AddLink() {

 

    var Uri = "www.google.com" + ".url";
    var library = "/documents"

 

    var resUri      = encodeURIComponent(Uri).replace(/\./g, '%2E');
    var reslibrary  = encodeURIComponent(library).replace(/\./g, '%2E');

 

    var AddLinkURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl(@a1)/Files/AddUsingPath"
 
    AddLinkURL +=  "(decodedUrl=@a2,overwrite=@a3)?";
    AddLinkURL += "@a1=%27" + reslibrary + "%27&";      // Library
    AddLinkURL += "@a2=%27" + resUri + "%27&";          // File
    AddLinkURL += "@a3=false";                          // overwrite
 
    console.log(AddLinkURL);



    $.ajax
        ({
            url: AddLinkURL,
            type: "POST",
            processData: false,
            async: false,
            headers: {
                "accept": "application/json;odata=verbose",
                "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
            },
            success: function (data) {
                console.log("Item Inserted..!!");
                console.log(data);
            },
            error: function (data) {
                console.log("API Error");
                console.log(data);
            }
        });
 

 

}


Thanks for your help

 

Iainmac

 

 

Great! Glad to know I could help! I might even save your code for reference.
Don't forget to mark the post as solved.

Regards

So on further testing it appears this only works for short urls with just the domain - for example

 

https://www.google.com/ works

https://www.google.com/maps does not work

 

Only www.google.com.url is saved to the library.

When manually adding 'https://www.google.com/maps'  with the new link optionl

 

This is the request URL for AddUsingPath

 

Request URL:
https://[removed].sharepoint.com/_api/web/GetFolderByServerRelativeUrl(@a1)/Files/AddUsingPath(decodedUrl=@a2,overwrite=@a3)?@a1=%27Cases%27&@a2=%27www%2Egoogle%2Ecom%2Eurl%27&@a3=false

 

There is a second request for SP.Utilities.ShortcutLink.GetShortcutLink

 

https://[removed]..sharepoint.com/_api/SP.Utilities.ShortcutLink.GetShortcutLink(@a1)?@a1=%Cases%2Fwww%2Egoogle%2Ecom%2Eurl%27

 

Which looks like a query for the full url

 

I think I need to include the 'request payload' with the first url, but not sure how to do this via Rest api.

 

Any help will be appreciated

 

Thanks

 

Iainmac

 

 

 

 

 

 

Hi @iainmac ,

 

I am not sure I understand the issue here. So, are you saying that if you try to add a link where the URL is "https://www.google.com/maps" to the library, when you click on it the link "https://www.google.com" is opened? I have done some testing(through the User Interface) and that is not happening to me. The title of the link in the library is just the domian, but if you click it will take you the full URL.

 

Let me know if I got it wrong so I can help.

 

Regards,

Hi @iainmac,

Found a way to do that in REST.

POST https://<tenant>.sharepoint.com/sites/<yoursite>/_api/web/GetFolderByServerRelativeUrl('TestLib')/Files/add(url='LinktoFolder.url',overwrite=true)

Headers

{
"content-type": "text/plain",
"accept": "application/json"
}

Body (in plain text as is shown below)

[InternetShortcut]
URL=https://<tenant>.sharepoint.com/sites/<anysite>/linkToFile

 

I was able to do this via the MS Graph API. For those that this may help:

MS Graph API version: 1.0
Method: PUT
URL: https://graph.microsoft.com/v1.0/sites/{site_id}/drive/root:/name of link that can contain spaces.url:/content
Request Headers: content-type text/plain
Request Body (separate lines are important here):
[InternetShortcut]
URL=https://some.website.com/path/more_path