예제 #1
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);
        }
예제 #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
        public override string ToString()
        {
            string stringValue = Value != null?CsomHelper.XmlString(Parameter.TypeSpecificHandling(Value.ToString(), Type), false) : "";

            _ = Value != null ? Type : "Null";

            if (Value == null)
            {
                return($"<Property Name=\"{Name}\" Type=\"Null\" />");
            }
            return($"<Property Name=\"{Name}\" Type=\"{Type}\">{stringValue}</Property>");
        }
예제 #4
0
        public ContentType()
        {
            PostMappingHandler = (json) =>
            {
                // In the case of Add operation, the CSOM api is used and JSON mapping to model is not possible
                // So here, populate the Rest Id metadata field to enable actions upon
                // this content type without having to read it again from the server
                AddMetadata(PnPConstants.MetaDataRestId, StringId);
                AddMetadata(PnPConstants.MetaDataType, "SP.ContentType");
                Requested = true;
            };

            // Handler to construct the Add request for this content type
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
            AddApiCallHandler = async(keyValuePairs) =>
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
            {
                // Content type creation by specifying the parent id is not possible using SharePoint REST
                //https://github.com/pnp/pnpjs/issues/457
                //https://github.com/SharePoint/sp-dev-docs/issues/3276
                //https://stackoverflow.com/questions/55529315/how-to-create-site-content-type-with-id-using-rest-api

                // Given this method can apply on both Web.ContentTypes as List.ContentTypes we're getting the entity info which will
                // automatically provide the correct 'parent'
                var entity = EntityManager.GetClassInfo(GetType(), this);

                // Adding new content types on a list is not something we should allow
                if (entity.Target == typeof(List))
                {
                    throw new ClientException(ErrorType.Unsupported,
                                              PnPCoreResources.Exception_Unsupported_AddingContentTypeToList);
                }

                // Fallback to CSOM call
                string actualDescription = !string.IsNullOrEmpty(Description)
                    ? $@"<Property Name=""Description"" Type=""String"">{CsomHelper.XmlString(Description)}</Property>"
                    : $@"<Property Name=""Description"" Type=""Null"" />";
                string actualGroup = !string.IsNullOrEmpty(Group)
                    ? $@"<Property Name=""Group"" Type=""String"">{CsomHelper.XmlString(Group)}</Property>"
                    : @"<Property Name=""Group"" Type=""Null"" />";

                string xml = CsomHelper.ContentTypeCreate.Replace(CsomHelper.ContentTypeActualDescription, actualDescription)
                             .Replace(CsomHelper.ContentTypeActualGroup, actualGroup)
                             .Replace(CsomHelper.ContentTypeStringId, CsomHelper.XmlString(StringId))
                             .Replace(CsomHelper.ContentTypeName, CsomHelper.XmlString(Name));

                return(new ApiCall(xml));
            };
        }
예제 #5
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);
        }
예제 #6
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);
        }
예제 #7
0
        /// <summary>
        /// Imports settings from the <see cref="CommonFieldOptions"/> configuration into the generic <see cref="FieldCreationOptions"/> model
        /// </summary>
        /// <param name="common">Set of field creation options to transform</param>
        /// <param name="title">Field title</param>
        internal void ImportFromCommonFieldOptions(string title, CommonFieldOptions common)
        {
            if (string.IsNullOrEmpty(title))
            {
                throw new ArgumentNullException(nameof(title));
            }

            if (common != null && common.Id.HasValue)
            {
                Id = common.Id.Value;
            }
            else
            {
                Id = Guid.NewGuid();
            }

            DisplayName  = title;
            InternalName = title;

            if (common == null)
            {
                return;
            }

            AddToDefaultView = common.AddToDefaultView;
            Options          = common.Options;

            if (common.Required.HasValue)
            {
                Required = common.Required.Value;
            }

            if (!string.IsNullOrEmpty(common.Group))
            {
                Group = common.Group;
            }

            if (!string.IsNullOrEmpty(common.Description))
            {
                SetAttribute("Description", common.Description);
            }

            if (common.EnforceUniqueValues.HasValue)
            {
                SetAttribute("EnforceUniqueValues", common.EnforceUniqueValues.Value.ToString().ToUpper());
            }

            if (common.Hidden.HasValue)
            {
                SetAttribute("Hidden", common.Hidden.Value.ToString().ToUpper());
            }

            if (common.Indexed.HasValue)
            {
                SetAttribute("Indexed", common.Indexed.Value.ToString().ToUpper());
            }

            if (!string.IsNullOrEmpty(common.DefaultFormula))
            {
                SetChildXmlNode("DefaultFormula", $"<DefaultFormula>{CsomHelper.XmlString(common.DefaultFormula)}</DefaultFormula>");
            }

            if (!string.IsNullOrEmpty(common.ValidationFormula) && !string.IsNullOrEmpty(common.ValidationMessage))
            {
                SetChildXmlNode("Validation", $"<Validation Message=\"{CsomHelper.XmlString(common.ValidationMessage, true)}\">{CsomHelper.XmlString(common.ValidationFormula)}</Validation>");
            }

            if (common.ShowInEditForm.HasValue)
            {
                SetAttribute("ShowInEditForm", common.ShowInEditForm.Value.ToString().ToUpper());
            }

            if (common.ShowInViewForms.HasValue)
            {
                SetAttribute("ShowInViewForms", common.ShowInViewForms.Value.ToString().ToUpper());
            }

            if (common.ShowInNewForm.HasValue)
            {
                SetAttribute("ShowInNewForm", common.ShowInNewForm.Value.ToString().ToUpper());
            }

            if (!string.IsNullOrEmpty(common.CustomFormatter))
            {
                SetAttribute("CustomFormatter", common.CustomFormatter);
            }
        }
예제 #8
0
 internal virtual string SerializeValue()
 {
     return(CsomHelper.XmlString(TypeSpecificHandling(Value.ToString(), Type), false));
 }