søndag den 3. juli 2011

SharePoint 2010 Client and OpenBinaryDirect

Update 22-09-2011: There is a much better way to handle this here
So now we know how to get and update information. Now its time to figure out how to upload and download files from SharePoint. All the examples you’ll find out there reference the above command, OpenBinaryDirect. Well, it doesn’t work when using the trick mentioned in my last blog post but I found a good work around.

Someone at stackoverflow gave an example on how to access cookiecontainer in webclient, and that was just what I needed to my downloads.
( the complete project can be downloaded here )

Imports System.Net

Public Class CookieAwareWebClient
    Inherits WebClient
    Private CookieJar As New CookieContainer()
    Private _domain As String
    Public Property Domain() As String
        Get
            Return _domain
        End Get
        Set(ByVal value As String)
            _domain = value
            NewCookieContainer()
        End Set
    End Property

    Private _SAMLToken As String
    Public Property SAMLToken() As String
        Get
            Return _SAMLToken
        End Get
        Set(ByVal value As String)
            _SAMLToken = value
            NEwCookieContainer()
        End Set
    End Property

    Sub NewCookieContainer()
        If _domain <> "" And _SAMLToken <> "" Then
            Dim samlAuth As New Cookie("FedAuth", SAMLToken)
            samlAuth.Expires = DateTime.Now.AddHours(1)
            samlAuth.Path = "/"
            samlAuth.Secure = True
            samlAuth.HttpOnly = True
            samlAuth.Domain = _domain
            CookieJar = New CookieContainer()
            CookieJar.Add(samlAuth)
        End If
    End Sub

    Protected Overrides Function GetWebRequest(address As Uri) As WebRequest
        Dim request As WebRequest = MyBase.GetWebRequest(address)
        If TypeOf request Is HttpWebRequest Then
            TryCast(request, HttpWebRequest).CookieContainer = CookieJar
        End If
        Return request
    End Function
End Class

And then after getting an item you can download it with

Dim cli As New CookieAwareWebClient
cli.Domain = SPAuth.samlUri.Host
cli.SAMLToken = SPAuth.SAMLToken

Dim Uri As New Uri("https://" & cli.Domain & "/" & CurrentListItem("FileRef"))
Dim filename As String = Mid(CurrentListItem("FileRef"), InStrRev(CurrentListItem("FileRef"), "/") + 1)
cli.DownloadFile(Uri, "c:\" & filename)

And finally, heres how to upload without using SaveBinaryDirect who also failed with 403 on sites only configured for claims based authentication.

OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName <> "" Then

    Dim listName As String = CurrentList.Title
    Dim filePath As String = OpenFileDialog1.FileName
    Dim filename As String = Mid(filePath, InStrRev(filePath, "\") + 1)
    'Dim fs As New FileStream(filePath, FileMode.Open)
    'Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, "/" & s & "/" & filename, fs, True)  '"/Shared Documents/" + filename, fs, True)

    Dim newFile As FileCreationInformation = New FileCreationInformation
    newFile.Content = System.IO.File.ReadAllBytes(filePath)
    newFile.Url = "/" & listName & "/" + filename
    newFile.Overwrite = True
    Dim uploadFile As Microsoft.SharePoint.Client.File = CurrentList.RootFolder.Files.Add(newFile)
    clientContext.Load(uploadFile)
    clientContext.ExecuteQuery()
    ' Set MetaData
    'Dim item As ListItem = uploadFile.ListItemAllFields
    'item("Title") = "TEST " & filename
    'item.Update()
    'clientContext.ExecuteQuery()
End If

2 kommentarer:

  1. CurrentList.RootFolder.Files.Add has the big limit of 2.5Mb File dimension

    SvarSlet
  2. I'm new to SharePoint and using SharePoint Foundation server 2010 to save files using client object model. The application will list the files in the server using MVC4 UI. To retreive the files and folders I use SharePoint 2010 client object model. I want to implement the Edit feature for the word and excel documents which is already available when we access SharePoint site directly through IE. Simply I just want to open the file and save changes to the server when we save it or close the program. Is it possible to do using client object model. I can open the file but not save the changes to the server. I google it to find a solution but yet not succeeded. If anybody knows how to do it please answer. Thanks in advance

    SvarSlet