0
votes

I'm uploading data from csv file which has about 200k records to sharepoint list, code works fine but I get this error after code has run for about 2hrs or so.

Cannot complete this action.

Please try again. at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
at Microsoft.SharePoint.Library.SPRequest.ProcessBatchData(String bstrUrl, String bstrData, ISPDataCallback pResultCallback) at Microsoft.SharePoint.SPWeb.ProcessBatchData(String strBatchData) at UploadIrisData.Program.Dof1FieldsBatchUpdate[T](IEnumerable`1 memberList, SPList list, StringBuilder methodBuilder, SPWeb web, SPQuery query, String createquery, String batchFormat) HandleComException at offset 27818482 in file:line:column :0:0

  private static void Dof1FieldsBatchUpdate<T>(IEnumerable<T> memberList, SPList list, StringBuilder methodBuilder, SPWeb web,
        SPQuery query, string createquery, string batchFormat)
    {
        string batch = String.Empty;
        string listGuid = list.ID.ToString();
        List<f1fields> f1Fieldses = (List<f1fields>)memberList;
        query.Query = "<Where>" + CreateDynamicQuery<T>(memberList.ToList(), createquery) + "</Where>";

        query.ViewAttributes = "Scope='Recursive'";

        query.RowLimit = 1000;
        try
        {
            do
            {
                SPListItemCollection items = list.GetItems(query);

                string methodFormat = "<Method ID=\"{0}\">" +
                            "<SetList>{1}</SetList>" +
                            "<SetVar Name=\"Cmd\">Save</SetVar>" +
                            "<SetVar Name=\"ID\">{2}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#Title\">{3}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLContractNumber\">{4}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLClubName\">{5}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLEmailHome\">{6}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLEmailWork\">{7}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLFaxNumber\">{9}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLHomePhone\">{10}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLIDNumber\">{11}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLLegalCategory\">{12}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLMemberEffectiveYear\">{13}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLMemberName\">{14}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLMemberStatus\">{15}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPassportNumber\">{16}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPasswordRequired\">{17}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPhysicalHomeAddress1\">{18}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPhysicalHomeAddress2\">{19}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPhysicalHomeAddress3\">{20}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPhysicalHomeAddress4\">{21}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPhysicalHomePostalCode\">{22}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPhysicalWorkAddress1\">{23}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPhysicalWorkAddress2\">{24}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPhysicalWorkAddress3\">{25}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPhysicalWorkAddress4\">{26}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPhysicalWorkPostalCode\">{27}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPostalHomeAddress1\">{28}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPostalHomeAddress2\">{29}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPostalHomeAddress3\">{30}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPostalHomeAddress4\">{31}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPostalHomePostalCode\">{32}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLWorkPhone\">{33}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLCompanyKey\">{34}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPostalWorkAddress1\">{35}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPostalWorkAddress2\">{36}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPostalWorkAddress3\">{37}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPostalWorkAddress4\">{38}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLPostalWorkPostalCode\">{39}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLCellphone\">{40}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLAnnualFeesTotalOverdue\">{41}</SetVar>" +
                            "<SetVar Name=\"urn:schemas-microsoft-com:office:office#CLContractTotalOverdue\">{42}</SetVar>" +
                            "</Method>";

                var excludedMemberIDs = new HashSet<string>(f1Fieldses.Select(r => r.MemberNumber));
                var memberNumbersFoundInList = new List<string>();

                //build the CAML update commands
                if (items.Count > 0)
                {
                    for (var i = 0; i < items.Count; i++)
                    {
                        var memberId = items[i].ID;
                        memberNumbersFoundInList.Add(items[i].Title);
                        int i1 = i;
                        foreach (var member in f1Fieldses.Where(member => items[i1].Title == member.MemberNumber))
                        {
                            methodBuilder.AppendFormat(methodFormat, memberId, listGuid, memberId,
                                member.MemberNumber, member.ContractNumber, member.ClubName, member.EmailHome, member.EmailWork,
                                member.HistoricalNumber, member.FaxNumber, member.HomePhone, member.IdNumber, member.LegalCategory,
                                member.MemberEffectiveYear, member.MemberName, member.MemberStatus,
                                member.PassportNumber, member.PasswordRequired, member.PhysicalHomeAddress1,
                                member.PhysicalHomeAddress2, member.PhysicalHomeAddress3,
                                member.PhysicalHomeAddress4, member.PhysicalHomePostalCode,
                                member.PhysicalWorkAddress1, member.PhysicalWorkAddress2,
                                member.PhysicalWorkAddress3, member.PhysicalWorkAddress4,
                                member.PhysicalWorkPostalCode,
                                member.PostalHomeAddress1, member.PostalHomeAddress2, member.PostalHomeAddress3,
                                member.PostalHomeAddress4, member.PostalHomePostalCode, member.WorkPhone, member.CompanyKey,
                                member.PostalWorkAddress1, member.PostalWorkAddress2, member.PostalWorkAddress3, member.PostalWorkAddress4,
                                member.PostalWorkPostalCode, member.CellPhone, member.AnnualFeesTotal, member.ContractTotalOverdue
                                );

                        }
                        //memberToWriteToLog.Add(items[i].Title);
                    }
                }
                var result = f1Fieldses.Where(p => !memberNumbersFoundInList.Contains(p.MemberNumber));
                IEnumerable<f1fields> f1FieldsesUpdateNew = result as IList<f1fields> ?? result.ToList();
                //if (f2FieldsesUpdateNew.Any()) Debugger.Break();

                // New member numbers to be add to the list
                var newMembersWithoutDuplicates = new List<f1fields>();
                var duplicatesMemberNumber = new List<f1fields>();

                //Find duplicates before batch update
                foreach (var s in f1FieldsesUpdateNew)
                {
                    var temp = newMembersWithoutDuplicates.Find(x=>x.MemberNumber.Contains(s.MemberNumber));
                    if (temp == null)
                        newMembersWithoutDuplicates.Add(s);
                    else
                    {
                        duplicatesMemberNumber.Add(s);
                    }
                }


                if (newMembersWithoutDuplicates.Any())
                {
                    var memberId = 1;
                    foreach (var member in newMembersWithoutDuplicates)
                    {
                        methodBuilder.AppendFormat(methodFormat, memberId, listGuid, "New",
                                member.MemberNumber, member.ContractNumber, member.ClubName, member.EmailHome, member.EmailWork,
                                member.HistoricalNumber, member.FaxNumber, member.HomePhone, member.IdNumber, member.LegalCategory,
                                member.MemberEffectiveYear, member.MemberName, member.MemberStatus,
                                member.PassportNumber, member.PasswordRequired, member.PhysicalHomeAddress1,
                                member.PhysicalHomeAddress2, member.PhysicalHomeAddress3,
                                member.PhysicalHomeAddress4, member.PhysicalHomePostalCode,
                                member.PhysicalWorkAddress1, member.PhysicalWorkAddress2,
                                member.PhysicalWorkAddress3, member.PhysicalWorkAddress4,
                                member.PhysicalWorkPostalCode,
                                member.PostalHomeAddress1, member.PostalHomeAddress2, member.PostalHomeAddress3,
                                member.PostalHomeAddress4, member.PostalHomePostalCode, member.WorkPhone, member.CompanyKey,
                                member.PostalWorkAddress1, member.PostalWorkAddress2, member.PostalWorkAddress3, member.PostalWorkAddress4,
                                member.PostalWorkPostalCode, member.CellPhone, member.AnnualFeesTotal, member.ContractTotalOverdue
                                );
                        memberId++;
                    }
                }
                web.AllowUnsafeUpdates = true;
                batch = string.Format(batchFormat, methodBuilder.ToString());

                // Process the batch of commands
                web.ProcessBatchData(batch);
                //web.AllowUnsafeUpdates = false;
                methodBuilder.Length = 0;
                if (duplicatesMemberNumber.Any())
                {
                    SPQuery query2 = new SPQuery();
                    string createquery2 = "";
                    Dof1FieldsBatchUpdate(duplicatesMemberNumber, list, methodBuilder, web, query2, createquery2, batchFormat);
                }

            } while (query.ListItemCollectionPosition != null);
        }
        catch (Exception ex)
        {
            using (FileStream fs = new FileStream("c:\\error.txt", FileMode.Append, FileAccess.Write))
            using (StreamWriter sw = new StreamWriter(fs))
            {
                // Get stack trace for the exception with source file information
                var st = new StackTrace(ex, true);
                // Get the top stack frame
                var frame = st.GetFrame(0);
                // Get the line number from the stack frame
                var line = frame.GetFileLineNumber();
                sw.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + frame + Environment.NewLine + line);
            }
        }
    }
1

1 Answers

0
votes

I had the same exception and the reason was an unescaped <div> tag that was breaking the xml structure. Ensure that your data is properly inserted in the <SetVar> tags.

If this is not the case I have read that too big batch update xml may result in an exception.