Skip to content

Commit

Permalink
Updated the sites v2 lib methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunjandatta committed Dec 6, 2024
1 parent 1925c64 commit c3e8a6d
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 30 deletions.
32 changes: 27 additions & 5 deletions @types/v2/sites.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ import { ITargetInfoProps } from "../utils";
*/
export const sites: Isites;

export interface IsiteGetDriveProps {
listName: string;
siteId?: string;
siteUrl?: string;
webId?: string;
}

export interface IsiteProps {
listName?: string;
siteId?: string;
siteUrl?: string;
webId?: string;
}

export interface IsiteIdResult {
digestValue: string;
siteId: string;
siteUrl: string;
webId: string;
webUrl: string;
}

/**
* Sites
* The v2.1 REST endpoint.
Expand All @@ -40,14 +62,14 @@ export interface Isites {
static getCurrentWeb(): IBaseQuery<site> & siteMethods;

/** Returns a drive for a site. */
static getDrive(props: { siteId?: string, siteUrl?: string, libName?: string, webId?: string }): PromiseLike<IBaseQuery<drive> & driveMethods>;
static getDrive(props: IsiteGetDriveProps): PromiseLike<IBaseQuery<drive> & driveMethods>;

/** Returns a drive for a site. */
static getFile(props: { fileUrl: string, siteId?: string, siteUrl?: string, libName?: string }): PromiseLike<IBaseQuery<item> & itemMethods>;
static getFile(props: IsiteProps & { fileUrl: 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>;
static getList(props: IsiteProps & { listId?: string }): PromiseLike<IBaseQuery<list> & listMethods>;

/** Returns the site and web ids. */
static getIdByUrl(siteUrl: string): PromiseLike<{ siteId: string, siteUrl: string, webId: string, webUrl: string }>;
/** Returns the url to the site, web, list or file. */
static getIdByUrl(url: string): PromiseLike<IsiteIdResult>;
}
32 changes: 27 additions & 5 deletions dist/gd-sprest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4536,6 +4536,28 @@ declare module 'gd-sprest/v2/sites' {
*/
export const sites: Isites;

export interface IsiteGetDriveProps {
listName: string;
siteId?: string;
siteUrl?: string;
webId?: string;
}

export interface IsiteProps {
listName?: string;
siteId?: string;
siteUrl?: string;
webId?: string;
}

export interface IsiteIdResult {
digestValue: string;
siteId: string;
siteUrl: string;
webId: string;
webUrl: string;
}

/**
* Sites
* The v2.1 REST endpoint.
Expand All @@ -4554,16 +4576,16 @@ declare module 'gd-sprest/v2/sites' {
static getCurrentWeb(): IBaseQuery<site> & siteMethods;

/** Returns a drive for a site. */
static getDrive(props: { siteId?: string, siteUrl?: string, libName?: string, webId?: string }): PromiseLike<IBaseQuery<drive> & driveMethods>;
static getDrive(props: IsiteGetDriveProps): PromiseLike<IBaseQuery<drive> & driveMethods>;

/** Returns a drive for a site. */
static getFile(props: { fileUrl: string, siteId?: string, siteUrl?: string, libName?: string }): PromiseLike<IBaseQuery<item> & itemMethods>;
static getFile(props: IsiteProps & { fileUrl: 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>;
static getList(props: IsiteProps & { listId?: string }): PromiseLike<IBaseQuery<list> & listMethods>;

/** Returns the site and web ids. */
static getIdByUrl(siteUrl: string): PromiseLike<{ siteId: string, siteUrl: string, webId: string, webUrl: string }>;
/** Returns the url to the site, web, list or file. */
static getIdByUrl(url: string): PromiseLike<IsiteIdResult>;
}
}

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.4",
"version": "8.5.5",
"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.54,
__ver: 8.55,
AppContext: (siteUrl: string) => { return Lib.Site.getAppContext(siteUrl); },
Apps: Lib.Apps,
ContextInfo: Lib.ContextInfo,
Expand Down
55 changes: 39 additions & 16 deletions src/v2/sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const sites: Isites = ((props: { siteId?: string, webId?: string, targetI
sites.getCurrentWeb = () => { return sites().sites(ContextInfo.webId.replace(/^\{|\}$/g, '')); }

/** Returns a drive */
sites.getDrive = (props: { siteId?: string, siteUrl?: string, libName?: string, webId?: string }) => {
sites.getDrive = (props) => {
// Return a promise
return new Promise((resolve, reject) => {
// Method to get the drive id
Expand Down Expand Up @@ -68,15 +68,15 @@ sites.getDrive = (props: { siteId?: string, siteUrl?: string, libName?: string,
// See if the site id exists
if (props.siteId) {
// Get the drive id
getDriveId(props.siteId, props.webId, props.libName).then(driveId => {
getDriveId(props.siteId, props.webId, props.listName).then(driveId => {
// Resolve the request
resolve(sites({ siteId: props.siteId }).drives(driveId));
}, reject);
} else {
// Get the site/web ids
sites.getIdByUrl(props.siteUrl).then(info => {
// Get the drive id
getDriveId(info.siteId, info.webId, props.libName).then(driveId => {
getDriveId(info.siteId, info.webId, props.listName).then(driveId => {
// Resolve the request
resolve(sites({ siteId: info.siteId, webId: info.webId }).drives(driveId));
}, reject);
Expand All @@ -86,25 +86,47 @@ sites.getDrive = (props: { siteId?: string, siteUrl?: string, libName?: string,
}

/** Returns a file */
sites.getFile = (props: { fileUrl: string, siteId?: string, siteUrl?: string, libName?: string }) => {
sites.getFile = (props) => {
// Return a promise
return new Promise((resolve, reject) => {
// Get the site/web info
sites.getIdByUrl(props.fileUrl).then(info => {
// Get the drive
sites.getDrive({ siteId: info.siteId, webId: info.webId, libName: props.libName }).then(drive => {
// Get the file
Web(info.webUrl).getFileByUrl(props.fileUrl).execute(file => {
// Resolve the request
resolve(drive.items(file.UniqueId));
});
}, reject);
// Get the file
Web(info.webUrl, { requestDigest: info.digestValue }).getFileByUrl(props.fileUrl).execute(file => {
// See if the library was provided
if (props.listName) {
// Get the drive
sites.getDrive({
listName: props.listName,
siteId: info.siteId,
siteUrl: info.siteUrl,
webId: info.webId
}).then(drive => {
// Resolve the request
resolve(drive.items(file.UniqueId));
}, reject);
} else {
// Get the list information
file.ListItemAllFields().query({ Expand: ["ParentList"], Select: ["ParentList/Title"] }).execute(item => {
// Get the drive
sites.getDrive({
listName: item.ParentList.Title,
siteId: info.siteId,
siteUrl: info.siteUrl,
webId: info.webId
}).then(drive => {
// Resolve the request
resolve(drive.items(file.UniqueId));
}, reject);
})
}
});
}, reject);
});
}

/** Returns a list */
sites.getList = ((props: { siteId?: string, siteUrl?: string, listId?: string, listName?: string }) => {
sites.getList = (props) => {
// Return a promise
return new Promise((resolve, reject) => {
// See if the site id exists
Expand All @@ -119,20 +141,21 @@ sites.getList = ((props: { siteId?: string, siteUrl?: string, listId?: string, l
}, reject);
}
});
}) as any;
}

/** Gets the site and web id from a url */
sites.getIdByUrl = (siteUrl: string) => {
sites.getIdByUrl = (url) => {
// Return a promise
return new Promise((resolve, reject) => {
// Get the context information
ContextInfo.getWeb(siteUrl).execute(context => {
ContextInfo.getWeb(url).execute(context => {
// Get the site id
Site(context.GetContextWebInformation.SiteFullUrl).query({ Select: ["Id"] }).execute(site => {
// Get the web id
Web(context.GetContextWebInformation.WebFullUrl).query({ Select: ["Id"] }).execute(web => {
// Resolve the request
resolve({
digestValue: context.GetContextWebInformation.FormDigestValue,
siteId: site.Id,
siteUrl: context.GetContextWebInformation.SiteFullUrl,
webId: web.Id,
Expand Down
6 changes: 6 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ v2.drives().execute(drives => {
drives.results[0].id;
})

v2.sites.getList({}).then(l => {
l.items(3).update({

})
})

v2.sites.getCurrentWeb();
v2.sites.getList("Test").items().execute(items => {
items.results[0].fields();
Expand Down

0 comments on commit c3e8a6d

Please sign in to comment.