0

I'm trying to test copying files from Documents library of one SharePoint site to the Documents library of another SharePoint site using PnP Core SDK. The code (based on the examples from https://pnp.github.io/pnpcore/using-the-sdk/files-large.html) looks like this:

        using (var sourcePnpContext = await pnpContextFactory.CreateAsync("Source"))
        {
            using (var destPnpContext = await pnpContextFactory.CreateAsync("Dest"))
            {
                string sourceDocumentUrl = $"{sourcePnpContext.Uri.PathAndQuery}/Shared Documents/{fileName}";
                IFile sourceFile = await sourcePnpContext.Web.GetFileByServerRelativeUrlAsync(sourceDocumentUrl);

                // Get file content from source file
                Stream contentStream = await sourceFile.GetContentAsync(true);

                // Get a reference to the destination folder
                IFolder destFolder = await destPnpContext.Web.Folders.Where(f => f.Name == "Shared Documents").FirstOrDefaultAsync();
                
                try
                {
                    // Add file with source content
                    IFile addedFile = await destFolder.Files.AddAsync(fileName, contentStream);
                }
                catch (Exception ex)
                {
                    log.LogError(ex.Message);
                }
            }
        };

The AddAsync line returns error message "Specified method is not supported."

The stack trace is:

at System.Net.Http.HttpBaseStream.get_Length() at PnP.Core.Model.SharePoint.FileCollection.d__2.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.Throw...

I do not understand why it is not working but I feel it must be something easy to fix...

The reason I'm using PnP Core SDK to test this copy is because eventually I will be copying files between SharePoint tenants.

Any help will be much appreciated! Thanks

3
  • Can you try to set position of the content stream to 0 like contentStream.Position=0; Aug 29, 2022 at 5:20
  • @KalpeshVaghela Thank you for your feedback. Setting content stream position did not fix it. But what I just tried and seems to solve the issue is when calling sourceFile.GetContentAsync(true), remove that 'true'. My guess is when 'true' is used, it does not get the whole content in one go, so destFolder.Files.AddAsync fails.
    – Conax
    Aug 29, 2022 at 5:50
  • @KalpeshVaghela That true is for streaming large files. Not sure taking out that will work when the file size > GB. I think better to try with CreateCopyJobs API which does the move on the servers side. Applicable to SharePoint Online to my understanding Oct 23, 2023 at 20:47

1 Answer 1

0

The AddFiles method reads the input stream by checking the length. Here the input stream is to another file which doesn't support even the length property and it fails. I would recommend using CreateCopyJobs if the environment is SharePoint Online or download/upload via disk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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