Skip to content

Commit 170f697

Browse files
committed
ListPicker - ability to select lists from a specific web (webAbsoluteUrl property)
1 parent b00e2d6 commit 170f697

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

docs/documentation/docs/controls/ListPicker.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ The `ListPicker` control can be configured with the following properties:
6262
| placeHolder | string | no | Placeholder label to show in the dropdown. **Deprecated. Use `placeholder` instead.** |
6363
| placeholder | string | no | Placeholder label to show in the dropdown. |
6464
| onSelectionChanged | (newValue: string OR string[]): void | no | Callback function when the selected option changes. |
65+
| webAbsoulteUrl | string | no | Absolute Web Url of target site (user requires permissions) |
6566

6667
Enum `LibsOrderBy`
6768

src/controls/listPicker/IListPicker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ export interface IListPickerProps {
5757
* Callback issued when the selected option changes
5858
*/
5959
onSelectionChanged?: (newValue: string | string[]) => void;
60+
/**
61+
* Absolute Web Url of target site (user requires permissions)
62+
*/
63+
webAbsoluteUrl?: string;
6064
}
6165

6266
export interface IListPickerState {

src/controls/listPicker/ListPicker.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export class ListPicker extends React.Component<IListPickerProps, IListPickerSta
5050
if (
5151
prevProps.baseTemplate !== this.props.baseTemplate ||
5252
prevProps.includeHidden !== this.props.includeHidden ||
53-
prevProps.orderBy !== this.props.orderBy
53+
prevProps.orderBy !== this.props.orderBy ||
54+
prevProps.webAbsoluteUrl !== this.props.webAbsoluteUrl
5455
) {
5556
this.loadLists();
5657
}
@@ -64,12 +65,12 @@ export class ListPicker extends React.Component<IListPickerProps, IListPickerSta
6465
* Loads the list from SharePoint current web site
6566
*/
6667
private loadLists() {
67-
const { context, baseTemplate, includeHidden, orderBy, multiSelect, filter } = this.props;
68+
const { context, baseTemplate, includeHidden, orderBy, multiSelect, filter, webAbsoluteUrl } = this.props;
6869

6970
// Show the loading indicator and disable the dropdown
7071
this.setState({ loading: true });
7172

72-
const service: ISPService = SPServiceFactory.createService(context, true, 5000);
73+
const service: ISPService = SPServiceFactory.createService(context, true, 5000, webAbsoluteUrl);
7374
service.getLibs({
7475
baseTemplate: baseTemplate,
7576
includeHidden: includeHidden,

src/services/SPService.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { SPHttpClient, ISPHttpClientOptions } from "@microsoft/sp-http";
66

77
export default class SPService implements ISPService {
88

9-
constructor(private _context: WebPartContext | ExtensionContext) { }
9+
private _webAbsoluteUrl: string;
10+
11+
constructor(private _context: WebPartContext | ExtensionContext, webAbsoluteUrl?: string) {
12+
this._webAbsoluteUrl = webAbsoluteUrl ? webAbsoluteUrl : this._context.pageContext.web.absoluteUrl;
13+
}
1014

1115
/**
1216
* Get lists or libraries
@@ -15,7 +19,7 @@ export default class SPService implements ISPService {
1519
*/
1620
public async getLibs(options?: ILibsOptions): Promise<ISPLists> {
1721
let filtered: boolean;
18-
let queryUrl: string = `${this._context.pageContext.web.absoluteUrl}/_api/web/lists?$select=Title,id,BaseTemplate`;
22+
let queryUrl: string = `${this._webAbsoluteUrl}/_api/web/lists?$select=Title,id,BaseTemplate`;
1923

2024
if (options.orderBy) {
2125
queryUrl += `&$orderby=${options.orderBy === LibsOrderBy.Id ? 'Id' : 'Title'}`;
@@ -52,7 +56,7 @@ export default class SPService implements ISPService {
5256
`substringof('${encodeURIComponent(filterText.replace("'","''"))}',${internalColumnName})${filter ? ' and ' + filter : ''}`
5357
: `startswith(${internalColumnName},'${encodeURIComponent(filterText.replace("'","''"))}')${filter ? ' and ' + filter : ''}`; //string = filterList ? `and ${filterList}` : '';
5458
try {
55-
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
59+
const webAbsoluteUrl = !webUrl ? this._webAbsoluteUrl : webUrl;
5660
const apiUrl = `${webAbsoluteUrl}/_api/web/lists('${listId}')/items?$select=${keyInternalColumnName || 'Id'},${internalColumnName}&$filter=${filterStr}`;
5761
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
5862
if (data.ok) {
@@ -109,7 +113,7 @@ export default class SPService implements ISPService {
109113

110114
try {
111115
const webAbsoluteUrl = !webUrl
112-
? this._context.pageContext.web.absoluteUrl
116+
? this._webAbsoluteUrl
113117
: webUrl;
114118
const apiUrl = `${webAbsoluteUrl}/_api/web/lists('${listId}')/items?$orderby=${internalColumnName}&$select=${keyInternalColumnName ||
115119
"Id"},${internalColumnName}&${_filter}${costumfilter}${_top}`;
@@ -144,7 +148,7 @@ export default class SPService implements ISPService {
144148
*/
145149
public async getListItemAttachments(listId: string, itemId: number, webUrl?: string): Promise<any[]> {
146150
try {
147-
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
151+
const webAbsoluteUrl = !webUrl ? this._webAbsoluteUrl : webUrl;
148152
const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(@itemId)/AttachmentFiles?@listId=guid'${encodeURIComponent(listId)}'&@itemId=${encodeURIComponent(String(itemId))}`;
149153
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
150154
if (data.ok) {
@@ -174,7 +178,7 @@ export default class SPService implements ISPService {
174178
const spOpts: ISPHttpClientOptions = {
175179
headers: { "X-HTTP-Method": 'DELETE', }
176180
};
177-
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
181+
const webAbsoluteUrl = !webUrl ? this._webAbsoluteUrl : webUrl;
178182
const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(@itemId)/AttachmentFiles/getByFileName(@fileName)/RecycleObject?@listId=guid'${encodeURIComponent(listId)}'&@itemId=${encodeURIComponent(String(itemId))}&@fileName='${encodeURIComponent(fileName.replace(/'/g, "''"))}'`;
179183
const data = await this._context.spHttpClient.post(apiUrl, SPHttpClient.configurations.v1, spOpts);
180184
} catch (error) {
@@ -206,7 +210,7 @@ export default class SPService implements ISPService {
206210
const spOpts: ISPHttpClientOptions = {
207211
body: file
208212
};
209-
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
213+
const webAbsoluteUrl = !webUrl ? this._webAbsoluteUrl : webUrl;
210214
const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(@itemId)/AttachmentFiles/add(FileName=@fileName)?@listId=guid'${encodeURIComponent(listId)}'&@itemId=${encodeURIComponent(String(itemId))}&@fileName='${encodeURIComponent(fileName.replace(/'/g, "''"))}'`;
211215
const data = await this._context.spHttpClient.post(apiUrl, SPHttpClient.configurations.v1, spOpts);
212216
return;
@@ -224,7 +228,7 @@ export default class SPService implements ISPService {
224228
* @param webUrl
225229
*/
226230
public async getAttachment(listId: string, itemId: number, fileName: string, webUrl?: string): Promise<any> {
227-
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
231+
const webAbsoluteUrl = !webUrl ? this._webAbsoluteUrl : webUrl;
228232
const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(@itemId)/AttachmentFiles/GetByFileBame(@fileName))?@listId=guid'${encodeURIComponent(listId)}'&@itemId=${encodeURIComponent(String(itemId))}&@fileName='${encodeURIComponent(fileName.replace(/'/g, "''"))}'`;
229233
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
230234
if (data.ok) {
@@ -248,7 +252,7 @@ export default class SPService implements ISPService {
248252
public async checkAttachmentExists(listId: string, itemId: number, fileName: string, webUrl?: string): Promise<any> {
249253
try {
250254
const listServerRelativeUrl = await this.getListServerRelativeUrl(listId, webUrl);
251-
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
255+
const webAbsoluteUrl = !webUrl ? this._webAbsoluteUrl : webUrl;
252256
const fileServerRelativeUrl = `${listServerRelativeUrl}/Attachments/${itemId}/${fileName}`;
253257
const apiUrl = `${webAbsoluteUrl}/_api/web/getfilebyserverrelativeurl(@url)/Exists?@url='${encodeURIComponent(fileServerRelativeUrl.replace(/'/g, "''"))}'`;
254258
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
@@ -272,7 +276,7 @@ export default class SPService implements ISPService {
272276
* @param webUrl
273277
*/
274278
public async getListName(listId: string, webUrl?: string): Promise<string> {
275-
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
279+
const webAbsoluteUrl = !webUrl ? this._webAbsoluteUrl : webUrl;
276280
const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/RootFolder/Name?@listId=guid'${encodeURIComponent(listId)}'`;
277281
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
278282
if (data.ok) {
@@ -292,7 +296,7 @@ export default class SPService implements ISPService {
292296
* @param webUrl
293297
*/
294298
public async getListServerRelativeUrl(listId: string, webUrl?: string): Promise<string> {
295-
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
299+
const webAbsoluteUrl = !webUrl ? this._webAbsoluteUrl : webUrl;
296300
const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/RootFolder/ServerRelativeUrl?@listId=guid'${encodeURIComponent(listId)}'`;
297301
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
298302
if (data.ok) {

src/services/SPServiceFactory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import SPServiceMock from "./SPServiceMock";
66
import SPService from "./SPService";
77

88
export class SPServiceFactory {
9-
public static createService(context: WebPartContext | ExtensionContext, includeDelay?: boolean, delayTimeout?: number): ISPService {
9+
public static createService(context: WebPartContext | ExtensionContext, includeDelay?: boolean, delayTimeout?: number, webAbsoluteUrl?: string): ISPService {
1010
if (Environment.type === EnvironmentType.Local) {
1111
return new SPServiceMock(includeDelay, delayTimeout);
1212
}
13-
return new SPService(context);
13+
return new SPService(context, webAbsoluteUrl);
1414
}
1515
}

0 commit comments

Comments
 (0)