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[0];
 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));

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?


I am facing a similar issue of extracting items from document library via CAML query. Today, I was executing adhoc queries and noticed if we include ListItemAllFields it tends to take an extra bit of time which makes sense because SharePoint is actually reading all fields attached to the document/item. Well, I was getting 651 hits in about 1021ms and same hits in about 6051ms when used ListItemAllFields. Its really not performance optimised since there is no use by getting name of the file or Id.

I would say if possible try removing ListItemAll Fields and give it a go!

If you have already found any solution to your problem, please share your thoughts.


| improve this answer | |

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.