SharePoint 2013 Auto hosted Apps:
When you create a SharePoint app and try to upload a large size document through client side object model, you face some issue related to maximum size limit.
You try below code which works for me:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Client;
using COM = Microsoft.SharePoint.Client;
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Net;
namespace TestMSWeb.Pages
{
public partial class test : System.Web.UI.Page
{
SharePointContextToken contextToken;
string accessToken;
Uri sharepointUrl;
COM.Web web = null;
COM.ClientContext clientContext = null;
protected void Page_Load(object sender, EventArgs e)
{
TokenHelper.TrustAllCertificates();
string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request);
if (contextTokenString != null)
{
contextToken =
TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);
sharepointUrl = new Uri(Request.QueryString[“SPHostUrl”]);
accessToken =
TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken;
ViewState[“AccessToken”] = accessToken;
// btnProcessData.CommandArgument = accessToken;
}
else if (!IsPostBack)
{
Response.Write(“Could not find a context token.”);
return;
}
if (IsPostBack)
{
sharepointUrl = new Uri(Request.QueryString[“SPHostUrl”]);
accessToken = ViewState[“AccessToken”].ToString();
}
clientContext = TokenHelper.GetClientContextWithAccessToken(sharepointUrl.ToString(), accessToken);
web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
}
protected void UploadAttachements()
{
//upload proposal documents
List documentsList = clientContext.Web.Lists.GetByTitle(“Documents”);
clientContext.Load(documentsList);
if (attachproposalDoc.PostedFile != null && attachproposalDoc.PostedFile.FileName != string.Empty)
{
using (var stream = attachproposalDoc.PostedFile.InputStream)
{
var fileCreationInformation = new FileCreationInformation();
//Assign to content byte[] i.e. documentStream
fileCreationInformation.ContentStream = stream;
//Allow owerwrite of document
fileCreationInformation.Overwrite = true;
//Upload URL
string[] strArray=attachproposalDoc.PostedFile.FileName.Split(‘\\’);
fileCreationInformation.Url = “ProposalDoc_” + strArray[strArray.Length-1];
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
clientContext.ExecuteQuery();
uploadFile.ListItemAllFields[“Title”] = attachproposalDoc.PostedFile.FileName;
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}
}
}
protected void testBtn_Click(object sender, EventArgs e)
{
UploadAttachements();
}
}
}
Update Web.Config Entries of your App:
replace your <httpRuntime> tag with below line:
<httpRuntime targetFramework=”4.5″ executionTimeout=”999999″ maxRequestLength=”2097151″ />