示例#1
0
        /// <summary>
        /// Updates the given SPListItem with the value passed.
        /// This method does not call Update or SystemUpdate.
        /// </summary>
        /// <param name="item">The SharePoint list item to update.</param>
        /// <param name="fieldValueInfo">The value information to be updated in the SPListItem.</param>
        public void WriteValueToListItem(SPListItem item, FieldValueInfo fieldValueInfo)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            if (fieldValueInfo == null || fieldValueInfo.FieldInfo == null)
            {
                throw new ArgumentNullException("fieldValueInfo");
            }

            IBaseValueWriter valueWriter = this.GetWriter(fieldValueInfo);

            valueWriter.WriteValueToListItem(item, fieldValueInfo);
        }
        /// <summary>
        /// Extracts the values from the entity to fill the values.
        /// </summary>
        /// <typeparam name="T">The type of the entity.</typeparam>
        /// <param name="entity">The entity.</param>
        /// <param name="listItem">The list item.</param>
        public void FromEntity <T>(T entity, SPListItem listItem)
        {
            var schema         = this.entitySchemaFactory.GetSchema(typeof(T));
            var listItemFields = listItem.Fields;

            foreach (var binding in schema.PropertyConversionDetails.ToList().Where(x => x.BindingType == BindingType.Bidirectional || x.BindingType == BindingType.WriteOnly))
            {
                var valueFromEntity     = binding.EntityProperty.GetValue(entity, null);
                IBaseValueWriter writer = binding.ValueWriter;

                // Create a MinimalFieldInfo<TValueType> to feed into the FieldValueInfo needed to
                // interact with IBaseValueWriter
                var     minimalFieldInfoType = typeof(MinimalFieldInfo <>).MakeGenericType(writer.AssociatedValueType);
                string  fieldInternalName    = binding.ValueKey;
                SPField itemField            = listItemFields.GetFieldByInternalName(fieldInternalName);
                var     minimalFieldInfo     = (BaseFieldInfo)Activator.CreateInstance(minimalFieldInfoType, new object[] { fieldInternalName, itemField.Id });
                var     fieldValueInfo       = new FieldValueInfo(minimalFieldInfo, valueFromEntity);

                // Update the list item through the IBaseValueWriter
                writer.WriteValueToListItem(listItem, fieldValueInfo);
            }
        }