Beispiel #1
0
        /// <summary>
        /// Writes a field value as an SPFolder's default column value
        /// </summary>
        /// <param name="folder">The folder for which we wish to update a field's default value</param>
        /// <param name="fieldValueInfo">The field and value information</param>
        public override void WriteValueToFolderDefault(SPFolder folder, FieldValueInfo fieldValueInfo)
        {
            var defaultValue = (UrlValue)fieldValueInfo.Value;
            MetadataDefaults listMetadataDefaults = new MetadataDefaults(folder.ParentWeb.Lists[folder.ParentListId]);

            if (defaultValue != null)
            {
                var sharePointFieldUrlValue = new SPFieldUrlValue {
                    Url = defaultValue.Url, Description = defaultValue.Description
                };

                this.log.Warn(
                    "WriteValueToFolderDefault - Initializing {0} field (fieldName={1}) with default value \"{2}\"."
                    + " Be aware that folder default values on {0}-type field are not well supported by SharePoint and that this default"
                    + " value will not be editable through your document library's \"List Settings > Column default value settings\" options page.",
                    fieldValueInfo.FieldInfo.FieldType,
                    fieldValueInfo.FieldInfo.InternalName,
                    sharePointFieldUrlValue.ToString());

                listMetadataDefaults.SetFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName, sharePointFieldUrlValue.ToString());
            }
            else
            {
                listMetadataDefaults.RemoveFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName);
            }

            listMetadataDefaults.Update();
        }
        /// <summary>
        /// Writes a standard field value as an SPFolder's default value
        /// </summary>
        /// <param name="folder">The folder for which we wish to update the default value</param>
        /// <param name="fieldValueInfo">The field and value information</param>
        public override void WriteValueToFolderDefault(SPFolder folder, FieldValueInfo fieldValueInfo)
        {
            var defaultValue = (bool?)fieldValueInfo.Value;
            MetadataDefaults listMetadataDefaults = new MetadataDefaults(folder.ParentWeb.Lists[folder.ParentListId]);

            var parentList = folder.ParentWeb.Lists[folder.ParentListId];
            var listField  = parentList.Fields[fieldValueInfo.FieldInfo.Id];

            // Pages library is a special case: attempting to set default value to TRUE will
            // always fail because of patchy OOTB support.
            if ((int)parentList.BaseTemplate == BuiltInListTemplates.Pages.ListTempateTypeId &&
                defaultValue.HasValue &&
                defaultValue.Value)
            {
                string exceptionMessage = "WriteValueToFolderDefault - Impossible to set folder default value as TRUE"
                                          + " within the Pages library. That column default would be ignored. (fieldName={0})";

                throw new NotSupportedException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              exceptionMessage,
                              fieldValueInfo.FieldInfo.InternalName));
            }

            if (!string.IsNullOrEmpty(listField.DefaultValue) &&
                bool.Parse(listField.DefaultValue) &&
                defaultValue.HasValue &&
                !defaultValue.Value)
            {
                // The SPField already has a default value set to TRUE. Our folder column default FALSE will have no
                // effect because the field definition's default will always be applied. Thanks SharePoint!
                string exceptionMessage = "WriteValueToFolderDefault - The field {0} already has a DefaultValue=TRUE definition."
                                          + " Your attempt to define a folder column default with value=FALSE would not work, since the TRUE"
                                          + " value imposed by the SPField's DefaultValue will always \"win\" and be applied instead.";

                throw new NotSupportedException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              exceptionMessage,
                              fieldValueInfo.FieldInfo.InternalName));
            }

            if (defaultValue.HasValue)
            {
                listMetadataDefaults.SetFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName, defaultValue.Value.ToString());
            }
            else
            {
                listMetadataDefaults.RemoveFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName);
            }

            listMetadataDefaults.Update();
        }
Beispiel #3
0
        /// <summary>
        /// Writes a standard field value as an SPFolder's default value
        /// </summary>
        /// <param name="folder">The folder for which we wish to update the column metadata defaults</param>
        /// <param name="fieldValueInfo">The field and value information</param>
        public override void WriteValueToFolderDefault(SPFolder folder, FieldValueInfo fieldValueInfo)
        {
            var defaultValue = (Guid?)fieldValueInfo.Value;
            MetadataDefaults listMetadataDefaults = new MetadataDefaults(folder.ParentWeb.Lists[folder.ParentListId]);

            if (defaultValue.HasValue)
            {
                listMetadataDefaults.SetFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName, defaultValue.Value.ToString());
            }
            else
            {
                listMetadataDefaults.RemoveFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName);
            }

            listMetadataDefaults.Update();
        }
Beispiel #4
0
        /// <summary>
        /// Writes a standard field value as an SPFolder's default value
        /// </summary>
        /// <param name="folder">The folder for which we wish to update the column metadata defaults</param>
        /// <param name="fieldValueInfo">The field and value information</param>
        public override void WriteValueToFolderDefault(SPFolder folder, FieldValueInfo fieldValueInfo)
        {
            var defaultValue = (string)fieldValueInfo.Value;
            MetadataDefaults listMetadataDefaults = new MetadataDefaults(folder.ParentWeb.Lists[folder.ParentListId]);

            var parentList = folder.ParentWeb.Lists[folder.ParentListId];

            // Pages library is a special case: attempting to set folder default value on any text-based field (Text, Note or HTML)
            // will always fail because of patchy OOTB support.
            if ((int)parentList.BaseTemplate == BuiltInListTemplates.Pages.ListTempateTypeId &&
                !string.IsNullOrEmpty(defaultValue))
            {
                string exceptionMessage = "WriteValueToFolderDefault - Impossible to set folder default value (val={0}) on field {1} of type {2}"
                                          + " within the Pages library. That column default would be ignored.";

                throw new NotSupportedException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              exceptionMessage,
                              defaultValue,
                              fieldValueInfo.FieldInfo.InternalName,
                              fieldValueInfo.FieldInfo.FieldType));
            }

            if (defaultValue != null)
            {
                if (fieldValueInfo.FieldInfo is NoteFieldInfo || fieldValueInfo.FieldInfo is HtmlFieldInfo)
                {
                    this.log.Warn(
                        "WriteValueToFolderDefault - Initializing {0} field (fieldName={1}) with default value \"{2}\"."
                        + " Be aware that folder default values on {0}-type field are not well supported by SharePoint and that this default"
                        + " value will not be editable through your document library's \"List Settings > Column default value settings\" options page.",
                        fieldValueInfo.FieldInfo.FieldType,
                        fieldValueInfo.FieldInfo.InternalName,
                        defaultValue);
                }

                listMetadataDefaults.SetFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName, defaultValue);
            }
            else
            {
                listMetadataDefaults.RemoveFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName);
            }

            listMetadataDefaults.Update();
        }
        /// <summary>
        /// Writes a field value as an SPFolder's default column value
        /// </summary>
        /// <param name="folder">The folder for which we wish to update a field's default value</param>
        /// <param name="fieldValueInfo">The field and value information</param>
        public override void WriteValueToFolderDefault(SPFolder folder, FieldValueInfo fieldValueInfo)
        {
            var defaultValue  = (TaxonomyValue)fieldValueInfo.Value;
            var list          = folder.ParentWeb.Lists[folder.ParentListId];
            var taxonomyField = (TaxonomyField)list.Fields[fieldValueInfo.FieldInfo.Id];
            MetadataDefaults listMetadataDefaults = new MetadataDefaults(list);

            if (defaultValue != null)
            {
                listMetadataDefaults.SetFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName, FormatTaxonomyString(taxonomyField, defaultValue));
            }
            else
            {
                listMetadataDefaults.RemoveFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName);
            }

            listMetadataDefaults.Update();
        }
        /// <summary>
        /// Writes a field value as an SPFolder's default column value
        /// </summary>
        /// <param name="folder">The folder for which we wish to update a field's default value</param>
        /// <param name="fieldValueInfo">The field and value information</param>
        public override void WriteValueToFolderDefault(SPFolder folder, FieldValueInfo fieldValueInfo)
        {
            var defaultValue  = (TaxonomyValueCollection)fieldValueInfo.Value;
            var list          = folder.ParentWeb.Lists[folder.ParentListId];
            var taxonomyField = (TaxonomyField)list.Fields[fieldValueInfo.FieldInfo.Id];
            MetadataDefaults listMetadataDefaults = new MetadataDefaults(list);

            if (defaultValue != null)
            {
                var    taxonomyFieldValueCollection = CreateSharePointTaxonomyFieldValue(taxonomyField, defaultValue, null);
                string collectionValidatedString    = taxonomyField.GetValidatedString(taxonomyFieldValueCollection);

                listMetadataDefaults.SetFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName, collectionValidatedString);
            }
            else
            {
                listMetadataDefaults.RemoveFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName);
            }

            listMetadataDefaults.Update();
        }
        /// <summary>
        /// Writes a standard field value as an SPFolder's default value
        /// </summary>
        /// <param name="folder">The field for which we wish to update the default value</param>
        /// <param name="fieldValueInfo">The field and value information</param>
        public override void WriteValueToFolderDefault(SPFolder folder, FieldValueInfo fieldValueInfo)
        {
            var              defaultValue         = (DateTime?)fieldValueInfo.Value;
            var              list                 = folder.ParentWeb.Lists[folder.ParentListId];
            var              listField            = list.Fields[fieldValueInfo.FieldInfo.Id];
            bool             isPagesLibrary       = (int)list.BaseTemplate == BuiltInListTemplates.Pages.ListTempateTypeId;
            MetadataDefaults listMetadataDefaults = new MetadataDefaults(list);

            // Pages library is a special case: attempting to set default value to TRUE will
            // always fail because of patchy OOTB support.
            if (isPagesLibrary &&
                defaultValue.HasValue &&
                (!string.IsNullOrEmpty(listField.DefaultValue) || !string.IsNullOrEmpty(listField.DefaultFormula)))
            {
                string exceptionMessage = "WriteValueToFolderDefault - Impossible to set folder default value as on DateTime-type field (fieldName={0})"
                                          + " within the Pages library when the SPField already has a DefaultValue or DefaultFormula. That folder column default (val={1})"
                                          + "would be ignored.";

                throw new NotSupportedException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              exceptionMessage,
                              fieldValueInfo.FieldInfo.InternalName,
                              defaultValue.Value.ToString(CultureInfo.InvariantCulture)));
            }

            if (defaultValue.HasValue)
            {
                // Weirdness warning: don't forget to convert to UTC so that the list item default value gets applied in UTC
                string dateString = FormatLocalDateTimeString(defaultValue.Value.ToUniversalTime());
                listMetadataDefaults.SetFieldDefault(folder.ServerRelativeUrl, fieldValueInfo.FieldInfo.InternalName, dateString);
            }
            else
            {
                listMetadataDefaults.RemoveFieldDefault(folder.ServerRelativeUrl, fieldValueInfo.FieldInfo.InternalName);
            }

            listMetadataDefaults.Update();
        }
Beispiel #8
0
        /// <summary>
        /// Writes a field value as an SPFolder's default column value
        /// </summary>
        /// <param name="folder">The folder for which we wish to update a field's default value</param>
        /// <param name="fieldValueInfo">The field and value information</param>
        public override void WriteValueToFolderDefault(SPFolder folder, FieldValueInfo fieldValueInfo)
        {
            var defaultValue = (ImageValue)fieldValueInfo.Value;
            var list         = folder.ParentWeb.Lists[folder.ParentListId];
            MetadataDefaults listMetadataDefaults = new MetadataDefaults(folder.ParentWeb.Lists[folder.ParentListId]);

            // Pages library is a special case: attempting to set default value will
            // always fail because of patchy OOTB support.
            if ((int)list.BaseTemplate == BuiltInListTemplates.Pages.ListTempateTypeId)
            {
                throw new NotSupportedException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "WriteValueToFolderDefault - Initializing a folder column default value with ImageValue within the Pages library s not supported (fieldName={0}).",
                              fieldValueInfo.FieldInfo.InternalName));
            }

            if (defaultValue != null)
            {
                var sharePointFieldImageValue = CreateSharePointImageFieldValue(defaultValue);

                this.log.Warn(
                    "WriteValueToFolderDefault - Initializing {0} field (fieldName={1}) with default value \"{2}\"."
                    + " Be aware that folder default values on {0}-type field are not well supported by SharePoint and that this default"
                    + " value will not be editable through your document library's \"List Settings > Column default value settings\" options page.",
                    fieldValueInfo.FieldInfo.FieldType,
                    fieldValueInfo.FieldInfo.InternalName,
                    sharePointFieldImageValue.ToString());

                listMetadataDefaults.SetFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName, sharePointFieldImageValue.ToString());
            }
            else
            {
                listMetadataDefaults.RemoveFieldDefault(folder, fieldValueInfo.FieldInfo.InternalName);
            }

            listMetadataDefaults.Update();
        }