Forum Discussion
SharePoint 365 Rest API: Add a link item in a document library
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
8 Replies
- AugustGeierCopper ContributorI 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 - PatrickBeauperinCopper Contributor
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 - Toby StathamBrass Contributor
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
- Carlos_MarinsIron Contributor
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.
- iainmacCopper Contributor
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 = "http://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&"; // LibraryAddLinkURL += "@a2=%27" + resUri + "%27&"; // FileAddLinkURL += "@a3=false"; // overwriteconsole.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
- Carlos_MarinsIron ContributorGreat! Glad to know I could help! I might even save your code for reference.
Don't forget to mark the post as solved.
Regards