private static string Migrate(string pagingInfo, MigrationMapping mapping) { string _pagingInfo = null; SPQuery query = new SPQuery { RowLimit = LIST_ROW_LIMIT, ViewFields = "<FieldRef Name='ContentTypeId'/>" + String.Join("", mapping.FieldMapping.Select(m => string.Format("<FieldRef Name='{0}'/>", m.ListField.InternalName))), Query = "<OrderBy><FieldRef Name='ID'/></OrderBy>" }; if (!string.IsNullOrEmpty(pagingInfo)) { query.ListItemCollectionPosition = new SPListItemCollectionPosition(pagingInfo); } Dictionary <int, int> usersCache = new Dictionary <int, int>(); usersCache.Add(1073741823, 1); // System Account !!!!!!! int eventType = (mapping.Table.Id == TableIDs.PLANNED_ROSTERS) ? 0 : 1; RosterDataService _dataService = new RosterDataService(); SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite eSite = new SPSite(mapping.List.ParentWeb.Url)) using (SPWeb eWeb = eSite.OpenWeb()) { Dictionary <int, string> errors = new Dictionary <int, string>(); SPListItemCollection items = eWeb.Lists[mapping.List.ID].GetItems(query); foreach (SPListItem itm in items) { var rosterEvent = _dataService.CreateRosterEvent(mapping.Table.Id, eventType); try { foreach (var mapEl in mapping.FieldMapping) { if (itm[mapEl.ListField.InternalName] == null) { continue; } object _val = null; if (mapEl.ListField.Type == SPFieldType.Lookup || mapEl.ListField.TypeAsString == "DualLookup") { var lookupVal = (mapEl.AllowMultipleValues) ? itm.GetLookupValueCollection(mapEl.ListField)[0] : itm.GetLookupValue(mapEl.ListField); _val = (lookupVal.LookupId.ToInt() == 0 || lookupVal.LookupValue == null) ? null : (int?)lookupVal.LookupId; } else if (mapEl.ListField.Type == SPFieldType.User) { SPFieldUserValue userVal = (mapEl.AllowMultipleValues) ? (itm[mapEl.ListField.InternalName] as SPFieldUserValueCollection)[0] : new SPFieldUserValue(eWeb, itm[mapEl.ListField.InternalName].ToString()); if (usersCache.ContainsKey(userVal.LookupId)) { // get value from cache _val = usersCache[userVal.LookupId]; } else { var dbUserFld = mapEl.TableDbColumn as DbFieldUser; if (dbUserFld == null) { _val = userVal.LookupValue; } else { int _valInt = dbUserFld.EnsureUser(userVal).RosterLookupId; usersCache.Add(userVal.LookupId, _valInt); _val = _valInt; } } } else if (mapEl.ListField.Type == SPFieldType.MultiChoice) { SPFieldMultiChoiceValue vals = new SPFieldMultiChoiceValue(itm[mapEl.ListField.InternalName].ToString()); List <string> choiceVals = new List <string>(); for (int k = 0; k < vals.Count; k++) { choiceVals.Add(vals[k]); } _val = choiceVals.ListToXml(); } else { _val = itm[mapEl.ListField.InternalName]; } rosterEvent.RosterEventDictionary[mapEl.TableColumn.InternalName] = _val; } rosterEvent.RosterEventDictionary[FieldNames.CONTENT_TYPE_ID] = mapping.ContentTypeMapping.Where(ct => ct.ListCtId == itm["ContentTypeId"].ToString()).Select(ct => ct.TableCtId).FirstOrDefault(); _dataService.SaveRosterEvent(rosterEvent, mapping.Table.Id); } catch (Exception ex) { //StringBuilder exMsg = new StringBuilder(); //exMsg.AppendFormat("Exception: {0};\nMessage: {1};\nStackTrace: {2}", ex.GetType().Name, ex.Message, ex.StackTrace); //Exception innerEx = ex; //while (innerEx.InnerException != null) { // innerEx = innerEx.InnerException; // exMsg.AppendFormat("\n\nInnerException: {0};\nMessage: {1};\nStackTrace: {2}", innerEx.GetType().Name, innerEx.Message, innerEx.StackTrace); //} errors.Add(itm.ID, "Error: " + ex.Message + ". Dict: " + String.Join(";", rosterEvent.RosterEventDictionary.Select(x => string.Format("[{0}]:{1}", x.Key, x.Value)))); } } if (errors.Any()) { throw new Exception("Errors:<br/>" + String.Join("<br/>", errors.Select(er => string.Format(" ListItemID: {0}. {1}", er.Key, er.Value)))); } _pagingInfo = items.ListItemCollectionPosition == null ? null : items.ListItemCollectionPosition.PagingInfo; //Paged=TRUE&p_ID=100 } }); return(_pagingInfo); }
public static MigrateResult MigrateItems(MigrateQuery query) { MigrateResult result = new MigrateResult { Success = true }; try { bool needValidation = (string.IsNullOrEmpty(query.PaginInfo)); RosterConfigService _configSrv = new RosterConfigService(); SPList shpList = SPContext.Current.Web.Lists[new Guid(query.Mapping.ListId)]; ListMetadata dbList = _configSrv.GetList(new Guid(query.Mapping.TableId)); var dbList_Fields = dbList.ListMetadataFields; var _globalMapping = _configSrv.GetMapping(); // collect and validate MigrationMapping mapping = new MigrationMapping(shpList, dbList); mapping.ContentTypeMapping.AddRange(query.Mapping.ContentTypeMapping.Select(m => new MigrationCtMappingElem { ListCtId = m.ListCtId, TableCtId = m.TableCtId.ToInt() })); foreach (var mItm in query.Mapping.FieldMapping) { ListMetadataField dbField = dbList_Fields.FirstOrDefault(fld => fld.InternalName.Equals(mItm.TableColumnName)); SPField listField = shpList.Fields.GetFieldByInternalName(mItm.ListFieldName); if (needValidation && dbField.DataSourceType == (int)LookupSourceType.Table && !(listField is SPFieldUser)) { SPFieldLookup shpFieldLookup = listField as SPFieldLookup; if (shpFieldLookup == null) { throw new Exception("You cannot map non-lookup field " + mItm.ListFieldName + " to Lookup column"); } var _lookupFieldMapping = _globalMapping.FirstOrDefault(m => m.ListName == new Guid(shpFieldLookup.LookupList).ToString()); if (_lookupFieldMapping == null || _lookupFieldMapping.TableName != dbField.DataSource) { throw new Exception(listField.Title + " field error. Mapping does not exist or Lookup lists don't match!"); } } bool listField_isMultiple = (listField.Type == SPFieldType.Lookup && (listField as SPFieldLookup).AllowMultipleValues) || (listField.Type == SPFieldType.User && (listField as SPFieldLookup).AllowMultipleValues) || (listField.TypeAsString == "DualLookup" && (listField as SPFieldLookup).AllowMultipleValues); mapping.FieldMapping.Add(new MigrationMappingElem { ListField = listField, AllowMultipleValues = listField_isMultiple, TableColumn = dbField, TableDbColumn = dbField.GetDbField() }); } if (needValidation) { var repeatedShPFields = mapping.FieldMapping.GroupBy(x => x.TableColumn.InternalName).Where(gr => gr.Count() > 1).Select(gr => gr.Key); if (repeatedShPFields.Any()) { throw new Exception("Following DB columns selected more than once: " + string.Join(",", repeatedShPFields)); } } double step = Math.Round(((LIST_ROW_LIMIT * 100) / (double)mapping.List.ItemCount), 2); string _pagingInfo = Migrate(query.PaginInfo, mapping); result.PagingInfo = (string)_pagingInfo ?? string.Empty; result.CurrentIndex = (_pagingInfo == null) ? 100 : query.CurrentIndex + step; } catch (Exception ex) { result.Success = false; result.ErrorMessage = ex.Message; } return(result); }