@jimmybjorklund/sharepoint-api 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +10 -10
- package/README.md +116 -129
- package/package.json +5 -5
package/LICENSE.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
=====================
|
|
3
|
-
|
|
4
|
-
Copyright (c) 2024 sharepoint-client contributors
|
|
5
|
-
----------------------------------------------
|
|
6
|
-
|
|
7
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
8
|
-
|
|
9
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
10
|
-
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
=====================
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2024 sharepoint-client contributors
|
|
5
|
+
----------------------------------------------
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
8
|
+
|
|
9
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
10
|
+
|
|
11
11
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,129 +1,116 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
[](https://www.npmjs.com/package/@jimmybjorklund/sharepoint-api)
|
|
4
|
+
[](LICENSE.md)
|
|
5
|
+
|
|
6
|
+
A lightweight Node.js client for interacting with Microsoft SharePoint via the Microsoft Graph API. This library simplifies file operations such as uploading, downloading, and listing files.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @jimmybjorklund/sharepoint-api
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Prerequisites
|
|
15
|
+
|
|
16
|
+
To use this library, you need to register an application in Microsoft Entra ID (formerly Azure AD) to obtain credentials.
|
|
17
|
+
|
|
18
|
+
### 1. Register an Application
|
|
19
|
+
1. Go to the [Microsoft Entra admin center](https://entra.microsoft.com/).
|
|
20
|
+
2. Navigate to **Applications** > **App registrations**.
|
|
21
|
+
3. Click **+ New registration**.
|
|
22
|
+
4. Enter a name for your app (e.g., "SharePoint Integration"), select **Single tenant**, and click **Register**.
|
|
23
|
+
|
|
24
|
+
### 2. Get Credentials
|
|
25
|
+
After registration, you will be taken to the application overview page.
|
|
26
|
+
- **Client ID**: Copy the `Application (client) ID`.
|
|
27
|
+
- **Tenant ID**: Copy the `Directory (tenant) ID`.
|
|
28
|
+
|
|
29
|
+
### 3. Create a Client Secret
|
|
30
|
+
1. In the app menu, go to **Certificates & secrets**.
|
|
31
|
+
2. Click **+ New client secret**.
|
|
32
|
+
3. Enter a description and expiration, then click **Add**.
|
|
33
|
+
4. **Important**: Copy the `Value` immediately. This is your `clientSecret`.
|
|
34
|
+
|
|
35
|
+
### 4. Configure API Permissions
|
|
36
|
+
1. In the app menu, go to **API permissions**.
|
|
37
|
+
2. Click **+ Add a permission** > **Microsoft Graph** > **Application permissions**.
|
|
38
|
+
3. Select and add the following permissions:
|
|
39
|
+
- `Directory.ReadWrite.All`
|
|
40
|
+
- `Files.ReadWrite.All`
|
|
41
|
+
4. Click **Grant admin consent** for your organization.
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
Here is a complete example of how to upload a file to a SharePoint site.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { SharepointApi } from '@jimmybjorklund/sharepoint-api';
|
|
49
|
+
import * as fs from 'fs';
|
|
50
|
+
|
|
51
|
+
async function main() {
|
|
52
|
+
// Configuration
|
|
53
|
+
const config = {
|
|
54
|
+
tenantName: "your-tenant-name", // e.g. "mycompany" from mycompany.sharepoint.com
|
|
55
|
+
tenantId: "00000000-0000-0000-0000-000000000000",
|
|
56
|
+
siteName: "MySharePointSite",
|
|
57
|
+
clientId: "00000000-0000-0000-0000-000000000000",
|
|
58
|
+
clientSecret: "your-client-secret-value"
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const client = new SharepointApi(config);
|
|
62
|
+
|
|
63
|
+
// 1. Authenticate
|
|
64
|
+
const token = await client.login();
|
|
65
|
+
if (!token) {
|
|
66
|
+
console.error("Failed to authenticate.");
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 2. Get the Site ID using the site name
|
|
71
|
+
const site = await client.getSite(token);
|
|
72
|
+
if (!site) {
|
|
73
|
+
console.error("Failed to get site information.");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const siteId = client.getSiteId(site);
|
|
77
|
+
|
|
78
|
+
// 3. Get the Drive (Document Library)
|
|
79
|
+
// 'Documents' is the default document library name in many locales,
|
|
80
|
+
// but it might be 'Dokument' or 'Shared Documents' depending on your setup.
|
|
81
|
+
const driveName = "Documents";
|
|
82
|
+
const drive = await client.getDrive(token, siteId, driveName);
|
|
83
|
+
if (!drive) {
|
|
84
|
+
console.error(`Failed to find drive with name: ${driveName}`);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 4. Upload a File
|
|
89
|
+
const remotePath = "/UploadedFiles"; // Folder path in SharePoint
|
|
90
|
+
const fileName = "example.txt";
|
|
91
|
+
const fileContent = Buffer.from("Hello SharePoint!", "utf-8"); // Or read from fs: fs.readFileSync('./local.txt')
|
|
92
|
+
|
|
93
|
+
console.log(`Uploading ${fileName}...`);
|
|
94
|
+
const uploadedItem = await client.upload(
|
|
95
|
+
token,
|
|
96
|
+
drive.id,
|
|
97
|
+
remotePath,
|
|
98
|
+
fileName,
|
|
99
|
+
"text/plain",
|
|
100
|
+
fileContent
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
if (uploadedItem) {
|
|
104
|
+
console.log("Upload successful:", uploadedItem.webUrl);
|
|
105
|
+
} else {
|
|
106
|
+
console.log("Upload failed.");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
main();
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT. See [LICENSE.md](LICENSE.md) for details.
|
|
116
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jimmybjorklund/sharepoint-api",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Lets you easily access and update files on sharepoint",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.js",
|
|
@@ -13,13 +13,13 @@
|
|
|
13
13
|
"author": "Jimmy Björklund",
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"axios": "^1.
|
|
17
|
-
"qs": "^6.
|
|
16
|
+
"axios": "^1.13.2",
|
|
17
|
+
"qs": "^6.14.1"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@types/node": "^20.11.16",
|
|
21
|
-
"@types/qs": "^6.
|
|
22
|
-
"prettier": "^3.
|
|
21
|
+
"@types/qs": "^6.14.0",
|
|
22
|
+
"prettier": "^3.7.4",
|
|
23
23
|
"ts-node": "^10.9.1",
|
|
24
24
|
"typescript": "^4.9.4"
|
|
25
25
|
},
|