checkout tutorials:
- Video
- Blog
This package allows you to get the authenticated Azure app to CRUD Sharepoint by Microsoft Graph API.
The project is built on top of Azure auth example
NOTE
This library is only available on Nodejs environment. Not for browser.
How to use
Register a Azure client app. Check the tutorial here
npm install ms365-graph-api-auth
// or
yarn add ms365-graph-api-auth
Import relevant modules to get started
CRUD LIST in Sharepoint
require("dotenv").config();
import { getAccessToken, GraphApiQuery } from "ms365-graph-api-auth";
const tenantId = process.env.TENANT_ID;
const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
(async () => {
try {
const authResponse = await getAccessToken(
clientId!,
clientSecret!,
tenantId!
);
if (!authResponse) return;
const query = new GraphApiQuery(authResponse.accessToken);
console.log("1. GET SHAREPOINT SITES ***");
const mySite = await query.getSites("<your-sharepoint-site-name>");
console.log({
sites: mySite.value.map((site) => ({
name: site.name,
displayName: site.displayName,
})),
});
console.log("\n\n\n2. GET LISTS IN CERTAIN SHAREPOINT SITE ***");
const myList = await query.getListsInSite(
mySite.value[0].id,
"Work progress tracker"
);
console.log(
myList.value.map((list) => ({
name: list.name,
displayName: list.displayName,
}))
);
console.log("\n\n\n3. GET ITEMS IN LIST ***");
const items = await query.getItemsInList(
mySite.value[0].id,
myList.value[0].id
);
const itemInfos = items.value.map((item) => item.fields);
console.log(itemInfos.map((info) => info?.["Title"]));
console.log(items);
console.log("\n\n\n4.1. CREATE NEW ITEM IN LIST ***");
const newItem = {
Title: `My new item create at ${Date.now()}`,
Description: `Created by ms365-graph-api test`,
};
const createdItem = await query.postCreateListItem(
mySite.value[0].id,
myList.value[0].id,
newItem
);
console.log(createdItem);
console.log("\n\n\n4.2 UPDATE ITEM IN LIST ***");
const data = {
Notes: `Updated by ms365-graph-api test`,
};
const updatedItem = await query.patchUpdateListItem(
mySite.value[0].id,
myList.value[0].id,
createdItem.id,
data
);
console.log(updatedItem);
console.log("\n\n\n4.3 DELETE ITEM IN LIST ***");
} catch (error) {
console.log(error);
}
})();
CRUD DRIVE files in Sharepoint
require("dotenv").config();
import { getAccessToken, GraphApiQuery } from "../src";
import fs from "fs";
const tenantId = process.env.TENANT_ID;
const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
(async () => {
try {
const authResponse = await getAccessToken(
clientId!,
clientSecret!,
tenantId!
);
if (!authResponse) return;
const query = new GraphApiQuery(authResponse.accessToken);
console.log("1. GET SHAREPOINT SITES ***");
const mySite = await query.getSites(process.env.SITE_NAME);
console.log({
sites: mySite.value.map((site) => ({
name: site.name,
displayName: site.displayName,
})),
});
console.log("\n\n\n2. GET DRIVES IN CERTAIN SHAREPOINT SITE ***");
const myDrives = await query.getDrives(
mySite.value[0].id,
process.env.DRIVE_NAME
);
console.log(myDrives);
console.log("\n\n\n3.1. GET ALL ITEMS IN CERTAIN DRIVE ***");
const myItems = await query.getDriveItems(
mySite.value[0].id,
myDrives.value[0].id
);
console.log(myItems);
console.log("\n\n\n3.2. GET CERTAIN ITEM IN DRIVE BY FILE NAME ***");
const myItem1 = await query.getDriveItemByFileName(
mySite.value[0].id,
myDrives.value[0].id,
"TainanView2.jpg"
);
console.log(myItem1);
console.log("\n\n\n3.3. GET CERTAIN ITEM IN DRIVE BY FILE ID ***");
const myItem2 = await query.getDriveItemById(
mySite.value[0].id,
myDrives.value[0].id,
myItems.value[0].id
);
console.log(myItem2);
console.log("\n\n\n4. UPLOAD DRIVE ITEM ***");
const filePath = process.env.UPLOAD_FILE_PATH;
const fileData = fs.readFileSync(filePath!);
const createdItem = await query.putUploadDriveItem(
mySite.value[0].id,
myDrives.value[0].id,
`file${Date.now()}.jpg`,
fileData,
`folder${Date.now()}/`
);
console.log(createdItem);
console.log("\n\n\n5. DELETE DRIVE ITEM ***");
const res = await query.deleteDriveItem(
mySite.value[0].id,
myDrives.value[0].id,
myItems.value[0].id
);
console.log(res);
} catch (error) {
console.log(error);
}
})();
Release note
Checkout the github page
Any contribution will be welcome
TODO
- Personal List & Drive (Currently only support Sharepoint)