npm package: @jm-7c3/sp-integration
Description: Contains elements to work with SharePoint through its API.
Latest version: 20.1.0

# SharePoint Integration

Contains elements to work with SharePoint through its API.

Now the major version matches the Angular version used in the project.

## Form Controls

Standalone UI form input controls, based on [PrimeNG](https://primeng.org) components, to be used in Reactive Forms.

### Autocomplete Input

Autocomplete input that shows suggestions from a specified list.

#### Import

```
import {SpAutocompleteInputComponent} from '@jm-7c3/sp-integration';
```

#### Implementation

```
<cl-sp-autocomplete-input listName="fields" />
```

#### Inputs

| Input | Type | Default | Required | Description |
| - | - | - | - | - |
| appendTo | string | 'body' | false | HTML element to append the control to |
| fieldName | string | 'Title' | false | List field to be used as label |
| forceSelection | boolean | true | false | - |
| listName | string | | true | List from which the options will be taken |
| maxlength | number | 25 | false | Max length for the input |
| minLength | number | 3 | false | Min length for the input |
| placeholder | string | '' | false | Placeholder for the input |

### Site Users Input

Input that shows users as suggestions.

#### Import

```
import {SpSiteusersInputComponent} from '@jm-7c3/sp-integration';
```

#### Implementation

```
<cl-sp-siteusers-input />
```

#### Inputs

| Input | Type | Default | Required | Description |
| - | - | - | - | - |
| appendTo | string | 'body' | false | HTML element to append the control to |
| fieldName | string | 'Title' | false | List field to be used as label |
| fieldValue | string | 'Id' | false | List field to be used as value |
| forceSelection | boolean | true | false | - |
| maxlength | number | 25 | false | Max length for the input |
| minLength | number | 3 | false | Min length for the input |
| placeholder | string | '' | false | Placeholder for the input |
| styleClass | string | '' | false | CSS classes to be added to the input |

## Interceptors

### Form Digest

Takes the Form Digest token from local storage and set the `X-RequestDigest` header to POST requests.

#### Implementation

```
import { spFormDigestInterceptor } from '@jm-7c3/sp-integration';

const appConfig: ApplicationConfig = {
  providers: [
    ...
    provideHttpClient(
      withInterceptors([spFormDigestInterceptor])
    ),
    ...
  ]
}
```

### Integration

Sets headers such as `Accept` and `Content-Type` to DELETE, GET and POST calls.

#### Implementation

```
import { spIntegrationInterceptor } from '@jm-7c3/sp-integration';

const appConfig: ApplicationConfig = {
  providers: [
    ...
    provideHttpClient(
      withInterceptors([spIntegrationInterceptor])
    ),
    ...
  ]
}
```

## Services

All services internally generate paths to work with [SharePoint's API](https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/get-to-know-the-sharepoint-rest-service).

### Files

Allows working with SharePoint's files and directories.

#### Import

```
import {SpFilesService} from '@jm-7c3/sp-integration';
```

#### Inject the service

```
private readonly spFilesService = inject(SpFilesService);
```

#### Methods

| Method | HTTP Method | Description |
| - | - | - |
| createDirectory | POST | Creates a directory in the specified path |
| downloadFile | NA | Opens a new tab to download the specified |
| getFileName | NA | Returns the name (with hash) of the specified file |
| getSafeFileName | NA | Returns the name (without hash) of the specified file |
| uploadFile | POST | Uploads a file to the specified path |

### Form Digest

Requests Form Digest token for authentication every 300s and saves it in local storage. It's only used by the Form Digest interceptor.

#### Implementation

It is suggested implementing the service's clear method in the root component's onDestroy hook to avoid memory leaks.

```
import {SpFormDigestService} from '@jm-7c3/sp-integration';

class AppComponent implements OnDestroy {
  private readonly spFormDigestService = inject(SpFormDigestService);

  ngOnDestroy(): void {
    this.spFormDigestService.clear();
  }
}
```

### Lists

Allows working with SharePoint's lists.

#### Import

```
import {SpListsService} from '@jm-7c3/sp-integration';
```

#### Inject the service

```
private readonly spListsService = inject(SpListsService);
```

#### Methods

| Method | HTTP Method | Description |
| - | - | - |
| getListItem | GET | Returns a single list item from a list |
| getListItemHistory | GET | Returns the version history of a list item |
| query | GET | Returns an array of filtered list items of a list |
| list | GET | Returns an array of all list items of a list |
| save | POST | Saves a single list item |

### Users

Allows working with SharePoint's users.

#### Import

```
import {SpUsersService} from '@jm-7c3/sp-integration';
```

#### Inject the service.

```
private readonly spUsersService = inject(SpUsersService);
```

#### Methods

| Method | HTTP Method | Description |
| - | - | - |
| getCurrentUser | GET | Returns the information of the current user |
| getCurrentUserProfile | GET | Returns the profile of the current user |
| getUser | GET | Returns the information of a specific user |
| siteUsers | GET | Returns an array of filtered users |