0

I am working on an app that when a file is created through Office, it asks the user to input the values for the fields that are stored in the Library's List. While that works great, I am completely unable to update that created file without updating its list item information and, while I have tried a huge range of options, it always override everything. I cannot ask the user to re-enter those values and the file might be in-memory or downloaded from SharePoint Online directly.

I only want to update the file's content, and I can use a byte[] or a Stream as needed. I am using this right now, just to try stuff out:

using (var context = new ClientContext(repositoryUri + "subSiteName"))
{
    context.Credentials = CurrentUser.Credentials;

    var web = context.Web;
    try
    {
        context.Load(web.Lists, all => all.Where(l => l.RootFolder.Name == "libraryName").Include(l => l.RootFolder));
        context.ExecuteQuery();
        var list = web.Lists.FirstOrDefault();

        var folder = list.RootFolder;

        context.Load(folder, f => f.Files);
        context.ExecuteQuery();

        var file = folder.Files.Where(f => f.Name == "fileName" /* hard-coded for testing */).FirstOrDefault();

        var fileInformation = new FileCreationInformation
        {
            Content = fileData,
            Url = file.ServerRelativeUrl,
            Overwrite = true // false: throws, true: overwrites list item
        };

        var newFile = folder.Files.Add(fileInformation);

        context.ExecuteQuery();
    }
    catch (Exception ex) // just to read exception messages
    {

    }
}

Is there a way to just update the binary? I use the REST API but now switched to CSOM to see if there was any better option.

0

I found an unwanted answer that at least works. After observing that SharePoint keeps metadata on Office files after it downloads them, I decided to do the same.
Now, when the file is saved as "new" by the user, I:

  1. Upload it, and keep its new URL
  2. Update its metadata using the file's URL
  3. Download the file to a temporary location
  4. Open that file without the user knowing about it.

The result is that when the file is saved again, the metadata is kept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.