The following piece of code creates an empty document in a library and get back all fields for the document (to set them later, and real document text will be loaded in a separate process):
string fileName = documentMetadata.DocumentStoreId + ".txt"; // Create empty file in memory byte byteArray = new byte; MemoryStream memoryStream = new MemoryStream(); memoryStream.Write(byteArray, 0, byteArray.Length); memoryStream.Seek(0, SeekOrigin.Begin); ClientContext clientContext = new ClientContext(_siteUrl); // Create an empty file File.SaveBinaryDirect(clientContext, "/Documents/" + fileName, memoryStream, true); // Load back the fields for the document just created. List documentLibrary = clientContext.Web.Lists.GetByTitle("Documents"); IEnumerable<File> files = clientContext.LoadQuery( documentLibrary.RootFolder.Files.Where(x => x.Name == fileName) .IncludeWithDefaultProperties(y => y.ListItemAllFields)); clientContext.ExecuteQuery();
I found that the performance of code drops greatly when more and more documents are uploaded. SQL Server Profiler shows that the time on LoadQuery is (almost) linear with the number of documents in library (0.4 sec when 4000 documents in library, 0.6 sec when 6000 documents in library and 0.9 sec when 9000 documents). This is definitely not scalable. The query searches against Name property which, I believe, is part of index in WSS_Content database by default.
How may I possibly improve the performance of this code? Or is there an alternate way to implement the same requirement: create a document placeholder and set up document fields?