From 3f885ecdb305019ddb5d7336d714ce80848f5616 Mon Sep 17 00:00:00 2001 From: Patrick Rodgers Date: Fri, 3 Apr 2020 15:15:57 -0400 Subject: [PATCH] adding support for getFileByUrl method on web, docs, and test #1140 --- docs/sp/files.md | 22 +++++++++++++++++++++- packages/sp/files/web.ts | 12 ++++++++++++ test/sp/files.ts | 9 ++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/docs/sp/files.md b/docs/sp/files.md index d17f88e02..32530a1b6 100644 --- a/docs/sp/files.md +++ b/docs/sp/files.md @@ -24,6 +24,26 @@ const text: string = await sp.web.getFileByServerRelativeUrl("/sites/dev/documen const text2: string = await sp.web.getFolderByServerRelativeUrl("/sites/dev/documents").files.getByName("file.txt").getText(); ``` +### getFileByUrl + +_Added in 2.0.4_ + +This method supports opening files form sharing links or absolute urls. The file must reside in the through which you are trying to open the file. + +```TypeScript +import { sp } from "@pnp/sp"; +import "@pnp/sp/webs"; +import "@pnp/sp/files/web"; + +const url = "{absolute file url OR sharing url}"; + +// file is an IFile and supports all the file operations +const file = sp.web.getFileByUrl(url); + +// for example +const fileContent = await file.getText(); +``` + ## Adding Files Likewise you can add files using one of two methods, add or addChunked. The second is appropriate for larger files, generally larger than 10 MB but this may differ based on your bandwidth/latency so you can adjust the code to use the chunked method. The below example shows getting the file object from an input and uploading it to SharePoint, choosing the upload method based on file size. @@ -38,7 +58,7 @@ import "@pnp/sp/webs"; import "@pnp/sp/files"; import "@pnp/sp/folders"; import { auth } from "./auth"; -let $ = require("jquery"); //<-- used here for illustration +let $ = require("jquery"); // <-- used here for illustration let siteUrl = "https://mytenant.sharepoint.com/sites/dev"; diff --git a/packages/sp/files/web.ts b/packages/sp/files/web.ts index 531f6482d..f3e55b874 100644 --- a/packages/sp/files/web.ts +++ b/packages/sp/files/web.ts @@ -7,6 +7,7 @@ declare module "../webs/types" { getFileByServerRelativeUrl(fileRelativeUrl: string): IFile; getFileByServerRelativePath(fileRelativeUrl: string): IFile; getFileById(uniqueId: string): IFile; + getFileByUrl(fileUrl: string): IFile; } interface IWeb { @@ -30,6 +31,13 @@ declare module "../webs/types" { * @param uniqueId The UniqueId of the file */ getFileById(uniqueId: string): IFile; + + /** + * Gets a file from a sharing link or absolute url + * + * @param fileUrl Absolute url of the file to get + */ + getFileByUrl(fileUrl: string): IFile; } } @@ -44,3 +52,7 @@ _Web.prototype.getFileByServerRelativePath = function (this: _Web, fileRelativeU _Web.prototype.getFileById = function (this: _Web, uniqueId: string): IFile { return File(this, `getFileById('${uniqueId}')`); }; + +_Web.prototype.getFileByUrl = function (this: _Web, fileUrl: string): IFile { + return File(this, `getFileByUrl('!@p1::${escapeQueryStrValue(fileUrl)}')`); +}; diff --git a/test/sp/files.ts b/test/sp/files.ts index 26a4a364f..0c1fbd3a5 100644 --- a/test/sp/files.ts +++ b/test/sp/files.ts @@ -40,6 +40,13 @@ describe("files", () => { return expect(sp.web.getFileByServerRelativePath(testFileNamePercentPoundServerRelPath)()).to.eventually.be.fulfilled; }); + it("getByUrl", async function () { + + const item = await sp.web.getFileByServerRelativePath(testFileNamePercentPoundServerRelPath).getItem(); + const urlData = await item.select("EncodedAbsUrl")(); + return expect(sp.web.getFileByUrl(urlData.EncodedAbsUrl)()).to.eventually.be.fulfilled; + }); + it("add", async function () { const name = `Testing Add - ${getRandomString(4)}.txt`; @@ -252,7 +259,7 @@ describe("file", () => { const name = `Testing setContent - ${getRandomString(4)}.txt`; await files.add(name, "Some test text content."); const item = await files.getByName(name).getItem(); - return expect(item()).to.eventually.be.fulfilled; + return expect(item()).to.eventually.be.fulfilled; }); } });