@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.
Files changed (3) hide show
  1. package/LICENSE.md +10 -10
  2. package/README.md +116 -129
  3. 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
- sharepoint-api
2
- ====
3
-
4
- [![NPM](https://nodei.co/npm/@jimmybjorklund/sharepoint-api.png)](https://nodei.co/npm/@jimmybjorklund/sharepoint-api/)
5
-
6
- ## Introduction
7
- This client makes it easier to upload/download and list files on your Microsoft SharePoint account.
8
-
9
- It uses the Microsoft Graph api to handle the communication and you need to create an
10
- client access token for this api for this library to work.
11
-
12
- ## Installation
13
-
14
- ```
15
- $ npm install @jimmybjorklund/sharepoint-client
16
- ```
17
-
18
- ## Create access.
19
-
20
- ### 1. First we need to create a app that is allowed to access the files.
21
-
22
- Navigate to: https://entra.microsoft.com/
23
-
24
- Once you have logged in navigate in the menu.
25
-
26
- Application->App registrations.
27
-
28
- On this page select
29
-
30
- + New registration.
31
-
32
- Give the integration app a good name and select single tenant and press Register.
33
-
34
- You should now be presented with the Application overview, on this page search for
35
- Application (client) ID <b>save this id for later as clientId.</b>
36
-
37
- On this page you should also be able to find the Directory (tenant) ID, <b>save this as
38
- tenantId for later.</b>
39
-
40
-
41
- ### 2. Create a client secret this is done by navigating to Certificates & secrets under you app view.
42
-
43
- On this page click
44
-
45
- + New client secret
46
-
47
- Enter a good description and max lifetime of you tokens and press Add.
48
-
49
- In the list of secrets you should now see the Value of your secret key, <b>save this into
50
- a variable called clientSecret.</b> NOTE: Do this directly as it will only be visible once.
51
- If you fail to do this you can remove it an recreated it to se a new value.
52
-
53
- ### 3. Setup Api permissions by going to API permissions.
54
-
55
- On this page click.
56
-
57
- + Add a permission
58
-
59
- You should select the api that say <b>Microsoft Graph.</b>
60
- You will be presented with two options delegated access or application.
61
- <b>Select application permissions.</b>
62
-
63
- You need to select:
64
-
65
- Directory.ReadWrite.All
66
- Files.ReadWrite.All
67
-
68
- ### Congratulations
69
- You should now have a clientId and secret that will give you access.
70
-
71
-
72
-
73
- ## Sample
74
- This is a simple example of how to use the Microsoft Graph API to upload a file to a SharePoint site.
75
-
76
- ```ts
77
- //This is the name of the tenant in SharePoint, normally first part of the url.
78
- //sample: myName.sharepoint.com
79
- const tenantName: "myName";
80
- // From App Overview page.
81
- const tenantId: "00000000-0000-0000-0000-000000000000";
82
-
83
- // This is the name of the site (group in teams) in SharePoint.
84
- const siteName: "MyGroup";
85
-
86
- // This is the id and secret of the app you created in Azure AD.
87
- const clientId: "00000000-0000-0000-0000-000000000000";
88
- const clientSecret: "0000000000000000000000000000000000000000";
89
-
90
- const client = new SharepointApi({
91
- tenantId,
92
- tenantName,
93
- siteName,
94
- clientId,
95
- clientSecret
96
- });
97
- // Get a Azure AD login token.
98
- const token = await client.login();
99
- if (token === undefined) {
100
- console.log("Error getting token");
101
- return;
102
- }
103
-
104
- // Fetch the site to get the siteId.
105
- const site = await azure.getSite(token);
106
- if (site === undefined) {
107
- console.log("Error getting site");
108
- return;
109
- }
110
- const siteId = client.getSiteId(site);
111
- const driveName = "Dokument"; // Folder name in SharePoint.
112
-
113
- // Get the driver to find the correct driverId.
114
- const driver = await client.getDrive(token, siteId, driveName);
115
- if (driver === undefined) {
116
- console.log("Error getting drive");
117
- return;
118
- }
119
-
120
- const path = "/TestUploadFiles";
121
- const fileName = "test.txt";
122
- // Upload files.
123
- const res = await client.upload(token, driver.id, path, fileName, "text/plain");
124
- console.log("Upload response", res);
125
- ```
126
-
127
- ## LICENSE
128
-
129
- MIT, see LICENSE.md file.
1
+ # SharePoint API Client
2
+
3
+ [![NPM](https://img.shields.io/npm/v/@jimmybjorklund/sharepoint-api)](https://www.npmjs.com/package/@jimmybjorklund/sharepoint-api)
4
+ [![License](https://img.shields.io/npm/l/@jimmybjorklund/sharepoint-api)](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.2",
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.6.8",
17
- "qs": "^6.11.2"
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.9.11",
22
- "prettier": "^3.2.5",
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
  },