Example #1
0
        private string GetFieldCreationXml(PropertyInfo property, Dictionary <string, Guid> lookupTableIds)
        {
            Type propertyType = property.PropertyType;

            string internalName = EntityHelper.GetInternalNameFromProperty(property);
            string displayName  = EntityHelper.GetDisplayNameFromProperty(property);
            string description  = EntityHelper.GetDescriptionFromEntityType(propertyType);


            bool   isRequired    = PropertyIsRequired(property);
            bool   isUnique      = IsTrue(propertyType.GetCustomAttribute <UniqueAttribute>()?.IsUnique);
            string fieldTypeName = EntityHelper.GetFieldType(property);
            bool   isMulti       = IsMulti(propertyType);

            if (fieldTypeName == nameof(FieldType.Lookup))
            {
                EntityHelper.RemoveLookupIdFromFieldName(isMulti, ref internalName, ref displayName);
            }

            GetFieldCreationAdditionalXmlForFieldType(fieldTypeName, property, lookupTableIds, out string fieldAttributes, out string fieldAdditional);
            string fieldTypeString = fieldTypeName.ToString();

            if (fieldAttributes == null)
            {
                fieldAttributes = string.Empty;
            }
            if (fieldAttributes == null)
            {
                fieldAdditional = string.Empty;
            }
            if (isMulti)
            {
                if (fieldTypeName == nameof(FieldType.Choice))
                {
                    fieldTypeString = "Multi" + fieldTypeString;
                }
                else
                {
                    fieldAttributes += " Mult='TRUE'";
                    fieldTypeString += "Multi";
                }
            }
            var indexAttribute = property.GetCustomAttribute <IndexAttribute>();

            if (indexAttribute != null)
            {
                fieldAttributes += " Indexed='TRUE'";
            }

            string csomCreateCaml = $"<Field Type='{fieldTypeString}' Name='{internalName}' DisplayName='{displayName}' StaticName='{internalName}'";

            if (isRequired)
            {
                csomCreateCaml += " Required='TRUE'";
            }
            if (isUnique)
            {
                csomCreateCaml += " EnforceUniqueValues='TRUE'";
            }
            csomCreateCaml += $" {fieldAttributes}";
            if (string.IsNullOrWhiteSpace(fieldAdditional))
            {
                csomCreateCaml += "/>";
            }
            else
            {
                csomCreateCaml += ">";
                csomCreateCaml += fieldAdditional;
                csomCreateCaml += "</Field>";
            }
            return(csomCreateCaml);
        }
Example #2
0
 public Field GetFieldDefinition(List sharePointList, PropertyInfo property)
 {
     return(sharePointList.Fields.GetByInternalNameOrTitle(EntityHelper.GetInternalNameFromProperty(property)));
 }
Example #3
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;
            }
        }