private static void ClearFolderAllFolderMetadataDefaults(SPFolder folder) { MetadataDefaults listMetadataDefaults = new MetadataDefaults(folder.ParentWeb.Lists[folder.ParentListId]); listMetadataDefaults.RemoveAllFieldDefaults(folder); 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 = (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(); }
/// <summary> /// Sets a default for a field at a location. /// </summary> /// <param name="metadata">Provides the method to set the default value for the field</param> /// <param name="folder"><see cref="SPFolder"/> location at which to set the default value</param> /// <param name="list">List of the TaxonomyField containing the validatedString corresponding to the default value.</param> public void ApplyFieldOnMetadata(MetadataDefaults metadata, SPFolder folder, SPList list) { var term = this.Term; var labelGuidPair = term.GetDefaultLabel((int)list.ParentWeb.Language) + "|" + term.Id; var taxonomyField = list.Fields.GetField(this.FieldName) as TaxonomyField; var newTaxonomyFieldValue = new TaxonomyFieldValue(taxonomyField); // PopulateFromLabelGuidPair takes care of looking up the WssId value and creating a new item in the TaxonomyHiddenList if needed. // Reference: http://msdn.microsoft.com/en-us/library/ee567833.aspx newTaxonomyFieldValue.PopulateFromLabelGuidPair(labelGuidPair); metadata.SetFieldDefault(folder, this.FieldName, newTaxonomyFieldValue.ValidatedString); }
/// <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(); }
public string GetDefaultContentTypeId(SPFolder folder) { var contentTypeId = string.Empty; if (folder.Item != null) { var metadata = new MetadataDefaults(folder.Item.ParentList); while (string.IsNullOrEmpty(contentTypeId) && folder != null) { contentTypeId = metadata.GetFieldDefault(folder, BuiltInFields.ContentTypeId.InternalName); folder = folder.ParentFolder; } } return contentTypeId; }
/// <summary> /// Applies the field on metadata. /// </summary> /// <param name="metadata">The metadata.</param> /// <param name="folder">The folder.</param> public virtual void ApplyFieldOnMetadata(MetadataDefaults metadata, SPFolder folder) { var defaultValue = string.Empty; if (this.Value != null) { defaultValue = this.Value.ToString(); } metadata.SetFieldDefault( folder, this.FieldName, defaultValue); metadata.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 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(); }
/// <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(); }
/// <summary> /// Sets a default for a field at a location. /// </summary> /// <param name="metadata">Provides the method to set the default value for the field</param> /// <param name="folder"><see cref="SPFolder"/> location at which to set the default value</param> /// <param name="list">List of the TaxonomyField containing the validatedString corresponding to the default value.</param> public void ApplyFieldOnMetadata(MetadataDefaults metadata, SPFolder folder, SPList list) { var taxonomyField = list.Fields.GetField(this.FieldName) as TaxonomyField; if (taxonomyField != null) { var newTaxonomyFieldValueCollection = this.GetTaxonomyFieldValueCollection(taxonomyField); List<string> wssIdAndLabelStrings = new List<string>(); foreach (TaxonomyFieldValue fieldValue in newTaxonomyFieldValueCollection) { var wssId = fieldValue.WssId; var label = fieldValue.Label; wssIdAndLabelStrings.Add(wssId + ";#" + label); } // Tax field value collection looks like this: "97;#Human resources;#96;#IT Services Portal" where "WSSidTerm1;#Term1Label;#WssidTerm2;#Term2Label" string taxFieldValueCollectionAsString = string.Join(";#", wssIdAndLabelStrings.ToArray()); metadata.SetFieldDefault(folder, this.FieldName, taxFieldValueCollectionAsString); } }
private static void ClearFolderAllFolderMetadataDefaults(SPFolder folder) { MetadataDefaults listMetadataDefaults = new MetadataDefaults(folder.ParentWeb.Lists[folder.ParentListId]); listMetadataDefaults.RemoveAllFieldDefaults(folder); listMetadataDefaults.Update(); }
private static void ApplyValuesAndDefaults(SPList library, SPFolder folder, SPListItem folderItem, IFolderInfo folderInfo) { MetadataDefaults metadataDefaults = null; if (folderInfo.Values != null) { foreach (var value in folderInfo.Values) { if (value == null) { continue; } var taxonomyInfo = value as TaxonomyInfo; var taxonomyMultiInfo = value as TaxonomyMultiInfo; if (taxonomyInfo != null) { taxonomyInfo.ApplyOnItem(folderItem, library); } else if (taxonomyMultiInfo != null) { taxonomyMultiInfo.ApplyOnItem(folderItem, library); } else { value.ApplyOnItem(folderItem); } } } if (folderInfo.Defaults != null) { metadataDefaults = new MetadataDefaults(library); foreach (var metaDefault in folderInfo.Defaults) { var taxonomyDefault = metaDefault as TaxonomyInfo; var taxonomyMultiInfo = metaDefault as TaxonomyMultiInfo; if (taxonomyDefault != null) { taxonomyDefault.ApplyFieldOnMetadata(metadataDefaults, folder, library); } else if (taxonomyMultiInfo != null) { taxonomyMultiInfo.ApplyFieldOnMetadata(metadataDefaults, folder, library); } else { metaDefault.ApplyFieldOnMetadata(metadataDefaults, folder); } } } if (folderInfo.UniqueContentTypeOrder.Count > 0) { var listContentTypes = new List<SPContentType>(); foreach (var contentTypeId in folderInfo.UniqueContentTypeOrder) { // Get the content type id for this particular list. var listContentTypeId = library.ContentTypes.BestMatch(contentTypeId); // Make sure it is the direct child of the one we specified. if (listContentTypeId.Parent == contentTypeId) { // Add it to the list of content types. listContentTypes.Add(library.ContentTypes[listContentTypeId]); } } // Set the content types to the folder. if (listContentTypes.Count > 0) { folder.UniqueContentTypeOrder = listContentTypes; } } if (metadataDefaults != null) { metadataDefaults.Update(); } }