lundi 8 décembre 2014

Upload files to SharePoint Online using REST API



I'm trying to develop a console application which will upload files from the users client PC to a SharePoint Online site.


I've written a version that used CSOM, but the file size is limited to 1.5Mb, so now I'm trying to use the REST API (it supports files up to 2Gb), however when I try to perform the upload all I get is 'forbidden'


I successfully obtain the form digest value, the code I have is



Public Shared Function UploadFile(baseURL As String, digest As CDigest, sourceFile As String) As Boolean

Dim byteArray As Byte() = IO.File.ReadAllBytes(sourceFile)

Dim folderName As String = "/TestFolder"
Dim fileName As String = "Test.txt"
Dim s As String = "/_api/web/GetFolderByServerRelativeUrl('[Folder]')/Files/add(url='[Filename]',overwrite=false)"
s = s.Replace("[Folder]", folderName)
s = s.Replace("[Filename]", fileName)

Dim req As Net.HttpWebRequest = Net.HttpWebRequest.Create(baseURL & s)
req.Method = "POST"
req.Accept = "application/atom+xml"
req.Headers.Add("X-RequestDigest", digest.Value)
req.Headers.Add("binaryStringRequestBody", "true")
req.Headers.Add("X-HTTP-Method", "PUT")

req.ContentLength = byteArray.Length
Dim ds As IO.Stream = req.GetRequestStream()

ds.Write(byteArray, 0, byteArray.Length)

ds.Close()

Dim res As Net.HttpWebResponse = req.GetResponse() ' Errors here with Forbidden

Console.WriteLine(CType(res, Net.HttpWebResponse).StatusDescription)


I would grateful for any assistance


Thanks


Lee...








0 commentaires:

Enregistrer un commentaire