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

New Contributor



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




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: 



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 ""(oops, typo) which is the folder 'pasta' of the Shared Documents library.


If you need any help, let me know.




@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 = "" + ".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

            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..!!");
            error: function (data) {
                console.log("API Error");



Thanks for your help





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


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


Only is saved to the library.

When manually adding ''  with the new link optionl


This is the request URL for AddUsingPath


Request URL:


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




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











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 "" to the library, when you click on it the link "" 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.



Hi @iainmac,

Found a way to do that in REST.

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


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

Body (in plain text as is shown below)



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:{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):