MetadataDefaults.ApplyDefaults Method
Applies default values to a SPListItem object.
Namespace: Microsoft.Office.DocumentManagement
Assembly: Microsoft.Office.DocumentManagement (in Microsoft.Office.DocumentManagement.dll)
Syntax
'Declaration
Public Function ApplyDefaults ( _
item As SPListItem _
) As Boolean
'Usage
Dim instance As MetadataDefaults
Dim item As SPListItem
Dim returnValue As Boolean
returnValue = instance.ApplyDefaults(item)
public bool ApplyDefaults(
SPListItem item
)
Parameters
item
Type: Microsoft.SharePoint.SPListItemItem to apply defaults to
Return Value
Type: System.Boolean
true if successful; otherwise false
Remarks
For fields with multiple default values at different levels of the folder hierarchy, the default value from the SPFolder object that is nearest to the object that contains the SPListItem takes precedence.
Location-based defaults are only applied when either there is no existing value on the field or there is a value, but it is the list's default value for that field. This method follows these steps when applying location-based metadata defaults to the specified item.
For each field, if there is a location-based default specified at the location the item is in, and there isn't a value on the item for that field or the value is the list default, set the location-based default on the item.
If there is no location-based default specified at this location, look in the parent folder, if it exists, for a location-based default and apply that using the same logic.
Repeat steps 1 and 2 while iterating up the folder hierarchy until the root folder is reached.
For example, if the item is being added to /Document/Old/Contoso/Sales and the "Title" field had a default value of "An Old Document" set at /Documents/Old, but no location-based default at /Documents/Old/Contoso/Sales, ApplyDefaults would look for a default for "Title" at /Documents/Old/Contoso/Sales, it wouldn't find one, so it would look at /Documents/Old/Contoso. Again it does not find a default so it navigates up to the parent folder and l at /Documents/Old. Here it would find the default for the "Title" field of "An Old Document". If the item passed in has no value for "Title" or the "Title" value is identical to the list-based default then M:Microsoft.Office.DocumentManagement.MetadataDefaults.ApplyDefaults(Microsoft.SharePoint.SPListItem) would set "Title" to be "An Old Document".
Examples
SPSite site = new SPSite("http://contoso"); SPWeb web = new site.OpenWeb("/"); MetadataDefaults defaults = MetadataDefaults("/Documents", web); SPListItem item = web.GetListItem("/Documents/AdventureWorks/Proposal.docx"); defaults.ApplyDefaults(item); item.SystemUpdate(false);