There are 2 ways to upload files to SharePoint 2010 document libraries using managed client object model.
The first method uses a client library batch mechanism. It will encode the binary using BASE64 encoding, which could increase the message size by one third. There might be problems if this isn’t configured correctly. An example of this code is below:
1: ClientContext context = new ClientContext("http://spdevinwin");
2:
3: Web web = context.Web;
4:
5: FileCreationInformation newFile = new FileCreationInformation();
6: newFile.Content = System.IO.File.ReadAllBytes(@"C:\Work\Files\17580_FAST2010_S05_Administration.pptx");
7: newFile.Url = "17580_FAST2010_S05_Administration 4MB file uploaded via client OM.pptx";
8:
9: List docs = web.Lists.GetByTitle("Documents");
10: Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
11: context.Load(uploadFile);
12: context.ExecuteQuery();
13: Console.WriteLine("done");
The above code might fail throwing a (400) Bad Request error depending on the file size. The following code is used to set a higher limit to the upload size. Note: This is not the same as the max. file size upload limit option available in the web application settings.
1: SPWebService ws = SPWebService.ContentService;
2: SPClientRequestServiceSettings clientSettings = ws.ClientRequestServiceSettings;
3: clientSettings.MaxReceivedMessageSize = 10485760;
4: ws.Update();
5: Console.WriteLine(clientSettings.MaxReceivedMessageSize);
The above code sets the message batch size to 10MB so that larger files can be uploaded using managed client object model.
The second methods uses HTTP DAV and sends raw binary across the wire and does not increase the message size. It is also the preferred upload method when using managed client object model. Here’s a sample:
2: using (FileStream fs = new FileStream(@"C:\Work\Files\17580_FAST2010_S03_Arch.pptx", FileMode.Open))
3: {
4: Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/documents/17580_FAST2010_S03_Arch from client OM.pptx", fs, true);
5: }
Hope this helps! Stay tuned for most posts on managed client object model.
Hi
I tried to implement the second way to upload documents into SharePoint Foundation 2010 however, all the documents were saved as checked out if "Required Checked Out" option is enabled for a document library. If anyone had encountered similar problem, a little guidance would be helpful.
Thanks
Mani
This is a great article.
Thank You!
When trying to run from my dev system, getting this same exception
"client requestexception Cannot invoke HTTP DAV request".
hope someone posts the solution quickly.
Hello,
I am getting the same exception "Cannot invoke HTTP DAV request", Did anyone resolve it?please let me know
ClientContext clientContext = new ClientContext(siteUrl);
List CurrentList = clientContext.Web.Lists.GetByTitle(libraryName);
clientContext.Load(CurrentList.RootFolder);
clientContext.ExecuteQuery();
using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
ClientOM.File.SaveBinaryDirect(clientContext, CurrentList.RootFolder.ServerRelativeUrl.ToString() + "/" + fileName.Split('\\')[1], fileStream, true);
We need to Load the Document Libraries' Root Folder before we execute the query. I've changed the code accordingly and it worked fine for me, I've tested it with Uploading 40 MB file and it went well. Try It.
Cheers,
Satish Cheeli
Nice article, thanks. What is the size limit on WebDav solution?
I had the following two errors:
Error 1 = The remote server returned an error: (409) Conflict
Error 2 = Cannot invoke HTTP DAV request. There is a pending query
Dug through Google but found no solution.... finally I tried playing with the "SPC.ClientContext" path and the "fileUrl" path ... BINGO ... it got fixed. Below is before and after code:
BEFORE:
SPC.ClientContext clientContext = new SPC.ClientContext("http://dev:44941/ParentSite");
string fileUrl = "/CreatedDocs/MyDoc.docx";
AFTER
SPC.ClientContext clientContext = new SPC.ClientContext("http://dev:44941");
string fileUrl = "/ParentSite/CreatedDocs/MyDoc.docx";
Hope this helps someone.
Hi, Try checking or a Pending request first, like so:
if (clientContext.HasPendingRequest)
FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, (string)v["FileRef"]);
SaveBinaryDirect throws 403 error if used with saml claims authentication. Bug? Any workarounds?
Is anyone monitoring this blog? All I see are tons of questions with no responses.
Sure why not just leave your requirements and Sridhar can develop your solution FOC!
Hi,
I tried this code in sandboxed visual web part and I am getting below error.
Error Msg: "Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred."
Please let me know how to upload File using sandboxed visual web part.
Thanks in Advanced.
I could not finr SPwebService class in Managed client object model..?? how I could get it?? Title of this blog post suggest that we can upload large files with the help of Client Object Model but code is say something diffrent isn't it?
I want to use first methos might be second one work but I need First one for updating metadata with upload folder..
When I use Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/documents/17580_FAST2010_S03_Arch from client OM.pptx", fs, true);
I get an exception"
" The remote server returned an error: (405) Method Not Allowed."
StackTrace:
StackTrace = " at System.Net.HttpWebRequest.GetResponse()\r\n at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()\r\n at Microsoft.SharePoint.Client.File.SaveBinary(ClientContext context, String serverRelativeUrl, Stream stream, String etag, Boolean over...
Any help would be appreciated
HI ,
Please can anyone tell me how to authenticate Rest service.
I am doing the same but getting exception while doing any thing with sharepoint.
I belive this is due to service is not authenticate.
So please can any one explain how to do that.
Regards,
Manoj