c# - Uploading file using Managed client object model in sharepoint 2013 -
i have devloped file uploading document library in sharepoint 2013 using managed client object model. here upload.aspx
<%@ page language="c#" autoeventwireup="true" codebehind="webform1.aspx.cs" inherits="webapplication7.webform1" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td colspan="2" align="center"> <asp:label font-bold="true" text="upload documents sharepoint 2013" runat="server" id="lblsharepoint" /> </td> </tr> <tr> <td></td> </tr> <tr> <td> <asp:label id="lbllocalpath" runat="server" text="local path:"></asp:label> </td> <td> <asp:fileupload id="filelocalpath" width="350" runat="server" /> </td> </tr> <tr> <td> <asp:label id="lblsharepointurl" runat="server" text="sharepoint site url:"></asp:label> </td> <td> <asp:textbox id="txtsharepointurl" width="350" runat="server"></asp:textbox> </td> </tr> <tr> <td> <asp:label id="lbllibraryname" runat="server" text="library name:"></asp:label> </td> <td> <asp:textbox id="txtlibraryname" width="350" runat="server"></asp:textbox> </td> </tr> <tr> <td colspan="2" align="right"> <asp:button id="btnupload" runat="server" text="upload" onclick="btnupload_click" /> </td> </tr> <tr> <td colspan="2"> <asp:label id="lblmsg" runat="server" text=""></asp:label> </td> </tr> </table> </div> </form> </body> </html>
and here upload.aspx.cs
using microsoft.sharepoint.client; using system; using system.collections.generic; using system.drawing; using system.io; using system.linq; using system.net; using system.web; using system.web.ui; using system.web.ui.webcontrols; namespace webapplication7 { public partial class webform1 : system.web.ui.page { protected void page_load(object sender, eventargs e) { } public clientcontext spclientcontext { get; set; } public web spweb { get; set; } public string sperrormsg { get; set; } protected void btnupload_click(object sender, eventargs e) { try { string surl = txtsharepointurl.text; string sdocname = string.empty; if (!string.isnullorwhitespace(surl) && !string.isnullorwhitespace(txtlibraryname.text) && (filelocalpath.hasfile)) { bool bbconnected = connect(surl, "abhishekr", "a123bhishek", "saras"); if (bbconnected) { uri uri = new uri(surl); string sspsiterelativeurl = uri.absolutepath; sdocname = uploadfile(filelocalpath.filecontent, filelocalpath.filename, sspsiterelativeurl, txtlibraryname.text); if (!string.isnullorwhitespace(sdocname)) { lblmsg.text = "the document " + sdocname + " has been uploaded successfully.."; lblmsg.forecolor = color.blue; } else { lblmsg.text = sperrormsg; lblmsg.forecolor = color.red; } } } } catch (exception ex) { response.write(ex.message.tostring()); } } public bool connect(string spurl, string spusername, string sppassword, string spdomainname) { bool bconnected = false; try { spclientcontext = new clientcontext(spurl); spclientcontext.credentials = new networkcredential(spusername, sppassword, spdomainname); spweb = spclientcontext.web; spclientcontext.load(spweb); spclientcontext.executequery(); bconnected = true; } catch (exception ex) { bconnected = false; sperrormsg = ex.message; response.write(ex.message.tostring()); } return bconnected; } public string uploadfile(stream fs, string sfilename, string sspsiterelativeurl, string slibraryname) { string sdocname = string.empty; try { if (spweb != null) { var sfileurl = string.format("{0}/{1}/{2}", sspsiterelativeurl, slibraryname, sfilename); microsoft.sharepoint.client.file.savebinarydirect(spclientcontext, sfileurl, fs, true); sdocname = sfilename; } } catch (exception ex) { sdocname = string.empty; sperrormsg = ex.message; response.write(ex.message.tostring()); } return sdocname; } } }
it working fine when uploading small files, through error when uploading large file.
error:" maximum request length exceeded "
please, me how resolve issue.
you can define max request length in sharepoint this: https://blogs.technet.microsoft.com/sammykailini/2013/11/06/how-to-increase-the-maximum-upload-size-in-sharepoint-2013/
in central admin>application management>manage web applications> select desired web app , click general settings on ribbon
in web.config edit httpruntime node (for 2gb files , unlimited execution):
Comments
Post a Comment