Skip to content

Commit

Permalink
Updated the getFile method for the v2 sites lib to be fileUrl not fil…
Browse files Browse the repository at this point in the history
…eName.
  • Loading branch information
gunjandatta committed Dec 4, 2024
1 parent b49a0f6 commit 6266800
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion @types/v2/sites.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface Isites {
static getDrive(props: { siteId?: string, siteUrl?: string, libName?: string }): PromiseLike<IBaseQuery<drive> & driveMethods>;

/** Returns a drive for a site. */
static getFile(props: { fileName: string, siteId?: string, siteUrl?: string, libName?: string }): PromiseLike<IBaseQuery<item> & itemMethods>;
static getFile(props: { fileUrl: string, siteId?: string, siteUrl?: string, libName?: string }): PromiseLike<IBaseQuery<item> & itemMethods>;

/** Returns a list for a site. */
static getList(props: { siteId?: string, siteUrl?: string, listId?: string, listName?: string }): PromiseLike<IBaseQuery<list> & listMethods>;
Expand Down
2 changes: 1 addition & 1 deletion dist/gd-sprest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4556,7 +4556,7 @@ declare module 'gd-sprest/v2/sites' {
static getDrive(props: { siteId?: string, siteUrl?: string, libName?: string }): PromiseLike<IBaseQuery<drive> & driveMethods>;

/** Returns a drive for a site. */
static getFile(props: { fileName: string, siteId?: string, siteUrl?: string, libName?: string }): PromiseLike<IBaseQuery<item> & itemMethods>;
static getFile(props: { fileUrl: string, siteId?: string, siteUrl?: string, libName?: string }): PromiseLike<IBaseQuery<item> & itemMethods>;

/** Returns a list for a site. */
static getList(props: { siteId?: string, siteUrl?: string, listId?: string, listName?: string }): PromiseLike<IBaseQuery<list> & listMethods>;
Expand Down
2 changes: 1 addition & 1 deletion dist/gd-sprest.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/gd-sprest.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gd-sprest",
"version": "8.5.1",
"version": "8.5.2",
"description": "An easy way to develop against the SharePoint REST API.",
"author": "Gunjan Datta <[email protected]> (https://gunjandatta.github.io)",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IREST } from "../@types";
* SharePoint REST Library
*/
export const $REST: IREST = {
__ver: 8.51,
__ver: 8.52,
AppContext: (siteUrl: string) => { return Lib.Site.getAppContext(siteUrl); },
Apps: Lib.Apps,
ContextInfo: Lib.ContextInfo,
Expand Down
40 changes: 34 additions & 6 deletions src/v2/sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ITargetInfoProps } from "../../@types/utils";
import { Isites } from "../../@types/v2";
import { ContextInfo } from "../lib/contextInfo";
import { Site } from "../lib/site";
import { Web } from "../lib/web";
import { Base, Request, RequestType } from "../utils";
import { init } from "./common";

Expand Down Expand Up @@ -79,17 +80,44 @@ sites.getDrive = (props: { siteId?: string, siteUrl?: string, libName?: string }
}

/** Returns a file */
sites.getFile = (props: { fileName: string, siteId?: string, siteUrl?: string, libName?: string }) => {
sites.getFile = (props: { fileUrl: string, siteId?: string, siteUrl?: string, libName?: string }) => {
let getWebUrl = (): PromiseLike<string> => {
return new Promise((resolve, reject) => {
// See if the site id exists
if (props.siteId) {
// Get the url for this site
Site.getUrlById(props.siteId).execute(siteInfo => {
// Resolve the request
resolve(siteInfo.GetUrlById);
});
} else {
// Resolve the request
resolve(props.siteUrl);
}
});
}

// Return a promise
return new Promise((resolve, reject) => {
// Get the library
sites.getDrive(props).then(drive => {
// Get the file
drive.items().query({ Filter: "name eq '" + props.fileName + "'" }).execute(resp => {
let file = resp.results[0];
// Get the web url
getWebUrl().then(webUrl => {
// Get the file
Web(webUrl).getFileByUrl(props.fileUrl).execute(file => {
// Get the item for this file
file.ListItemAllFields().query({ Expand: ["ParentList"] }).execute(item => {
drive.items().query({
Expand: ["listItem"],
Filter: "listItem/id eq '" + item.Id + "'"
}).execute(resp => {
let file = resp.results[0];

// Resolve the request
resolve(file ? drive.items(file.id) : null);
// Resolve the request
resolve(file ? drive.items(file.id) : null);
}, reject);
});
});
}, reject);
}, reject);
});
Expand Down

0 comments on commit 6266800

Please sign in to comment.