@sysdoc/sharepoint-utils
Version:
Sysdoc's core Sharepoint utilities
217 lines (149 loc) • 5.88 kB
Markdown
# Sysdoc's Sharepoint Utilities Package
This package contains all of Sysdoc's core Sharepoint utiltiies.
## Versions
| Version | @pnp/sp Support | Branch |
| ------------- |:-------------:| -----:|
| 0.x.x | 1.3.8 | pnp/v1 |
| 1.x.x | ^2.0.0 | Master |
## Included Modules
1. Search Provider
2. Basic REST Provider
3. Utilities
4. JSOM based List Provider
5. Termstore
## Sharepoint Search Provider
TODO - complete this section.
Largest update here has been the inclusion of a callback function to allow custom processing of search results.
When instantiating a new SPSearchProvider you can now pass a resultsTransformer function in to transform your results - this gets passed via callback into the SPSearchResultPageModel and supercedes the makeSPSearchResultEntry function.
```tsx
import { ISearchResult, ISearchResultEntry } from "@sysdoc/sharepoint-utils";
const resultsTransformer = (
result: ISearchResult
): ISearchResultEntry<ISearchResult> => {
try {
return {
href: result.Path,
payload: result,
foo: "Some custom result"
};
} catch (err) {
console.error(err);
return null;
}
};
const searchProvider = new SPSearchProvider({
resultsTransformer: resultsTransformer
});
```
## Sysdoc's Basic REST Provider for Sharepoint
### Notes
- Currently uses @pnp/sp v2.0.1-3 - this needs to be updated to use the stable version of 2.0.1 as soon as it is released.
### Requirements
#### Basic Use
```tsx
import { SPBasicRestProvider } from "@sysdoc/sp-rest-provider";
const provider = new SPBasicRestProvider({
contentTypeId: "<A string value of a Sharepoint ContentTypeId>",
listTitle: "<A string value of a Sharepoint list title>",
webUrl: "<The string value of the Sharepoint WebURL you want to use>" // NB: You can use _spPageContextInfo.siteAbsoluteUrl here
});
```
#### Basic Use - Available Items
Once a provider has been instantiated you have the following fields available on the provider:
```tsx
provider.fields;
```
This returns an array of field names as strings.
```tsx
provider.schema;
```
This returns an object which contains all field names for the list used to instantiate the REST provider. Each field name has an object associated with it which contains various metadata fields i.e:
```tsx
{
schema: {
title: {
odata.type: "SP.FieldText",
odata.id: "https://sysdoc.sharepoint.com/sites/sys-registers-dev/_api/Web/Lists(guid'4c90fdea-b467-4d80-a9b4-9133de1d8586')/Fields(guid'fa564e0f-0c70-4ab9-b863-0177e6ddd247')",
odata.editLink: "Web/Lists(guid'4c90fdea-b467-4d80-a9b4-9133de1d8586')/Fields(guid'fa564e0f-0c70-4ab9-b863-0177e6ddd247')",
EnforceUniqueValues: false,
Group: "Custom Columns",
Hidden: false,
Id: "fa564e0f-0c70-4ab9-b863-0177e6ddd247",
Indexed: false,
InternalName: "Title",
Required: true,
TypeAsString: "Text"
}
}
}
```
#### Basic Use - Internal Methods
##### toItem
TODO: Write explanation of method
##### getSchema
getSchema is called by the constructor when SPBasicRestProvider is instantiated - it uses the listTitle passed in when the SPBasicRestProvider is instantiated to programatically generate a schema of that lists fields. It then makes this available via the schema prop i.e. - taking our example above the schema would be available:
```tsx
const provider = new SPBasicRestProvider({
contentTypeId: "<A string value of a Sharepoint ContentTypeId>",
listTitle: "<A string value of a Sharepoint list title>",
webUrl: "<The string value of the Sharepoint WebURL you want to use>" // NB: You can use _spPageContextInfo.siteAbsoluteUrl here
});
const schema = provider.schema;
```
##### whenReady
Internal method called by getSchema - generally you do not need to use this by itself.
##### createSchemaFromFields
Internal method called by whenReady - generally you do not need to use this by itself.
##### itemToRest
TODO: Write explanation of method
##### prepareObject
TODO: Write explanation of method
##### create
TODO: Write explanation of method
##### update
TODO: Write explanation of method
##### updateBatch
TODO: Write explanation of method
##### delete
TODO: Write explanation of method
##### deleteBatch
TODO: Write explanation of method
##### getAll
TODO: Write explanation of method
##### getByQuery
TODO: Write explanation of method
##### get
TODO: Write explanation of method
#### Advanced Use - Extending
If you want to extend the REST provider you can do so using the following pattern:
```tsx
import { SPBasicRestProvider } from "@sysdoc/sp-rest-provider";
export interface ISPFooProvider {
getBar(userId?: number, limit?: number): Promise<any>;
}
export class SPFooProvider extends SPBasicRestProvider
implements ISPFooProvider {
constructor(cfg: ISPBasicRestProviderConfig) {
super(cfg);
}
async getBar(userId?: number, limit?: number): Promise<any> {
return this.getByQuery(
`FooBarUser eq ${userId || _spPageContextInfo.userId}`,
{
limit: limit || null,
orderBy: {
field: "FooBarDate",
sortAsc: false
}
}
);
}
}
const provider = new SPFooProvider({
contentTypeId: "<A string value of a Sharepoint ContentTypeId>",
listTitle: "<A string value of a Sharepoint list title>",
webUrl: "<The string value of the Sharepoint WebURL you want to use>" // NB: You can use _spPageContextInfo.siteAbsoluteUrl here
});
const baz = provider.getBar();
```
This strategy allows you to access all the available methods from basic use - but also allows you to add additional methods