0

SP2016 On Prem:

Utltimate Goal: Pull files from Sharepoint Document libraries and place onto file server.

I'm using this Sharepoint api to read the data from the file:

http://siteurl/_api/web/GetFileByServerRelativeUrl('/MyDocLib/myWordDoc.docx')/$value

I can download the file data from the api, then manually launch Word and open that file. It looks perfect.

But now I need to create those files programatically on a file server using visual basic.

It works great when it's a simple text file, but with Word docs I get the error:

We're sorry. We can't open myWordDoc.docx because we found a problem with its contents.

I've tried 2 methods to create and populate the file:

Method 1:

Sub CreateFile(filename As String, newText As String)
        Dim path As String = "c:\temp\" + filename
        Dim sw As StreamWriter
        ' Create or overwrite the file.
        sw = File.AppendText(path)
        sw.WriteLine(newText)
        sw.Dispose()
End Sub

Method 2:

Sub CreateFile(filename As String, newText As String)
        Dim path As String = "c:\temp\" + filename
        Dim fs As FileStream = File.Create(path)
        Dim info As Byte() = New System.Text.UTF8Encoding(True).GetBytes(newText)
        fs.Write(info, 0, info.Length)
        fs.Close()
End Sub

I assume I need to encode the text somehow but I don't know what else to try.

Eventually I need to do this with Excel, ppt and pdf files too so I need something generic.

If you know of a way to simply grab the files and move them that would be better, but I don't know how I can do that using the sharepoint api.

The copyto api can only copy files between sharepoint sites, it can't access the file system.

Thank you

0

After a couple of days I decided to abandon the above approach and do it through the file system instead.

Now I use the Sharepoint api to locate the files that need to be moved, but then use the simple File.Copy method to do the transfer:

FSO = CreateObject("Scripting.FileSystemObject")

Directory = FSO.GetFolder(ServerShare)
For Each Filename In Directory.Files
     'MsgBox(Filename.Name)
     File.Copy(ServerShare + Filename.name, destinationFolder + Filename.Name, True)
     Debug.Print(Filename.Name + " Copied Sucessfully")
Next

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.