Skip to content

Commit

Permalink
adding support for addUser and addLookup to fields collection and get…
Browse files Browse the repository at this point in the history
…All to items collection
  • Loading branch information
patrick-rodgers committed Jan 26, 2018
1 parent 090c698 commit 1fb0a55
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 7 deletions.
65 changes: 59 additions & 6 deletions packages/sp/src/fields.ts
Expand Up @@ -6,6 +6,7 @@ import {
FieldTypes,
CalendarType,
UrlFieldFormatType,
FieldUserSelectionMode,
} from "./types";

/**
Expand Down Expand Up @@ -66,12 +67,12 @@ export class Fields extends SharePointQueryableCollection {

const postBody: string = JSON.stringify({
"parameters":
Util.extend({
"__metadata":
{
"type": "SP.XmlSchemaFieldCreationInformation",
},
}, info),
Util.extend({
"__metadata":
{
"type": "SP.XmlSchemaFieldCreationInformation",
},
}, info),
});

return this.clone(Fields, "createfieldasxml").postCore<{ Id: string }>({ body: postBody }).then((data) => {
Expand Down Expand Up @@ -299,6 +300,58 @@ export class Fields extends SharePointQueryableCollection {

return this.add(title, "SP.FieldUrl", Util.extend(props, properties));
}

/** Adds a user field to the colleciton
*
* @param title The new field's title
* @param selectionMode The selection mode of the field
* @param selectionGroup Value that specifies the identifier of the SharePoint group whose members can be selected as values of the field
* @param properties
*/
public addUser(title: string,
selectionMode: FieldUserSelectionMode,
properties?: TypedHash<string | number | boolean>): Promise<FieldAddResult> {

const props = {
FieldTypeKind: 20,
SelectionMode: selectionMode,
};

return this.add(title, "SP.FieldUser", Util.extend(props, properties));
}

/**
* Adds a SP.FieldLookup to the collection
*
* @param title The new field's title
* @param lookupListId The guid id of the list where the source of the lookup is found
* @param lookupFieldName The internal name of the field in the source list
* @param properties Set of additional properties to set on the new field
*/
public addLookup(
title: string,
lookupListId: string,
lookupFieldName: string,
properties?: TypedHash<string | number | boolean>,
): Promise<FieldAddResult> {

const postBody: string = JSON.stringify({
parameters: Util.extend({
FieldTypeKind: 7,
LookupFieldName: lookupFieldName,
LookupListId: lookupListId,
Title: title,
"__metadata": { "type": "SP.FieldCreationInformation" },
}, properties),
});

return this.clone(Fields, "addfield").postCore<{ Id: string }>({ body: postBody }).then((data) => {
return {
data: data,
field: this.getById(data.Id),
};
});
}
}

/**
Expand Down
54 changes: 53 additions & 1 deletion packages/sp/src/items.ts
Expand Up @@ -8,6 +8,7 @@ import { ListItemFormUpdateValue } from "./types";
import { ODataParserBase } from "@pnp/odata";
import { AttachmentFiles } from "./attachmentfiles";
import { List } from "./lists";
import { Logger, LogLevel } from "@pnp/logging";

/**
* Describes a collection of Item objects
Expand Down Expand Up @@ -63,7 +64,58 @@ export class Items extends SharePointQueryableCollection {
return this.get(new PagedItemCollectionParser());
}

//
/**
* Gets all the items in a list, regardless of count. Does not support batching or caching
*
* @param requestSize Number of items to return in each request (Default: 2000)
*/
public getAll(requestSize = 2000): Promise<any[]> {

Logger.write("Calling items.getAll should be done sparingly. Ensure this is the correct choice. If you are unsure, it is not.", LogLevel.Warning);

// this will be used for the actual query
// and we set no metadata here to try and reduce traffic
const items = new Items(this, "").top(requestSize).configure({
headers: {
"Accept": "application/json;odata=nometadata",
},
});

// let's copy over the odata query params that can be applied
// $top - allow setting the page size this way (override what we did above)
// $select - allow picking the return fields (good behavior)
// $filter - allow setting a filter, though this may fail due for large lists
this.query.getKeys()
.filter(k => /^\$select$|^\$filter$|^\$top$/.test(k.toLowerCase()))
.reduce((i, k) => {
i.query.add(k, this.query.get(k));
return i;
}, items);

// give back the promise
return new Promise((resolve, reject) => {

// this will eventually hold the items we return
const itemsCollector: any[] = [];

// action that will gather up our results recursively
const gatherer = (last: PagedItemCollection<any>) => {

// collect that set of results
[].push.apply(itemsCollector, last.results);

// if we have more, repeat - otherwise resolve with the collected items
if (last.hasNext) {
last.getNext().then(gatherer).catch(reject);
} else {
resolve(itemsCollector);
}
};

// start the cycle
items.getPaged().then(gatherer).catch(reject);
});
}

/**
* Adds a new item to the collection
Expand Down
5 changes: 5 additions & 0 deletions packages/sp/src/types.ts
Expand Up @@ -1426,3 +1426,8 @@ export interface MenuNodeCollection {
StartingNodeTitle: string;
Version: Date;
}

export enum FieldUserSelectionMode {
PeopleAndGroups = 1,
PeopleOnly = 0,
}

0 comments on commit 1fb0a55

Please sign in to comment.