Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit d2e1499

Browse files
author
Andrew Koltyakov
committed
Attachment files add and delete basic implementation
1 parent 25a0821 commit d2e1499

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"use strict";
2+
3+
import { Queryable, QueryableCollection, QueryableInstance } from "./queryable";
4+
import { QueryableSecurable } from "./queryablesecurable";
5+
import { TypedHash } from "../../collections/collections";
6+
import { Util } from "../../utils/util";
7+
import * as Types from "./types";
8+
import { ODataParserBase } from "./odata";
9+
10+
/**
11+
* Describes a collection of Item objects
12+
*
13+
*/
14+
export class AttachmentFiles extends QueryableCollection {
15+
16+
/**
17+
* Creates a new instance of the AttachmentFiles class
18+
*
19+
* @param baseUrl The url or Queryable which forms the parent of this attachments collection
20+
*/
21+
constructor(baseUrl: string | Queryable, path = "AttachmentFiles") {
22+
super(baseUrl, path);
23+
}
24+
25+
/**
26+
* Gets a Attachment File by filename
27+
*
28+
* @param name The name of the file, including extension.
29+
*/
30+
public getByName(name: string): AttachmentFile {
31+
let f = new AttachmentFile(this);
32+
f.concat(`(FileName='${name}')`);
33+
return f;
34+
}
35+
36+
/**
37+
* Adds a new attachment to the collection
38+
*
39+
* @param name The name of the file, including extension.
40+
* @param content The Base64 file content.
41+
*/
42+
public add(name: string, content: string): Promise<AttachmentFileAddResult> {
43+
return new AttachmentFiles(this, `add(FileName='${name}')`)
44+
.post({
45+
// binaryStringRequestBody: true,
46+
body: content
47+
}).then((response) => {
48+
return {
49+
data: response,
50+
file: this.getByName(name),
51+
};
52+
});
53+
}
54+
}
55+
56+
/**
57+
* Descrines a single attachment file instance
58+
*
59+
*/
60+
export class AttachmentFile extends QueryableSecurable {
61+
62+
/**
63+
* Creates a new instance of the AttachmentFile class
64+
*
65+
* @param baseUrl The url or Queryable which forms the parent of this attachment file
66+
*/
67+
constructor(baseUrl: string | Queryable, path?: string) {
68+
super(baseUrl, path);
69+
}
70+
71+
/**
72+
* Delete this attachment file
73+
*
74+
* @param eTag Value used in the IF-Match header, by default "*"
75+
*/
76+
public delete(eTag = "*"): Promise<void> {
77+
return this.post({
78+
headers: {
79+
"IF-Match": eTag,
80+
"X-HTTP-Method": "DELETE",
81+
},
82+
});
83+
}
84+
}
85+
86+
export interface AttachmentFileAddResult {
87+
file: AttachmentFile;
88+
data: any;
89+
}

src/sharepoint/rest/items.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { TypedHash } from "../../collections/collections";
88
import { Util } from "../../utils/util";
99
import * as Types from "./types";
1010
import { ODataParserBase } from "./odata";
11+
import { AttachmentFiles } from "./attachmentfiles";
1112

1213
/**
1314
* Describes a collection of Item objects
@@ -104,7 +105,7 @@ export class Item extends QueryableSecurable {
104105
*
105106
*/
106107
public get attachmentFiles(): QueryableCollection {
107-
return new QueryableCollection(this, "AttachmentFiles");
108+
return new AttachmentFiles(this, "AttachmentFiles");
108109
}
109110

110111
/**

0 commit comments

Comments
 (0)