private static async Task <FieldUser> AddPeoplePickerField( this List list , string fieldInternalName , string fieldDisplayName , bool allowMultipleValues , FieldUserSelectionMode mode) { if (await list.ContainsField(fieldDisplayName)) { throw new Exception( $@"""{list.Title}"" list already have a ""{fieldDisplayName}"" field!"); } var clientContext = list.Context.AsClientContext(); var field = list.Fields.AddFieldAsXml( $"<Field Type='UserMulti' DisplayName='{fieldInternalName}'/>" , true , AddFieldOptions.AddFieldToDefaultView ); var userField = clientContext.CastTo <FieldUser>(field); userField.Title = fieldDisplayName; userField.Update(); userField.SelectionMode = mode; userField.AllowMultipleValues = allowMultipleValues; list.Update(); await clientContext.ExecuteQueryAsync(); return(userField); }
internal Field ApplyField(Field field) { if (field == null || Field == null) { return(field); } if (!field.IsPropertyAvailable("FieldTypeKind") || field.FieldTypeKind != Field.DataType) { if (!typeof(LookupFieldAttribute).IsAssignableFrom(Field.GetType())) { field.FieldTypeKind = Field.DataType; } } if (!field.IsPropertyAvailable("Title") || field.Title != Field.Title) { field.Title = Field.Title; } if (!field.IsPropertyAvailable("Required") || field.Required != Field.Required) { field.Required = Field.Required; } if (!field.IsPropertyAvailable("ReadOnlyField") || field.ReadOnlyField != Field.IsReadOnly) { field.ReadOnlyField = Field.IsReadOnly; } if (!field.IsPropertyAvailable("Group") || field.Group != Field.Group) { if (!string.IsNullOrEmpty(Field.Group)) { field.Group = Field.Group; } } if (!field.IsPropertyAvailable("Indexed") || field.Indexed != Field.Indexed) { field.Indexed = Field.Indexed; } if (!field.IsPropertyAvailable("Hidden") || field.Hidden != Field.Hidden) { field.Hidden = Field.Hidden; } if (!field.IsPropertyAvailable("Description") || field.Description != Field.Description) { if (!string.IsNullOrEmpty(Field.Description)) { field.Description = Field.Description; } } if (!field.IsPropertyAvailable("DefaultValue") || field.DefaultValue != Field.DefaultValue) { if (!string.IsNullOrEmpty(Field.DefaultValue)) { field.DefaultValue = Field.DefaultValue; } } if (!field.IsPropertyAvailable("EnforceUniqueValues") || field.EnforceUniqueValues != Field.EnforceUniqueValues) { field.EnforceUniqueValues = Field.EnforceUniqueValues; } switch (Field.DataType) { case FieldType.Calculated: { FieldCalculated calculatedField = field.Context.CastTo <FieldCalculated>(field); if (typeof(CalculatedFieldAttribute).IsAssignableFrom(Field.GetType())) { if (!calculatedField.IsPropertyAvailable("OutputType") || calculatedField.OutputType != (Field as CalculatedFieldAttribute).ResultType) { calculatedField.OutputType = (Field as CalculatedFieldAttribute).ResultType; } if (!calculatedField.IsPropertyAvailable("Formula") || calculatedField.Formula != (Field as CalculatedFieldAttribute).Formula) { calculatedField.Formula = (Field as CalculatedFieldAttribute).Formula; } } return(calculatedField); } case FieldType.Lookup: { bool allowMultipleValues = false; if (typeof(LookupFieldAttribute).IsAssignableFrom(Field.GetType())) { allowMultipleValues = (Field as LookupFieldAttribute).IsMultiple; } var lookupField = field.Context.CastTo <FieldLookup>(field); if (lookupField.IsPropertyAvailable("AllowMultipleValues") && lookupField.AllowMultipleValues != allowMultipleValues) { lookupField.AllowMultipleValues = allowMultipleValues; } Type lookupEntityType = null; if (_valueType != null && typeof(ISpEntityLookup).IsAssignableFrom(_valueType) || typeof(ISpEntityLookupCollection).IsAssignableFrom(_valueType)) { lookupEntityType = _valueType.GenericTypeArguments.FirstOrDefault(); } else if (typeof(IListItemEntity).IsAssignableFrom(_valueType)) { lookupEntityType = _valueType; } if (lookupEntityType != null) { if (!lookupField.IsPropertyAvailable("Id")) { lookupField.Update(); lookupField.Context.Load(lookupField); lookupField.Context.ExecuteQuery(); } SetLookupList(lookupField, lookupEntityType); } return(lookupField); } case FieldType.User: { bool allowMultipleValues = false; FieldUserSelectionMode userSelectionMode = FieldUserSelectionMode.PeopleOnly; if (typeof(LookupFieldAttribute).IsAssignableFrom(Field.GetType())) { allowMultipleValues = (Field as LookupFieldAttribute).IsMultiple; } if (typeof(UserFieldAttribute).IsAssignableFrom(Field.GetType())) { userSelectionMode = (Field as UserFieldAttribute).UserSelectionMode; } var userField = field.Context.CastTo <FieldUser>(field); if (userField.IsPropertyAvailable("AllowMultipleValues") && userField.AllowMultipleValues != allowMultipleValues) { userField.AllowMultipleValues = allowMultipleValues; } if (userField.IsPropertyAvailable("AllowMultipleValues") && userField.IsPropertyAvailable("SelectionMode") && userField.SelectionMode != userSelectionMode) { userField.SelectionMode = userSelectionMode; } return(userField); } case FieldType.Choice: case FieldType.MultiChoice: { string[] choices = null; if (_valueType.IsEnum) { choices = AttributeHelper.GetFieldAttributes <ChoiceAttribute>(_valueType).Select(choice => choice.Value) .OrderBy(choice => choice.Index).Select(choice => choice.Value).ToArray(); } bool isMultiple = false; if (Field is ChoiceFieldAttribute) { if ((Field as ChoiceFieldAttribute).IsMultiple) { isMultiple = true; var multiChoiceField = field.Context.CastTo <FieldMultiChoice>(field); if (!multiChoiceField.IsPropertyAvailable("Choices") || !multiChoiceField.Choices.SequenceEqual(choices)) { multiChoiceField.Choices = choices; } return(multiChoiceField); } } if (!isMultiple) { var choiceField = field.Context.CastTo <FieldChoice>(field); if (!choiceField.IsPropertyAvailable("Choices") || !choiceField.Choices.SequenceEqual(choices)) { choiceField.Choices = choices; } return(choiceField); } return(field); } case FieldType.Note: { var noteField = field.Context.CastTo <FieldMultiLineText>(field); if (Field is NoteFieldAttribute) { if (!noteField.IsPropertyAvailable("AllowHyperlink") || noteField.AllowHyperlink != (Field as NoteFieldAttribute).AllowHyperlink) { noteField.AllowHyperlink = (Field as NoteFieldAttribute).AllowHyperlink; } if (!noteField.IsPropertyAvailable("AppendOnly") || noteField.AppendOnly != (Field as NoteFieldAttribute).AppendOnly) { noteField.AppendOnly = (Field as NoteFieldAttribute).AppendOnly; } if (!noteField.IsPropertyAvailable("NumberOfLines") || noteField.NumberOfLines != (Field as NoteFieldAttribute).NumberOfLines) { if ((Field as NoteFieldAttribute).NumberOfLines > 0) { noteField.NumberOfLines = (Field as NoteFieldAttribute).NumberOfLines; } } if (!noteField.IsPropertyAvailable("RestrictedMode") || noteField.RestrictedMode != (Field as NoteFieldAttribute).RestrictedMode) { noteField.RestrictedMode = (Field as NoteFieldAttribute).RestrictedMode; } if (!noteField.IsPropertyAvailable("RichText") || noteField.RichText != (Field as NoteFieldAttribute).RichText) { noteField.RichText = (Field as NoteFieldAttribute).RichText; } #if !SP2013 && !SP2016 if (!noteField.IsPropertyAvailable("UnlimitedLengthInDocumentLibrary") || noteField.UnlimitedLengthInDocumentLibrary != (Field as NoteFieldAttribute).UnlimitedLengthInDocumentLibrary) { noteField.UnlimitedLengthInDocumentLibrary = (Field as NoteFieldAttribute).UnlimitedLengthInDocumentLibrary; } #endif } return(noteField); } case FieldType.Text: var textField = field.Context.CastTo <FieldText>(field); if (Field is TextFieldAttribute) { if (!textField.IsPropertyAvailable("MaxLength") || textField.MaxLength != (Field as TextFieldAttribute).MaxLength) { if ((Field as TextFieldAttribute).MaxLength > 0) { textField.MaxLength = (Field as TextFieldAttribute).MaxLength; } } } return(textField); } return(field); }
/// <summary> /// Creates a UserFieldCreationInformation /// </summary> /// <param name="displayName"></param> /// <param name="fieldUserSelectionMode"></param> /// <param name="required"></param> public UserFieldCreationInformation(string displayName, FieldUserSelectionMode fieldUserSelectionMode, bool required = false) : base(displayName, FieldType.User, required) { FieldUserSelectionMode = fieldUserSelectionMode; }