Beispiel #1
0
        private void AddFields(frm_Data_Field fieldForm, string listId, string siteAddress)
        {
            Guid g = new Guid(listId);

            SP.ClientContext clientContext = SharePoint.GetClient(siteAddress, frm_Main_Menu.username, frm_Main_Menu.password);
            SP.List          oList         = clientContext.Web.Lists.GetById(g);

            // Load in the Fields
            clientContext.Load(oList.Fields);
            clientContext.ExecuteQuery();

            int i = 0;

            foreach (SP.Field oField in oList.Fields)
            {
                // Store the Field Type
                string fieldType = oField.TypeAsString;

                // We will exclude some entries by Field Type
                bool skip = false;

                // These 2 attributes will depend on the field type
                string maxLength = null;
                string formula   = null;

                // Find field types for specific actions
                switch (fieldType)
                {
                // Max Length
                case "Text":

                    // Convert to "Text Field"
                    SP.FieldText textField = (SP.FieldText)oField;

                    // We now have access to the attribute
                    maxLength = textField.MaxLength.ToString();

                    // Break out of the switch
                    break;

                // Formula
                case "Calculated":

                    // Convert to "Calculated Field"
                    FieldCalculated calcField = (FieldCalculated)oField;

                    // We now have access to the attribute
                    formula = calcField.Formula;

                    // Break out of the switch
                    break;

                // We won't include "Computed" or "Lookup" fields
                case "Lookup":
                case "Computed":

                    // Skip
                    skip = true;

                    // Break out of the switch
                    break;
                }

                if (skip == false)
                //if (true)
                {
                    i++;

                    string fieldName           = oField.Title;
                    string defaultValue        = oField.DefaultValue;
                    string enforceUniqueValues = oField.EnforceUniqueValues.ToString();
                    string required            = oField.Required.ToString();
                    string readOnly            = oField.ReadOnlyField.ToString();
                    string isSealed            = oField.Sealed.ToString();

                    fieldForm.AddRow
                    (
                        i,
                        fieldName,
                        oField.InternalName,
                        fieldType,
                        maxLength,
                        enforceUniqueValues,
                        required,
                        readOnly,
                        isSealed,
                        defaultValue,
                        formula
                    );

                    lbl_Row_Count.Text = i.ToString() + " record(s) found";
                    lbl_Row_Count.Refresh();
                }
            }
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        /// <summary>
        /// Tokenizes calculated fieldXml to use tokens for field references
        /// </summary>
        /// <param name="fields">the field collection that the field is contained within</param>
        /// <param name="field">the field to tokenize</param>
        /// <param name="fieldXml">the xml to tokenize</param>
        /// <returns></returns>
        internal static string TokenizeFieldFormula(Microsoft.SharePoint.Client.FieldCollection fields, FieldCalculated field, string fieldXml)
        {
            var schemaElement = XElement.Parse(fieldXml);

            var formulaElement = schemaElement.Descendants("Formula").FirstOrDefault();

            if (formulaElement != null)
            {
                field.EnsureProperty(f => f.Formula);

                var formulastring = field.Formula;

                if (formulastring != null)
                {
                    var fieldRefs = schemaElement.Descendants("FieldRef");
                    foreach (var fieldRef in fieldRefs)
                    {
                        var fieldInternalName = fieldRef.Attribute("Name").Value;
                        var referencedField   = fields.GetFieldByInternalName(fieldInternalName);
                        formulastring = formulastring.Replace($"[{referencedField.Title}]", $"[{{fieldtitle:{fieldInternalName}}}]");
                    }
                    var fieldRefParent = schemaElement.Descendants("FieldRefs");
                    fieldRefParent.Remove();

                    formulaElement.Value = formulastring;
                }
            }
            return(schemaElement.ToString());
        }
        /// <summary>
        /// Tokenizes calculated fieldXml to use tokens for field references
        /// </summary>
        /// <param name="fields">the field collection that the field is contained within</param>
        /// <param name="field">the field to tokenize</param>
        /// <param name="fieldXml">the xml to tokenize</param>
        /// <returns></returns>
        internal static string TokenizeFieldFormula(Microsoft.SharePoint.Client.FieldCollection fields, FieldCalculated field, string fieldXml)
        {
            var schemaElement = XElement.Parse(fieldXml);

            var formulaElement = schemaElement.Descendants("Formula").FirstOrDefault();

            if (formulaElement != null)
            {
                field.EnsureProperty(f => f.Formula);

                var formulastring = field.Formula;

                if (formulastring != null)
                {
                    var fieldRefs = schemaElement.Descendants("FieldRef");
                    foreach (var fieldRef in fieldRefs)
                    {
                        var fieldInternalName = fieldRef.Attribute("Name").Value;
                        var referencedField = fields.GetFieldByInternalName(fieldInternalName);
                        formulastring = formulastring.Replace(string.Format("[{0}]", referencedField.Title), string.Format("[{{fieldtitle:{0}}}]", fieldInternalName));
                    }
                    var fieldRefParent = schemaElement.Descendants("FieldRefs");
                    fieldRefParent.Remove();

                    formulaElement.Value = formulastring;
                }
            }
            return schemaElement.ToString();
        }