Trying to eliminate a performance bottleneck on a method doing file and metadata uploads. According to the profiler on a line-by-line of the decompiled code, the majority of time is spent on context.ExecuteQuery(). Below is the code. Any and all suggestions and insights greatly appreciated.

Public Shared Sub UploadDocuments(ByVal siteURL As String, _
                                 ByVal credentials As Net.NetworkCredential, _
                                 ByVal documentListName As String, _
                                 ByVal documentListURL As String, _
                                 ByVal sharePointUploads As List(Of SharePointDocumentToSave))

    Using context As New ClientContext(siteURL)
        If Not credentials Is Nothing Then
            context.Credentials = credentials
        End If

        AddHandler context.ExecutingWebRequest, AddressOf ClientContextExecutingWebRequest

        Dim documentsList As List = context.Web.Lists.GetByTitle(documentListName)

        For Each documentUpload As SharePointDocumentToSave In sharePointUploads
            Dim fci = New FileCreationInformation()
            With fci
                .Url = siteURL & documentListURL & documentUpload.DocumentName
                .Content = documentUpload.DocumentFile
                .Overwrite = True
            End With

            Dim uploadFile As Microsoft.SharePoint.Client.File = documentsList.RootFolder.Files.Add(fci)

            For Each pair As KeyValuePair(Of String, String) In documentUpload.MetaData
                uploadFile.ListItemAllFields(pair.Key) = pair.Value


    End Using

End Sub

This is totally normal when working with Client Object Model (CSOM). All operations are batched and not performed until you fire them away using ExecuteQuery() or ExecuteQueryAsync(). The rest of your code is just setting of local properties, which is converted into an XML format, which is sent to SharePoint at the time you execute the ExecuteQuery method.

  • 1
    Thanks for the reply. In particular, would you have any idea why it might take 20 seconds for a 20KB file to upload? This is being executed over a gigabit network. – Alfred Apr 5 '12 at 13:23

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.