Пример #1
0
        private void AssignPropertiesToListItem <T>(T entity, ListItem listItem)
        {
            Type entityType = typeof(T);

            foreach (var property in entityType.GetProperties())
            {
                try
                {
                    if (!property.CanRead)
                    {
                        continue;
                    }
                    if (property.GetCustomAttribute <IgnoreOnInsertAttribute>() != null)
                    {
                        continue;
                    }
                    var value = EntityHelper.GetItemValueFromProperty(property, entity);
                    if (property.PropertyType == typeof(DateTime))
                    {
                        var year = ((DateTime)value).Year;
                        if (year < 1900 || year > 8900)
                        {
                            throw new ArgumentOutOfRangeException("SharePoint-Datetime must be within 1900 and 8900");
                        }
                    }
                    if (value != null)
                    {
                        listItem[EntityHelper.GetInternalNameFromProperty(property)] = value;
                    }
                }
                catch (Exception ex)
                {
                    ex.Data.Add("Propertyname", property.Name);
                    ex.Data.Add("Listname", listItem);
                    throw (ex);
                }
            }
        }
Пример #2
0
        public void UpdateItem <T>(T entity)
        {
            Type entityType = typeof(T);

            try
            {
                PropertyInfo idProperty = entityType.GetProperty(AweCsomeField.SuffixId);
                if (idProperty == null)
                {
                    throw new FieldMissingException("Field 'Id' is required for Update-Operations on Lists", AweCsomeField.SuffixId);
                }
                int?idValue = idProperty.GetValue(entity) as int?;
                if (!idValue.HasValue)
                {
                    throw new FieldMissingException("Field 'Id' is has no value. Update failed", AweCsomeField.SuffixId);
                }
                string listName = EntityHelper.GetInternalNameFromEntityType(entityType);
                using (var clientContext = GetClientContext())
                {
                    Web            web            = clientContext.Web;
                    ListCollection listCollection = web.Lists;
                    clientContext.Load(listCollection);
                    clientContext.ExecuteQuery();
                    List list = listCollection.FirstOrDefault(q => q.Title == listName);
                    if (list == null)
                    {
                        throw new ListNotFoundException();
                    }
                    ListItem existingItem = list.GetItemById(idValue.Value);
                    foreach (var property in entityType.GetProperties())
                    {
                        if (!property.CanRead)
                        {
                            continue;
                        }
                        if (property.GetCustomAttribute <IgnoreOnUpdateAttribute>() != null)
                        {
                            continue;
                        }
                        existingItem[EntityHelper.GetInternalNameFromProperty(property)] = EntityHelper.GetItemValueFromProperty(property, entity);
                    }
                    existingItem.Update();
                    clientContext.ExecuteQuery();
                }
            }
            catch (Exception ex)
            {
                _log.Error($"Cannot update data from entity of type '{entityType.Name}'", ex);
                throw;
            }
        }