Skip to content

Commit

Permalink
Documentation updates
Browse files Browse the repository at this point in the history
Fixed sample pnp#2217
Fixed sample pnp#2218
Added samples pnp#2212
  • Loading branch information
juliemturner committed Apr 18, 2022
1 parent 2c9f409 commit b5c06c6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 58 deletions.
3 changes: 3 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ const spWebB = spfi({Other Web URL}).using(AssignFrom(sp.web));

// Option 3: Create a new instance of Queryable using other credentials?
const spWebB = spfi({Other Web URL}).using(SPDefault(this.context));

// Option 4: Create new Web instance by using copying SPQuerable and new pointing to new web url
const web = Web([sp.web, {Other Web URL}]);
```
## Next Steps
Expand Down
80 changes: 23 additions & 57 deletions docs/sp/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,57 +52,30 @@ Likewise you can add files using one of two methods, addUsingPath or addChunked.

The addUsingPath method, supports the percent or pound characters in file names.

```typescript
declare var require: (s: string) => any;

import { ConsoleListener, Logger, LogLevel } from "@pnp/logging";

import { Web } from "@pnp/sp/webs";
```TypeScript
import { spfi } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/files";
import "@pnp/sp/folders";
import { auth } from "./auth";
let $ = require("jquery"); // <-- used here for illustration

let siteUrl = "https://mytenant.sharepoint.com/sites/dev";

// comment this out for non-node execution
// auth(siteUrl);

Logger.subscribe(new ConsoleListener());
Logger.activeLogLevel = LogLevel.Verbose;

let web = Web(siteUrl);


$(() => {
$("#testingdiv").append("<button id='thebuttontodoit'>Do It</button>");

$("#thebuttontodoit").on('click', async (e) => {

e.preventDefault();

let input = <HTMLInputElement>document.getElementById("thefileinput");
let file = input.files[0];

// you can adjust this number to control what size files are uploaded in chunks
if (file.size <= 10485760) {

// small upload
await web.getFolderByServerRelativePath("Shared Documents").files.addUsingPath(file.name, file, {Overwrite: true});
Logger.write("done");
} else {

// large upload
await web.getFolderByServerRelativePath("Shared Documents").files.addChunked("filename%#%.txt", file, data => {
const sp = spfi(...);

Logger.log({ data: data, level: LogLevel.Verbose, message: "progress" });
//Sample uses pure JavaScript to access the input tag of type="file" ->https://www.w3schools.com/tags/att_input_type_file.asp
let input = <HTMLInputElement>document.getElementById("thefileinput");
const fileNamePath = encodeURI(file.name);
let result: IFileAddResult;
// you can adjust this number to control what size files are uploaded in chunks
if (file.size <= 10485760) {
// small upload
result = await sp.web.getFolderByServerRelativePath("Shared Documents").files.addUsingPath(fileNamePath, file, { Overwrite: true });
} else {
// large upload
result = await sp.web.getFolderByServerRelativePath("Shared Documents").files.addChunked(fileNamePath, file, data => {
console.log(`progress`);
}, true);
}

}, true);
Logger.write("done!")
}
});
});
console.log(`Result of file upload: ${JSON.stringify(result)}`);
```

### Adding a file using Nodejs Streams
Expand All @@ -113,26 +86,19 @@ If you are working in nodejs you can also add a file using a stream. This exampl

```TypeScript
// triggers auto-application of extensions, in this case to add getStream

import { spfi } from "@pnp/sp";
import "@pnp/nodejs";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
import "@pnp/sp/files";
import "@pnp/sp/folders/list";
import "@pnp/sp/files/folder";
import { createReadStream } from 'fs';
import { SPDefault } from "@pnp/nodejs";
import { ThrowErrors } from "@pnp/queryable";

// get a stream of an existing file
const stream = createReadStream("c:/temp/file.txt");

// now add the stream as a new file, remember to set the content-length header
const sp = spfi("{tenant url}").using(SPDefault({
baseUrl: 'https://{tenant}.sharepoint.com/sites/dev',
msal: {
config: config,
scopes: [ 'https://{tenant}.sharepoint.com/.default' ]
}
})).using(ThrowErrors());
// now add the stream as a new file
const sp = spfi(...);

const fr = await sp.web.lists.getByTitle("Documents").rootFolder.files.addChunked( "new.txt", stream, undefined, true );
```
Expand Down
2 changes: 2 additions & 0 deletions docs/sp/taxonomy.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Provides access to the v2.1 api term store
[![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)

![Batching Not Supported Banner](https://img.shields.io/badge/Batching%20Not%20Supported-important.svg)

## Term Store

Access term store data from the root sp object as shown below.
Expand Down
23 changes: 22 additions & 1 deletion docs/sp/webs.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ There are several ways to use the `Web` factory directly and have some special c
```TypeScript
import { spfi } from "@pnp/sp";
import "@pnp/sp/webs";
import { Web } from "@pnp/sp/webs";

// creates a web:
// - whose root is "https://tenant.sharepoint.com/sites/myweb"
Expand Down Expand Up @@ -135,6 +135,27 @@ const web3 = Web("https://tenant.sharepoint.com/sites/myweb", "some sub web/_api
const web4 = Web("https://tenant.sharepoint.com/sites/myweb", "_api/web/lists");
```

The above examples show you how to use the constructor to create the base url for the `Web` although none of them are usable as is until you add observers. You can do so by either adding them explicitly with a using...

```TypeScript
import { spfi, SPFx } from "@pnp/sp";
import { Web } from "@pnp/sp/webs";

const web1 = Web("https://tenant.sharepoint.com/sites/myweb").using(SPFx(this.context));
```

or by copying them from another SPQueryable instance...

```TypeScript
import { spfi } from "@pnp/sp";
import { Web } from "@pnp/sp/webs";
import "@pnp/sp/webs";

const sp = spfi(...);
//sp.web is of type SPQueryable; using tuple pattern pass SPQueryable and the web's url
const web = Web([sp.web, "https://tenant.sharepoint.com/sites/otherweb"]);
```

### webs

Access the child [webs collection](#Webs%20Collection) of this web
Expand Down

0 comments on commit b5c06c6

Please sign in to comment.