Beispiel #1
0
        private void EnsureFieldImpl(Field fieldInfo)
        {
            if (ContainsFieldImpl(fieldInfo.Property))
            {
                return;
            }

            if (fieldInfo.Type == SPFieldType.Lookup)
            {
                if (string.IsNullOrEmpty(fieldInfo.LookupList))
                {
                    throw new SharepointCommonException(string.Format("LookupList must be set for lookup fields. ({0})", fieldInfo.Name));
                }

                var lookupList = ParentWeb.Web.TryGetListByNameOrUrlOrId(fieldInfo.LookupList);

                if (lookupList == null)
                {
                    throw new SharepointCommonException(string.Format("List {0} not found on {1}", fieldInfo.LookupList, ParentWeb.Web.Url));
                }

                List.Fields.AddLookup(fieldInfo.Name, lookupList.ID, false);
            }
            else
            {
                var customPropAttrs = (CustomPropertyAttribute[])Attribute.GetCustomAttributes(fieldInfo.Property, typeof(CustomPropertyAttribute));

                var sb = new StringBuilder();
                var xv = new XmlTextWriter(new StringWriter(sb));
                xv.WriteStartElement("Field");

                xv.WriteAttributeString("ID", Guid.NewGuid().ToString());

                var type = "";
                if (fieldInfo.Type == SPFieldType.Invalid && fieldInfo.FieldAttribute.FieldProvider != null)
                {
                    type = fieldInfo.FieldAttribute.FieldProvider.FieldTypeAsString;
                }
                else
                {
                    var typeAttr = customPropAttrs.FirstOrDefault(cp => cp.Name == "Type");
                    if (typeAttr != null)
                    {
                        type = typeAttr.Value;
                    }
                    else
                    {
                        type = fieldInfo.Type.ToString();
                    }
                }
                xv.WriteAttributeString("Type", type);


                xv.WriteAttributeString("DisplayName", fieldInfo.Name);
                xv.WriteAttributeString("Name", fieldInfo.Name);

                foreach (var customProp in customPropAttrs.Where(cp => cp.Name != "Type"))
                {
                    xv.WriteAttributeString(customProp.Name, customProp.Value);
                }

                xv.WriteEndElement();

                //  Mockable.AddFieldAsXml(List.Fields, sb.ToString());
                List.Fields.AddFieldAsXml(sb.ToString());
            }

            // var field = Mockable.GetFieldByInternalName(List.Fields, fieldInfo.Name);
            var field = List.Fields.GetFieldByInternalName(fieldInfo.Name);


            //Mockable.FieldMapper_SetFieldProperties(field, fieldInfo);
            FieldMapper.SetFieldProperties(field, fieldInfo);
        }