Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for getFileByUrl method on web, docs, and test #1140 #1142

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion docs/sp/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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";

Expand Down
12 changes: 12 additions & 0 deletions packages/sp/files/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
}
}

Expand All @@ -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)}')`);
};
9 changes: 8 additions & 1 deletion test/sp/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down Expand Up @@ -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;
});
}
});