Jul 01 2019 04:29 AM
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
Jul 02 2019 10:48 AM
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.
Jul 10 2019 06:40 AM
@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
Jul 10 2019 07:46 AM
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.
Thanks for your help
Iainmac
Jul 10 2019 08:06 AM
Jul 19 2019 05:42 AM
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
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
Jul 19 2019 01:42 PM
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,
Sep 15 2021 08:52 AM
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
Nov 04 2021 06:48 AM