0

I want to upload large files to SharePoint 2010 from web service using client object model.

Below is the code, which i am using to upload file to sharepoint.

using (MemoryStream stream = new MemoryStream(System.IO.File.ReadAllBytes(file)))
{
 Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, relativeurl, stream, true);
}

Problem is, it is working fine for small files only. but when i am uploading file with 120 MB, then it is throwing Operation TimeOut error. Then i set time out to 20 minutes using

spcontext.RequestTimeout = 1200000;

But still it is not working. Then i found that i need to increase file upload size limit on web application level.

So i set it to 200 MB, which is shown below:

enter image description here

But still it didn't work.

Then i found below site reference http://thuansoldier.net/?p=4328, which says that we need to allow messages larger than 2097152 bytes.

So i ran below commands on my SharePoint server also

$a = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$a.ClientRequestServiceSettings.MaxReceivedMessageSize = 200000000
$a.Update()

But no luck.

Can anyone please help me on this?

Below is the my webservice code:

 [WebMethod]
    public string UploadFilesToSharePoint()
    {
        try
        {
            string file = @"C:\TEST\Professional_Microsoft_SQL_Server_2012_Analysis_Services_with_MDX.pdf";
            UploadDocumentRPC("http://domain:port/sites/test/", "Docs", file);

            return "Successfully Uploaded.";
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
    }

 public static void UploadDocumentRPC(string siteUrl, string libraryName, string filePath)
    {
        string method = HttpUtility.UrlEncode("put document:14.0.2.5420");
        string serviceName = HttpUtility.UrlEncode(siteUrl);
        string document = HttpUtility.UrlEncode(libraryName + "/" + System.IO.Path.GetFileName(filePath));
        string metaInfo = string.Empty;
        string putOption = "overwrite";
        string keepCheckedOutOption = "false";
        string putComment = string.Empty;
        string result = string.Empty;

        string fpRPCCallStr = "method={0}&service_name={1}&document=[document_name={2};meta_info=[{3}]]&put_option={4}&comment={5}&keep_checked_out={6}";
        fpRPCCallStr = String.Format(fpRPCCallStr, method, serviceName, document, metaInfo, putOption, putComment, keepCheckedOutOption);

        byte[] fpRPCCall = System.Text.Encoding.UTF8.GetBytes(fpRPCCallStr + "\n");
        byte[] postData = System.IO.File.ReadAllBytes(filePath);
        byte[] data;

        if (postData != null && postData.Length > 0)
        {
            data = new byte[fpRPCCall.Length + postData.Length];
            fpRPCCall.CopyTo(data, 0);
            postData.CopyTo(data, fpRPCCall.Length);
        }
        else
        {
            data = new byte[fpRPCCall.Length];
            fpRPCCall.CopyTo(data, 0);
        }

        HttpWebRequest wReq = WebRequest.Create(siteUrl + "/_vti_bin/_vti_aut/author.dll") as HttpWebRequest;

        NetworkCredential credentials = new NetworkCredential(Username, Password, Domain);

        wReq.Credentials = credentials;
        wReq.Method = "POST";
        wReq.Timeout = 1000000;
        wReq.ContentType = "application/x-vermeer-urlencoded";
        wReq.Headers.Add("X-Vermeer-Content-Type", "application/x-vermeer-urlencoded");
        wReq.ContentLength = data.Length;

        using (System.IO.Stream requestStream = wReq.GetRequestStream())
        {
            int chunkSize = 2097152;
            int tailSize;
            int chunkNum = Math.DivRem(data.Length, chunkSize, out tailSize);

            for (int i = 0; i < chunkNum; i++)
            {
                requestStream.Write(data, chunkSize * i, chunkSize);
            }

            if (tailSize > 0)
                requestStream.Write(data, chunkSize * chunkNum, tailSize);

        }

        WebResponse wresp = wReq.GetResponse();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(wresp.GetResponseStream()))
        {
            result = sr.ReadToEnd();
        }

    }
  • Can you try uploading using RPC? Refer this article: link. Steve has mentioned how to do this using RPC. – Akhoy Jul 16 '15 at 8:54
  • Is it supported for SharePoint onPremise? – User5590 Jul 16 '15 at 8:58
  • Yes it is supported. Just remove the part where he has used SharePointOnlineCredentials. – Akhoy Jul 16 '15 at 9:04
  • Let me try, will let you know the result – User5590 Jul 16 '15 at 9:07
  • Again same Time out error. Do i need to update anything in webservice? – User5590 Jul 16 '15 at 9:36
0

Have you tried upload using this code?

ClientContext context = new ClientContext("URL of the site collection");

Web oWeb = context.Web;
FileCreationInformation oFile = new FileCreationInformation();
oFile.Content = System.IO.File.ReadAllBytes(@"C:\Work\Files\17580_FAST2010_S05_Administration.pptx");
oFile.Url = "File_To_Be_Uploaded_to_SP.mp4";
List docs = oWeb.Lists.GetByTitle("Documents");
Microsoft.SharePoint.Client.File File2BeUploaded = docs.RootFolder.Files.Add(oFile);
context.Load(File2BeUploaded);
context.ExecuteQuery();

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.