0

Through CSOM we are trying to upload some 500 PDF files and set their Metadata. The code works fine for 4 or 5 files. However after that it throws exception

The underlying connection was closed: An unexpected error occurred on a receive.

Below is my code

using (clientContext = new Microsoft.SharePoint.Client.ClientContext(Helper.GetConfigValue("WebAppURL"))) { web = clientContext.Web;

                                NetworkCredential _myCredentials = new NetworkCredential(
                                                                                        Helper.GetConfigValue("SPAdminUserName"),
                                                                                        Helper.GetConfigValue("SPAdminPassword"),
                                                                                        Helper.GetConfigValue("Domain")
                                                                                        );

                                clientContext.Credentials = _myCredentials;
                                clientContext.RequestTimeout = int.MaxValue;

                                //Get Class Library path
                                Microsoft.SharePoint.Client.Folder docSetFolder = web.GetFolderByServerRelativeUrl(targetFolder);
                                clientContext.ExecuteQuery();

                                string fileUrl = targetFolder + filename;
                                string relativeFileUrl = (new Uri(fileUrl)).PathAndQuery;


                                Microsoft.SharePoint.Client.File file;

                                Microsoft.SharePoint.Client.List lst = clientContext.Web.Lists.GetByTitle(Helper.GetConfigValue("ClsLibName"));


                                //Upload Document
                                Microsoft.SharePoint.Client.FileCreationInformation fci = new Microsoft.SharePoint.Client.FileCreationInformation();
                                fci.Url = fileUrl;
                                fci.Content = System.IO.File.ReadAllBytes(string.Concat(Helper.GetConfigValue("DocumentsPath"), filename));
                                fci.Overwrite = true;
                                Microsoft.SharePoint.Client.FileCollection documentFiles = docSetFolder.Files;
                                clientContext.Load(documentFiles);
                                clientContext.ExecuteQuery();
                                Microsoft.SharePoint.Client.File newFile = documentFiles.Add(fci);
                                clientContext.Load(newFile);
                                clientContext.ExecuteQuery();

                                //Set new record's Properties
                                Microsoft.SharePoint.Client.ListItem item = newFile.ListItemAllFields;
                                clientContext.Load(item);

                                item[CustomStrings.ListFields.Empno] = r.Cells[CustomStrings.ListFields.Empno].Value.ToString();
                                item[CustomStrings.ListFields.EName] = r.Cells[CustomStrings.ListFields.EName].Value.ToString();
                                item[CustomStrings.ListFields.EmailAddress] = r.Cells[CustomStrings.ListFields.EmailAddress].Value.ToString();
                                item[CustomStrings.ListFields.OtherLink1] = r.Cells[CustomStrings.ListFields.OtherLink1].Value.ToString();
                                item[CustomStrings.ListFields.OtherLink2] = r.Cells[CustomStrings.ListFields.OtherLink2].Value.ToString();
                                item[CustomStrings.ListFields.EmailPlaceHolder1] = r.Cells[CustomStrings.ListFields.EmailPlaceHolder1].Value.ToString();
                                item[CustomStrings.ListFields.EmailPlaceHolder2] = r.Cells[CustomStrings.ListFields.EmailPlaceHolder2].Value.ToString();
                                item[CustomStrings.ListFields.EmailPlaceHolder3] = r.Cells[CustomStrings.ListFields.EmailPlaceHolder3].Value.ToString();
                                item[CustomStrings.ListFields.EmailPlaceHolder4] = r.Cells[CustomStrings.ListFields.EmailPlaceHolder4].Value.ToString();
                                item[CustomStrings.ListFields.EmailPlaceHolder5] = r.Cells[CustomStrings.ListFields.EmailPlaceHolder5].Value.ToString();
                                item[CustomStrings.ListFields.ReportingSupervisorPosition] = r.Cells[CustomStrings.ListFields.ReportingSupervisorPosition].Value.ToString();
                                item[CustomStrings.ListFields.ReportingSupervisor] = r.Cells[CustomStrings.ListFields.ReportingSupervisor].Value.ToString();
                                item[CustomStrings.ListFields.Department] = r.Cells[CustomStrings.ListFields.Department].Value.ToString();
                                item[CustomStrings.ListFields.ConfirmationTitle] = r.Cells[CustomStrings.ListFields.ConfirmationTitle].Value.ToString();
                                //item[CustomStrings.ListFields.EffectiveDate] = DateTime.ParseExact(r.Cells[CustomStrings.ListFields.EffectiveDate].Value.ToString(), "dd-MM-yyyy", CultureInfo.InvariantCulture);
                                item[CustomStrings.ListFields.EffectiveDate] = Convert.ToDateTime(r.Cells[CustomStrings.ListFields.EffectiveDate].Value.ToString());

                                item.Update();
                                clientContext.ExecuteQuery();

                                status = CustomStrings.RecordProcessedMessage;
                                SetBackColor(r, true);

                                r.Cells["Status"].Value = status;

                            }
  • Any thought...? Do i need to share more info. Due to emergency of the task I had eventually implemented above program using Server Object Model. It works fine for large number of files there. Is is that CSOM can be used for more files upload....? – shoab Jan 2 '18 at 7:27
  • 1
    Have you tried Microsoft.SharePoint.Client.File.SaveBinaryDirect with clientContext.PendingRequest.RequestExecutor.WebRequest.UnsafeAuthenticatedConnectionSharing = true;? By the way, CSOM uses WCF services to talk to SharePoint so in your case server object model is a good choice. Moreover WCF runs over http which is an extra layer. Server object model apis talk directly to SQL Server SharePoint using binary code. – Ahmad Zia Jan 2 '18 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.