示例#1
0
        private static FieldCreationOptions FieldBooleanOptionsToCreation(string title, FieldBooleanOptions options)
        {
            FieldCreationOptions creationOptions = new FieldCreationOptions(FieldType.Boolean);

            creationOptions.ImportFromCommonFieldOptions(title, options);
            return(creationOptions);
        }
示例#2
0
        private static FieldCreationOptions FieldChoiceMultiOptionsToCreation(string title, FieldChoiceMultiOptions options)
        {
            FieldCreationOptions creationOptions = new FieldCreationOptions(FieldType.MultiChoice);

            creationOptions.ImportFromCommonFieldOptions(title, options);
            if (options.FillInChoice.HasValue)
            {
                creationOptions.SetAttribute("FillInChoice", options.FillInChoice.Value.ToString().ToUpper());
            }

            StringBuilder sb = new StringBuilder();

            if (!string.IsNullOrEmpty(options.DefaultChoice))
            {
                sb.AppendLine($"<Default>{CsomHelper.XmlString(options.DefaultChoice)}</Default>");
            }

            sb.AppendLine("<CHOICES>");

            foreach (var choice in options.Choices)
            {
                sb.AppendLine($"<CHOICE>{CsomHelper.XmlString(choice)}</CHOICE>");
            }
            sb.AppendLine("</CHOICES>");

            creationOptions.SetChildXmlNode("ChoiceXml", sb.ToString());

            return(creationOptions);
        }
示例#3
0
        private static FieldCreationOptions FieldNumberOptionsToCreation(string title, FieldNumberOptions options)
        {
            FieldCreationOptions creationOptions = new FieldCreationOptions(FieldType.Number);

            creationOptions.ImportFromCommonFieldOptions(title, options);

            if (options.DefaultValue.HasValue)
            {
                creationOptions.SetChildXmlNode("DefaultValue", $"<Default>{CsomHelper.XmlString(options.DefaultValue.Value.ToString())}</Default>");
            }

            if (options.Decimals.HasValue)
            {
                creationOptions.SetAttribute("Decimals", options.Decimals.ToString());
            }

            if (options.MinimumValue.HasValue)
            {
                creationOptions.SetAttribute("Min", options.MinimumValue.ToString());
            }

            if (options.MaximumValue.HasValue)
            {
                creationOptions.SetAttribute("Max", options.MaximumValue.ToString());
            }

            if (options.ShowAsPercentage.HasValue)
            {
                creationOptions.SetAttribute("Percentage", options.ShowAsPercentage.ToString().ToUpper());
            }

            return(creationOptions);
        }
示例#4
0
        private static FieldCreationOptions FieldMultilineTextOptionsToCreation(string title, FieldMultilineTextOptions options)
        {
            FieldCreationOptions creationOptions = new FieldCreationOptions(FieldType.Note);

            creationOptions.ImportFromCommonFieldOptions(title, options);

            if (options.NumberOfLines.HasValue)
            {
                creationOptions.SetAttribute("NumLines", options.NumberOfLines.ToString());
            }

            if (options.UnlimitedLengthInDocumentLibrary.HasValue)
            {
                creationOptions.SetAttribute("UnlimitedLengthInDocumentLibrary", options.UnlimitedLengthInDocumentLibrary.Value.ToString().ToUpper());
            }

            if (options.RichText.HasValue)
            {
                creationOptions.SetAttribute("RichText", options.RichText.Value.ToString().ToUpper());
            }

            if (options.AppendOnly.HasValue)
            {
                creationOptions.SetAttribute("AppendOnly", options.AppendOnly.Value.ToString().ToUpper());
            }

            if (options.AllowHyperlink.HasValue)
            {
                creationOptions.SetAttribute("AllowHyperlink", options.AllowHyperlink.Value.ToString().ToUpper());
            }

            return(creationOptions);
        }
示例#5
0
        private static FieldCreationOptions FieldUrlOptionsToCreation(string title, FieldUrlOptions options)
        {
            FieldCreationOptions creationOptions = new FieldCreationOptions(FieldType.URL);

            creationOptions.ImportFromCommonFieldOptions(title, options);
            creationOptions.SetAttribute("Format", options.DisplayFormat.ToString());
            return(creationOptions);
        }
示例#6
0
        private static FieldCreationOptions FieldLookupOptionsToCreation(string title, FieldLookupOptions options)
        {
            FieldCreationOptions creationOptions = new FieldCreationOptions(FieldType.Lookup);

            creationOptions.ImportFromCommonFieldOptions(title, options);
            creationOptions.SetAttribute("ShowField", options.LookupFieldName);
            creationOptions.SetAttribute("List", options.LookupListId.ToString());
            return(creationOptions);
        }
示例#7
0
        public IField AddField(FieldCreationOptions fieldCreationOptions)
        {
            if (fieldCreationOptions == null)
            {
                throw new ArgumentNullException(nameof(fieldCreationOptions));
            }

            return(AddFieldAsync(fieldCreationOptions).GetAwaiter().GetResult());
        }
示例#8
0
        private static FieldCreationOptions FieldDateTimeOptionsToCreation(string title, FieldDateTimeOptions options)
        {
            FieldCreationOptions creationOptions = new FieldCreationOptions(FieldType.DateTime);

            creationOptions.ImportFromCommonFieldOptions(title, options);
            creationOptions.SetAttribute("Format", options.DisplayFormat.ToString());
            creationOptions.SetAttribute("FriendlyDisplayFormat", options.FriendlyDisplayFormat.ToString());
            creationOptions.SetAttribute("CalType", ((int)options.DateTimeCalendarType).ToString());
            return(creationOptions);
        }
示例#9
0
        private static FieldCreationOptions FieldUserOptionsToCreation(string title, FieldUserOptions options)
        {
            FieldCreationOptions creationOptions = new FieldCreationOptions(FieldType.User);

            creationOptions.ImportFromCommonFieldOptions(title, options);
            creationOptions.SetAttribute("UserSelectionMode", ((int)options.SelectionMode).ToString());
            creationOptions.SetAttribute("UserSelectionScope", "0");
            //creationOptions.SetAttribute("IsModern", "TRUE");
            //creationOptions.SetAttribute("Dropdown", "TRUE");
            return(creationOptions);
        }
示例#10
0
        public async Task <IField> AddFieldBatchAsync(Batch batch, FieldCreationOptions fieldCreationOptions)
        {
            if (fieldCreationOptions == null)
            {
                throw new ArgumentNullException(nameof(fieldCreationOptions));
            }

            // Translate into field creation caml
            var newFieldCAML = FormatFieldXml(fieldCreationOptions);

            // Create the field using the XML approach
            return(await AddFieldAsXmlBatchAsync(batch, newFieldCAML, fieldCreationOptions.AddToDefaultView, fieldCreationOptions.Options).ConfigureAwait(false));
        }
示例#11
0
        private static FieldCreationOptions FieldTextOptionsToCreation(string title, FieldTextOptions options)
        {
            FieldCreationOptions creationOptions = new FieldCreationOptions(FieldType.Text);

            creationOptions.ImportFromCommonFieldOptions(title, options);

            if (options != null && options.MaxLength.HasValue)
            {
                creationOptions.SetAttribute("MaxLength", options.MaxLength.ToString());
            }

            return(creationOptions);
        }
示例#12
0
        private static FieldCreationOptions FieldCurrencyOptionsToCreation(string title, FieldCurrencyOptions options)
        {
            FieldCreationOptions creationOptions = new FieldCreationOptions(FieldType.Currency);

            creationOptions.ImportFromCommonFieldOptions(title, options);
            if (options.Decimals.HasValue)
            {
                creationOptions.SetAttribute("Decimals", options.Decimals.ToString().ToUpper());
            }
            if (options.CurrencyLocaleId.HasValue)
            {
                creationOptions.SetAttribute("LCID", options.CurrencyLocaleId.ToString().ToUpper());
            }
            return(creationOptions);
        }
示例#13
0
        private static FieldCreationOptions FieldCalculatedOptionsToCreation(string title, FieldCalculatedOptions options)
        {
            FieldCreationOptions creationOptions = new FieldCreationOptions(FieldType.Calculated);

            creationOptions.ImportFromCommonFieldOptions(title, options);
            creationOptions.SetChildXmlNode("Formula", $"<Formula>{CsomHelper.XmlString(options.Formula)}</Formula>");
            if (options.DateFormat.HasValue)
            {
                creationOptions.SetAttribute("Format", options.DateFormat.ToString());
            }
            creationOptions.SetAttribute("ResultType", options.OutputType.ToString());
            if (options.ShowAsPercentage.HasValue)
            {
                creationOptions.SetAttribute("Percentage", options.ShowAsPercentage.ToString().ToUpper());
            }
            return(creationOptions);
        }
示例#14
0
 public IField AddFieldBatch(Batch batch, FieldCreationOptions fieldCreationOptions)
 {
     return(AddFieldBatchAsync(batch, fieldCreationOptions).GetAwaiter().GetResult());
 }
示例#15
0
 public async Task <IField> AddFieldBatchAsync(FieldCreationOptions fieldCreationOptions)
 {
     return(await AddFieldBatchAsync(PnPContext.CurrentBatch, fieldCreationOptions).ConfigureAwait(false));
 }
示例#16
0
        /// <summary>
        /// Formats a FieldCreationOptions object into Field CAML xml.
        /// </summary>
        /// <param name="fieldCreationOptions">Field Creation Information object</param>
        /// <returns>Field creation CAML</returns>
        private static string FormatFieldXml(FieldCreationOptions fieldCreationOptions)
        {
            List <string> additionalAttributesList = new List <string>();

            if (fieldCreationOptions.AdditionalAttributes != null)
            {
                foreach (var keyvaluepair in fieldCreationOptions.AdditionalAttributes)
                {
                    additionalAttributesList.Add(string.Format(FIELD_XML_PARAMETER_FORMAT, keyvaluepair.Key, CsomHelper.XmlString(keyvaluepair.Value)));
                }
            }

            List <string> additionalChildNodesList = new List <string>();

            if (fieldCreationOptions.AdditionalChildNodes != null)
            {
                foreach (var keyvaluepair in fieldCreationOptions.AdditionalChildNodes)
                {
                    if (keyvaluepair.Key.StartsWith("Xml:"))
                    {
                        additionalChildNodesList.Add(keyvaluepair.Value);
                    }
                    else
                    {
                        additionalChildNodesList.Add(string.Format(FIELD_XML_CHILD_NODE, keyvaluepair.Key, CsomHelper.XmlString(keyvaluepair.Value)));
                    }
                }
            }

            if (!additionalAttributesList.Contains("ClientSideComponentId"))
            {
                if (fieldCreationOptions.ClientSideComponentId != Guid.Empty)
                {
                    additionalAttributesList.Add(string.Format(FIELD_XML_PARAMETER_FORMAT, "ClientSideComponentId", fieldCreationOptions.ClientSideComponentId.ToString("D")));
                }
            }
            if (!additionalAttributesList.Contains("ClientSideComponentProperties"))
            {
                if (fieldCreationOptions.ClientSideComponentProperties != null)
                {
                    additionalAttributesList.Add(string.Format(FIELD_XML_PARAMETER_FORMAT, "ClientSideComponentProperties", fieldCreationOptions.ClientSideComponentProperties));
                }
            }

            string newFieldCAML;

            if (additionalChildNodesList.Count > 0)
            {
                // Calculated fields require a Formula child node
                newFieldCAML = string.Format(FIELD_XML_FORMAT_WITH_CHILD_NODES,
                                             fieldCreationOptions.FieldType,
                                             fieldCreationOptions.InternalName,
                                             fieldCreationOptions.DisplayName,
                                             fieldCreationOptions.Id,
                                             fieldCreationOptions.Group,
                                             fieldCreationOptions.Required ? "TRUE" : "FALSE",
                                             additionalAttributesList.Any() ? string.Join(" ", additionalAttributesList) : "",
                                             string.Join("", additionalChildNodesList));
            }
            else
            {
                newFieldCAML = string.Format(FIELD_XML_FORMAT,
                                             fieldCreationOptions.FieldType,
                                             fieldCreationOptions.InternalName,
                                             fieldCreationOptions.DisplayName,
                                             fieldCreationOptions.Id,
                                             fieldCreationOptions.Group,
                                             fieldCreationOptions.Required ? "TRUE" : "FALSE",
                                             additionalAttributesList.Any() ? string.Join(" ", additionalAttributesList) : "");
            }

            return(newFieldCAML);
        }