{"id":1639,"date":"2014-02-24T16:31:07","date_gmt":"2014-02-24T16:31:07","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1639"},"modified":"2014-02-24T16:31:07","modified_gmt":"2014-02-24T16:31:07","slug":"cannot-invoke-http-dav-request","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2014\/02\/24\/cannot-invoke-http-dav-request\/","title":{"rendered":"Cannot invoke HTTP DAV request"},"content":{"rendered":"<p>When using the Client-Side Object Model (CSOM) for SharePoint and you&#8217;re trying to create\\overwrite a file using binary data, you&#8217;d naturally try and use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.client.file.savebinarydirect(v=office.14)\" title=\"File.SaveBinaryDirect Method\" target=\"_blank\">SaveBinaryDirect<\/a> method of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.client.file(v=office.14).aspx\" title=\"File Class\" target=\"_blank\">File<\/a> class:<\/p>\n<pre lang=\"csharp\">\r\npublic void SaveFile(Microsoft.SharePoint.Client.ClientContext context, Microsoft.SharePoint.Client.File file,\r\n                     string relativeItemUrl, byte[] fileData)\r\n{\r\n    using (Stream ms = new MemoryStream(fileData))\r\n    {\r\n        File.SaveBinaryDirect(context, relativeItemUrl, ms, true);\r\n    }\r\n}\r\n<\/pre>\n<p>When using a MemoryStream the following error will more than likely be raised:<\/p>\n<p><em>Cannot invoke HTTP DAV request. There is a pending query<\/em><br \/>\n<!--more--><br \/>\nI&#8217;ve had better success using the System.IO.File class to obtain a FileStream object for a physical file, although in my tests this has been rather hit or miss.  Sometimes it worked perfectly and others it gave the same HTTP DAV request error.<\/p>\n<p>Even if that did work perfectly, it doesn&#8217;t get around the problem of creating a file on the fly not based on one from a FileStream.<\/p>\n<p>The solution to the problem is to bypass the <em>SaveBinaryDirect<\/em> method and use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.client.filecreationinformation.aspx\" title=\"FileCreationInformation class\" target=\"_blank\">FileCreationInformation<\/a> class instead:<\/p>\n<pre lang=\"csharp\">\r\npublic void SaveFile(Microsoft.SharePoint.Client.ClientContext context, string folderRelativeUrl,\r\n                     string relativeItemUrl, byte[] fileData)\r\n{\r\n    var fci = new FileCreationInformation\r\n    {\r\n        Url = relativeItemUrl,\r\n        Content = fileData,\r\n        Overwrite = true\r\n    };\r\n\r\n    Microsoft.SharePoint.Client.Folder folder = context.Web.GetFolderByServerRelativeUrl(folderRelativeUrl);\r\n    Microsoft.SharePoint.Client.FileCollection files = folder.Files;\r\n    Microsoft.SharePoint.Client.File file = files.Add(fci);\r\n\r\n    context.Load(files);\r\n    context.Load(file);\r\n    context.ExecuteQuery();\r\n}\r\n<\/pre>\n<p>Creating or overwriting files using this method does not raise the <em>HTTP DAV Request<\/em> error unlike the <em>SaveBinaryDirect<\/em> method which is susceptible to this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using the Client-Side Object Model (CSOM) for SharePoint and you&#8217;re trying to create\\overwrite a file using binary data, you&#8217;d naturally try and use the SaveBinaryDirect method of the File class: public void SaveFile(Microsoft.SharePoint.Client.ClientContext context, Microsoft.SharePoint.Client.File file, string relativeItemUrl, byte[] &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2014\/02\/24\/cannot-invoke-http-dav-request\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[67,3],"tags":[91,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-qr","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1639"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1639"}],"version-history":[{"count":4,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1639\/revisions"}],"predecessor-version":[{"id":1644,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1639\/revisions\/1644"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}