# SharePoint Attachment Controller for SPA Addins

This library includes a component and service which we can use for actions related sharepoint list item attachments

  - sp-attachment(component)
  - SpAttachmentLibraryService(service)

---

# How to install and use

```bash
npm install sp-attachment
```
  - Import the module:
  **Import the module:**
  
```typescript
//...
import { SpAttachmentLibraryModule, SpAttachmentConfig, SpAttachmentLibraryService } from 'sp-attachment-library';

const spAttachmentconfig: SpAttachmentConfig = {
// here I have used sp-rest-proxy to test it locally for dev mode
  baseUrl: isDevMode() ? 'http://localhost:8181' : (window as any)._spPageContextInfo.siteAbsoluteUrl,
};

@NgModule({
  //...
  imports: [
    //...
    SpAttachmentLibraryModule,
    SpAttachmentLibraryModule.forRoot(spAttachmentconfig)
  ],
   providers: [
   //...
   SpAttachmentLibraryService
   ],
  //...
})
export class AppModule { }
```
  - *Example Use*
  
```html
<sp-attachment [listName]="'TestList'" [itemId]='1'></sp-attachment>
```

 - Service Use
 
```typescript
//...
import { SpAttachmentLibraryService } from 'sp-attachment-library';

export class SampleComponent {

    constructor(
        private spAttachmentLibraryService: SpAttachmentLibraryService) {
    }

    loadAttachments() {
        this.spAttachmentLibraryService.getAttachmentsById(this.listName, this.itemId).then((data) => {
            // implement logic here
        }).catch((error) => {
            // handle error here
        });
    }
}
```

---
### Functions

* getAttachmentsById(listName: string, itemId: number)
* addAttachment(listName: string, itemId: number, attachment: AttachmentFileInfo)
* addAttachments(listName: string, itemId: number, attachments: AttachmentFileInfo[])
* deleteAttachment(listName: string, itemId: number, fileName: string)
* deleteAttachments(listName: string, itemId: number, fileNames: string[])
* getAttachmentContent(listName: string, itemId: number, fileName: string)
* updateAttachmentContent(listName: string, itemId: number, fileName: string, content: string | Blob | ArrayBuffer)
---
### Demo
![](./demo/sp-attachment-demo.gif)

---

### ⚠ Note

This library needs some modification. 

### Todos

 - Write MORE Tests
 - Add better styling
