Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Update file contents with REST #214

Closed
Abrissirba opened this issue Oct 9, 2016 · 10 comments
Closed

Update file contents with REST #214

Abrissirba opened this issue Oct 9, 2016 · 10 comments
Assignees

Comments

@Abrissirba
Copy link
Contributor

Category

[x ] Enhancement

[ ] Bug

[x ] Question

Expected / Desired Behavior / Question

I want to be able to update the contents of a file with REST.

Observed Behavior

I can't see any way of making the rest call defined in https://msdn.microsoft.com/en-us/library/office/dn450841.aspx that suggest a call to getfilebyserverrelativeurl('/Shared Documents/filename.txt')/$value with the X-HTTP-METHOD set to PUT. I searched the code for $value but only found one occurrence and was only to get the content.

@patrick-rodgers
Copy link
Contributor

Great suggestion, will try and sneak this into 1.0.5.

@patrick-rodgers
Copy link
Contributor

Had a look at this and we do already have the saveBinaryStream method on file which takes a blob. Does that option not work for you or do you need some other functionality it isn't providing?

@patrick-rodgers
Copy link
Contributor

Any updates here? Was the saveBinaryStream method what you needed?

@Abrissirba
Copy link
Contributor Author

I did a quick test with pnp.sp.web.getFileByServerRelativeUrl(file.ServerRelativeUrl).saveBinaryStream(content) which produced a POST to api/web/getFileByServerRelativeUrl('/Document/test.txt')/saveBinary with a 404 as result. The documentation here is quite unclear about whether the endpoint is saveBinary or saveBinaryStream, https://msdn.microsoft.com/en-us/library/office/dn450841.aspx#bk_FileSaveBinaryStream.

@patrick-rodgers
Copy link
Contributor

Thanks - will have a look and do some testing.

@patrick-rodgers
Copy link
Contributor

Hi @Abrissirba ,

Check out the updates in this PR I just merged into dev: #232. Added better support for getting and setting file content to the File class. Also now easily supports chunked uploading for large files.

Sample code for using the new methods.

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

import { ConsoleListener, Web, Logger, LogLevel, ODataRaw } from "sp-pnp-js";
import { auth } from "./auth";
let $ = require("jquery");

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

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

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

let web = new Web(siteUrl);

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

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

        e.preventDefault();

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

        if (file.size <= 10485760) {
            // small upload

            web.getFolderByServerRelativeUrl("/sites/dev/Shared%20Documents/test/").files.add(file.name, file, true).then(_ => Logger.write("done"));

        } else {

            // large upload
            web.getFolderByServerRelativeUrl("/sites/dev/Shared%20Documents/test/").files.addChunked(file.name, file, data => {

                Logger.log({ data: data, level: LogLevel.Verbose, message: "progress" });

            }).then(_ => Logger.write("done!"));
        }

        // web.getFileByServerRelativeUrl("/sites/dev/Shared%20Documents/test/test.txt").setContent("New string content for the file.");

        // web.getFileByServerRelativeUrl("/sites/dev/Shared%20Documents/test/something.mp4").setContentChunked(file);
    });
});

@patrick-rodgers
Copy link
Contributor

Small note, can't actually merge currently due to permissions after transferring the repo ownership.

@patrick-rodgers
Copy link
Contributor

Now merged in dev branch

@Abrissirba
Copy link
Contributor Author

Looks good!

@patrick-rodgers
Copy link
Contributor

Thanks, will close this as resolved - can reopen if the updates in dev aren't working for you. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants