public EnsureUserResult EnsureUser(EnsureUserQuery query)
        {
            DisableCaching();
            var result = new EnsureUserResult {
                RosterLookupId = -1,
                Key            = "",
                DisplayText    = ""
            };

            try {
                RosterConfigService configProvider = new RosterConfigService();
                ListMetadataField   field          = configProvider.GetField(query.FieldMetedataId.ToGuid());

                DbFieldUser dbFld = field.GetDbField() as DbFieldUser;
                var         user  = dbFld.EnsureUser(XDocument.Parse(query.XmlText));

                result.DisplayText    = user.DisplayText;
                result.Key            = user.Key;
                result.RosterLookupId = user.RosterLookupId;
            }
            catch (Exception ex) {
                HandleException(ex);
            }

            return(result);
        }
Example #2
0
        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);
        }