/// <summary> /// Creates a Field definition object from a SPML field definition. /// </summary> /// <param name="spml">SharePoint field definition in SPML.</param> /// <returns>Field definition object for the specified field.</returns> public static Field FromSpml(XmlNode spml) { // // Field object. // Field field = new Field(); // // General field information. // field.Name = spml.Attributes["Name"].Value; field.DisplayName = spml.Attributes["DisplayName"].Value; field.Id = new Guid(spml.Attributes["Id"].Value); field.SharePointType = spml.Attributes["Type"].Value; XmlAttribute description = spml.Attributes["Description"]; if (description != null) { field.Description = description.Value; } XmlAttribute alias = spml.Attributes["Alias"]; if (alias != null) { field.Alias = alias.Value; } // // Boolean values. // XmlAttribute hidden = spml.Attributes["Hidden"]; if (hidden != null) { field.IsHidden = Helpers.ParseBool(hidden.Value); } XmlAttribute readOnly = spml.Attributes["ReadOnly"]; if (readOnly != null) { field.IsReadOnly = Helpers.ParseBool(readOnly.Value); } XmlAttribute primaryKey = spml.Attributes["PrimaryKey"]; if (primaryKey != null) { field.IsPrimaryKey = Helpers.ParseBool(primaryKey.Value); } XmlAttribute calculated = spml.Attributes["Calculated"]; if (calculated != null) { field.IsCalculated = Helpers.ParseBool(calculated.Value); } XmlAttribute required = spml.Attributes["Required"]; if (required != null) { field.IsRequired = Helpers.ParseBool(required.Value); } // // Choices. // XmlAttribute fillInChoice = spml.Attributes["FillInChoice"]; if (fillInChoice != null) { field.FillInChoiceEnabled = Helpers.ParseBool(fillInChoice.Value); } XmlElement choices = spml["Choices"]; if (choices != null) { field.Choices = new List <Choice>(); foreach (XmlNode choice in choices.ChildNodes) { field.Choices.Add(Choice.FromSpml(choice)); } } // // Lookup. // XmlAttribute lookupList = spml.Attributes["LookupList"]; XmlAttribute lookupField = spml.Attributes["LookupField"]; if (lookupList != null) { field.LookupList = lookupList.Value; } if (lookupField != null) { field.LookupField = lookupField.Value; } // // Populate type information. // GetType(field); // // Return field definition object. // return(field); }
/// <summary> /// Creates a Field definition object from a CAML list definition. /// </summary> /// <param name="fieldDefinition">SharePoint list field definition.</param> /// <returns>Field definition object for the specified list field.</returns> public static Field FromCaml(XmlNode fieldDefinition) { // // Field object. Include by default. // Field field = new Field(); field.Include = true; // // Field ID (GUID). // XmlAttribute aID = fieldDefinition.Attributes["ID"]; if (aID != null) { field.Id = new Guid(aID.Value); } // // Field name. // field.Name = (string)fieldDefinition.Attributes["Name"].Value; field.DisplayName = (string)fieldDefinition.Attributes["DisplayName"].Value; // // Field description. // XmlAttribute aDescription = fieldDefinition.Attributes["Description"]; if (aDescription != null) { field.Description = (string)aDescription.Value; } // // Field hidden? // XmlAttribute aHidden = fieldDefinition.Attributes["Hidden"]; field.IsHidden = false; if (aHidden != null) { field.IsHidden = bool.Parse(aHidden.Value); } // // Field read-only? // XmlAttribute aReadOnly = fieldDefinition.Attributes["ReadOnly"]; field.IsReadOnly = false; if (aReadOnly != null) { field.IsReadOnly = bool.Parse(aReadOnly.Value); } // // Field type. // string type = (string)fieldDefinition.Attributes["Type"].Value; field.IsCalculated = false; // // Calculated field. Use underlying type for mapping. // if (type == "Calculated") { field.SharePointType = (string)fieldDefinition.Attributes["ResultType"].Value; field.IsCalculated = true; } else { field.SharePointType = type; } // // Primary key field should be imported as well. // XmlAttribute primaryKey = fieldDefinition.Attributes["PrimaryKey"]; field.IsPrimaryKey = false; if (primaryKey != null) { field.IsPrimaryKey = bool.Parse(primaryKey.Value); } // // Check whether the field is required or not. Will be used to make value types nullable if not required. // XmlAttribute aRequired = fieldDefinition.Attributes["Required"]; field.IsRequired = false; if (aRequired != null) { field.IsRequired = bool.Parse(aRequired.Value); } // // Choices. // if (field.SharePointType.EndsWith("Choice")) { field.Choices = new List <Choice>(); foreach (XmlNode choice in fieldDefinition["CHOICES"]) { field.Choices.Add(Choice.FromCaml(choice)); } // // Additional fill-in field needed if FillInChoice is set. // XmlAttribute fillInChoice = fieldDefinition.Attributes["FillInChoice"]; field.FillInChoiceEnabled = fillInChoice != null && bool.Parse((string)fillInChoice.Value); } // // Lookup. // if (field.SharePointType.StartsWith("Lookup")) { field.LookupList = (string)fieldDefinition.Attributes["List"].Value; field.LookupField = (string)fieldDefinition.Attributes["ShowField"].Value; } // // Populate type information. // GetType(field); // // Return field definition object. // return(field); }