Skip to content

Commit

Permalink
Merge pull request #2932 from juliemturner/version-4
Browse files Browse the repository at this point in the history
Graph Files Updates - Still a couple failing tests
  • Loading branch information
juliemturner authored Feb 9, 2024
2 parents 265146e + 137a26a commit 625c8c2
Show file tree
Hide file tree
Showing 15 changed files with 782 additions and 288 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- ISite now supports `async rebase()` to ensure any ISite is represented by the url pattern /sites/{site id} regardless of how it was first loaded
- ISites.getAllSites()
- support for operations for ISite and IList
- completed support for Files

- sp
- explict error thrown if SPFx context is null or undefined when needed
Expand All @@ -26,6 +27,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- graph
- paged method removed from IGraphQueryableCollection
- ./operations.ts methods moved to ./graphqueryable.ts
- deprecated DriveItem move method.
- deprecated DriveItem setContent method.

- sp
- getPaged method removed from _Items/IItems
Expand Down
88 changes: 80 additions & 8 deletions debug/launch/graph.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,92 @@
import { Logger, LogLevel } from "@pnp/logging";
import { graphSetup } from "./setup.js";
import "@pnp/graph/users";
import "@pnp/graph/groups";
import "@pnp/graph/sites";
import "@pnp/graph/files";
import { IDriveItemAdd, IDriveItemAddFolder } from "@pnp/graph/files";
import * as fs from "fs";
import { IResumableUploadOptions } from "@pnp/graph/files";
import { graphPut } from "@pnp/graph";
import { InjectHeaders } from "@pnp/queryable/index.js";
import { DriveItemUploadableProperties } from "@microsoft/microsoft-graph-types";
import { ISensitivityLabel } from "@pnp/graph/files";

declare var process: { exit(code?: number): void };

export async function Example(settings: any) {
const userId = "julie@sympjulie.onmicrosoft.com";
const graph = graphSetup(settings);

const graph = graphSetup(settings);
const folderInfo: IDriveItemAddFolder = {
name: "Sub Folder",
conflictBehavior: "replace",
};

const users = await graph.users();
const fileInfo: IDriveItemAdd = {
filename: "Test File.txt",
content: "Contents of test file",
contentType: "text/plain",
conflictBehavior: "replace",
};

Logger.log({
data: users,
level: LogLevel.Info,
message: "List of Users Data",
});
//const users = await graph.users.getById(userId).drive.root.children.addFolder(folderInfo);
//const folder = await graph.users.getById(userId).drive.getItemByPath("/Test Folder")();
//const folder = await graph.users.getById(userId).drive.root.getItemByPath("/Test Folder")();

process.exit(0);
//const file = await graph.users.getById(userId).drive.root.children.add(fileInfo);

// const moveItem = {
// parentReference: {
// id: folder.id,
// },
// name: "Moved File.txt",
// }
// const move = await graph.users.getById(userId).drive.getItemById(file.id).moveItem(moveItem);
//const thumbnails = await graph.users.getById(userId).drive.getItemById(folder.id).thumbnails();
//const versions = await graph.users.getById(userId).drive.getItemById(folder.id).versions();
//const users = await graph.sites.getById(settings.testing.graph.id).drive.list();

const fileBuff = fs.readFileSync("C:\\Users\\jturner.BMA\\Desktop\\TestDocument.docx");

const fileUploadOptions: IResumableUploadOptions<DriveItemUploadableProperties> = {
item: {
name: "TestDocument2.docx",
fileSize: fileBuff.byteLength,
},
};

const label: ISensitivityLabel = {
sensitivityLabelId: "b7a3c3d5-7b6d-4e6c-8e0c-3f5c7b1d0e3d",
assignmentMethod: "standard",
justificationText: "Just because",
};

const driveRoot = await graph.sites.getById(settings.testing.graph.id).drive.root();
const driveItems = await graph.sites.getById(settings.testing.graph.id).drive.root.children();
const driveItem = await graph.sites.getById(settings.testing.graph.id).drive.getItemById(driveItems[1].id)();
const retentionLabelStatusUrl = await graph.sites.getById(settings.testing.graph.id).drive.getItemById(driveItems[1].id).assignSensitivityLabel(label);
//const retentionLabel = await graph.users.getById(userId).drive.getItemById(driveItems[0].id).extractSensitivityLabels();
const uploadSession = await graph.users.getById(userId).drive.getItemById(driveRoot.id).createUploadSession(fileUploadOptions);
const status = await uploadSession.resumableUpload.status();

const upload = await uploadSession.resumableUpload.upload(fileBuff.length, fileBuff);

// Upload a chunk of the file to the upload session
// Using a fragment size that doesn't divide evenly by 320 KiB results in errors committing some files.
const chunkSize = 327680;
let startFrom = 0;
while (startFrom < fileBuff.length) {
const fileChunk = fileBuff.slice(startFrom, startFrom + chunkSize);
const contentLength = `bytes ${startFrom}-${startFrom + chunkSize}/${fileBuff.length}`
const uploadChunk = await uploadSession.resumableUpload.upload(chunkSize, fileChunk, contentLength);
startFrom += chunkSize;
}
Logger.log({
data: retentionLabelStatusUrl,
level: LogLevel.Info,
message: "List of Users Data",
});

process.exit(0);
}
4 changes: 2 additions & 2 deletions debug/launch/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { ITestingSettings } from "../../test/load-settings.js";
// add your debugging imports here and prior to submitting a PR git checkout debug/debug.ts
// will allow you to keep all your debugging files locally
// comment out the example
import { Example } from "./sp.js";
// import { Example } from "./graph.js";
// import { Example } from "./sp.js";
import { Example } from "./graph.js";

// setup the connection to SharePoint using the settings file, you can
// override any of the values as you want here, just be sure not to commit
Expand Down
103 changes: 103 additions & 0 deletions docs/graph/files-labels.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# @pnp/graph/files - Sensitivity and Retention Labels (Premium Endpoint)

The ability to manage sensitivity and retention labels on drive items in SharePoint.

More information can be found in the official Graph documentation:

- [Drives/Files Resource Type](https://learn.microsoft.com/en-us/graph/api/resources/drive?view=graph-rest-1.0)

## IInvitations

[![Invokable Banner](https://img.shields.io/badge/Invokable-informational.svg)](../concepts/invokable.md) [![Selective Imports Banner](https://img.shields.io/badge/Selective%20Imports-informational.svg)](../concepts/selective-imports.md)

## Assign Sensitivity Label to Drive Item

Using the assignSensitivityLabel() you can add a sensitivity label to a DriveItem

```TypeScript
import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/sites";
import "@pnp/graph/groups";
import "@pnp/graph/files";
import { ISensitivityLabel } from "@pnp/graph/files";

const graph = graphfi(...);

const label: ISensitivityLabel = {
sensitivityLabelId: "b7a3c3d5-7b6d-4e6c-8e0c-3f5c7b1d0e3d",
assignmentMethod: "standard",
justificationText: "Just because",
};

// This is a long running operation and returns a url to check the status.
const retentionLabelStatusUrl = await graph.sites.getById({site id}).drive.getItemById({item id}).assignSensitivityLabel(label);
const retentionLabelStatusUrl = await graph.users.getById({user id}).drive.getItemById({item id}).assignSensitivityLabel(label);
const retentionLabelStatusUrl = await graph.group.getById({group id}).drive.getItemById({item id}).assignSensitivityLabel(label);
```

## Extract Sensitivity Labels from a Drive Item

Using extractSensitivityLabels() extract one or more sensitivity labels assigned to a drive item

```TypeScript
import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/sites";
import "@pnp/graph/groups";
import "@pnp/graph/files";

const graph = graphfi(...);

const sensitivityLabels = await graph.sites.getById({site id}).drive.getItemById({item id}).extractSensitivityLabels();
const sensitivityLabels = await graph.users.getById({user id}).drive.getItemById({item id}).extractSensitivityLabels();
const sensitivityLabels = await graph.group.getById({group id}).drive.getItemById({item id}).extractSensitivityLabels();
```

## Retrieve/Update/Delete Retention Label of the Drive Item

Method for retrieving, updating, and removing the retention label of the drive item.

```TypeScript
import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/sites";
import "@pnp/graph/groups";
import "@pnp/graph/files";

const graph = graphfi(...);

// Get retention label
const retentionLabel = await graph.sites.getById({site id}).drive.getItemById({item id}).retentionLabel();
const retentionLabel = await graph.users.getById({user id}).drive.getItemById({item id}).retentionLabel();
const retentionLabel = await graph.group.getById({group id}).drive.getItemById({item id}).retentionLabel();

// Update retention label
const retentionLabel = await graph.sites.getById({site id}).drive.getItemById({item id}).updateRetentionLabel("New Name");
const retentionLabel = await graph.users.getById({user id}).drive.getItemById({item id}).updateRetentionLabel("New Name");
const retentionLabel = await graph.group.getById({group id}).drive.getItemById({item id}).updateRetentionLabel("New Name");

// Delete retention label
await graph.sites.getById({site id}).drive.getItemById({item id}).removeRetentionLabel();
await graph.users.getById({user id}).drive.getItemById({item id}).removeRetentionLabel();
await graph.group.getById({group id}).drive.getItemById({item id}).removeRetentionLabel();
```

## Lock/Unlock Record of the Drive Item

Method for locking/unlocking a record of the drive item.

```TypeScript
import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/sites";
import "@pnp/graph/groups";
import "@pnp/graph/files";

const graph = graphfi(...);

// Send 'true' to lock the record, and 'false' to unlock the record.
const retentionLabel = await graph.sites.getById({site id}).drive.getItemById({item id}).recordLocked(true);
const retentionLabel = await graph.users.getById({user id}).drive.getItemById({item id}).recordLocked(true);
const retentionLabel = await graph.group.getById({group id}).drive.getItemById({item id}).recordLocked(true);
```
Loading

0 comments on commit 625c8c2

Please sign in to comment.