共用方式為


IRecordDeclarationHandler Interface

Provides an interface for custom processing of declaring a list item as record.

Namespace:  Microsoft.Office.RecordsManagement.RecordsRepository
Assembly:  Microsoft.Office.Policy (in Microsoft.Office.Policy.dll)

Syntax

'Declaration
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Interface IRecordDeclarationHandler
'Usage
Dim instance As IRecordDeclarationHandler
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public interface IRecordDeclarationHandler

Remarks

When a list item is declared as record, custom processing handler can be added by implementing the OnDeclare method in the IRecordDeclarationHandler interface. After the handler has processed the list item, it can choose to continue or skip default processing by returning the corresponiding RecordOperationResult.

The following code examples show you how to register a custom record handler and create a custom declaration filter that manages record declaration and undeclaration.

Examples

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Office.RecordsManagement.RecordsRepository;
using Microsoft.SharePoint;

namespace Microsoft.SDK.SharePoint.Samples 
{
    /// <summary>
    /// RegisterDeclarationFilter registers the DeclarationFilter assembly with the server 
    /// to handle record declaration and undeclaration.
    /// </summary>
    class RegisterDeclarationFilter
    {
        private const string ASSEMBLY_STRONG_NAME = "DeclarationFilter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=991a95319bfcc113";
        private const string CLASS_NAME = "Microsoft.SDK.SharePoint.Samples.DeclarationFilter";

        /// <summary>
        /// Registers the DeclarationFilter assembly on the given site.
        /// </summary>
        /// <param name="args">command line arguments, the first one must be a valid site URL</param>
        /// <exception cref="System.ArgumentException">empty command line arguments</exception>
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                throw new ArgumentException("Must supply site URL");
            }

            using (SPSite site = new SPSite(args[0]))
            {
                Records.RegisterCustomCodeForRecordDeclaration(site,
                    ASSEMBLY_STRONG_NAME,
                    CLASS_NAME);
                Console.WriteLine(string.Format("DeclarationFilter registered to site {0}.", site.Url));
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Office.RecordsManagement.RecordsRepository;
using Microsoft.SharePoint;

namespace Microsoft.SDK.SharePoint.Samples
{
    /// <summary>
    /// DeclarationFilter is a custom handler for record declaration and undeclaration.
    /// </summary>
    /// <remarks>
    /// <para>
    /// DeclarationFilter filters out items which should not be declared or undeclared before record processing. 
    /// If an item should not be declared as record, sets the property "_do_not_declare_record" to true.
    /// If a record item should not be undeclared, sets the property "_do_not_undeclare_record" to true.
    /// </para>
    /// <para>
    /// DeclarationFilter implements IRecordDeclarationHandler and IRecordUndeclarationHandler to handle both record declaration and undeclaration.
    /// When an item is declared record, DeclarationFilter checks whether the item has the "_do_not_declare_record" property set to true, if so it cancels the record declaration.
    /// When a record is undeclared, DeclarationFilter checks if whether the item has the "_do_not_undeclare_record" property set to true, if so it cancels the undeclaration.
    /// </para>
    /// </remarks>
    class DeclarationFilter : IRecordDeclarationHandler, IRecordUndeclarationHandler
    {
        /// <summary>
        /// This is the name of the property we assign to items that should not be declared as record.
        /// </summary>
        public const string PROPERTY_DO_NOT_DECLARE = "_do_not_declare_record";

        /// <summary>
        /// This is the name of the property we assign to records that should not be undeclared.
        /// </summary>
        public const string PROPERTY_DO_NOT_UNDECLARE = "_do_not_undeclare_record";

        #region IRecordDeclarationHandler Members

        /// <summary>
        /// Checks and filters out item marked with "_do_not_declare_record" for record declaration.
        /// </summary>
        /// <remarks>
        /// Checks whether item is marked with "_do_not_declare_record". If so then cancels record declaration, 
        /// otherwise contniue default record declaration processing.
        /// </remarks>
        /// <param name="item">the item to be declared as record</param>
        /// <returns>
        /// RecordOperationResult.CancelRecordProcessing if item should not be declared;
        /// RecordOperationResult.ContinueRecordProcessing otherwise.
        /// </returns>
        RecordOperationResult IRecordDeclarationHandler.OnDeclare(SPListItem item)
        {
            // checks if item is marked as "_do_not_declare_record"
            if (CheckRecordFilterProperty(item, PROPERTY_DO_NOT_DECLARE))
            {
                // item marked as "_do_not_declare_record", cancel processing
                return RecordOperationResult.CancelRecordProcessing;
            }
            else
            {
                // continue with default processing
                return RecordOperationResult.ContinueRecordProcessing;
            }
        }

        #endregion

        #region IRecordUndeclarationHandler Members

        /// <summary>
        /// Checks and filters out item marked with "_do_not_undeclare_record" for record undeclaration.
        /// </summary>
        /// <remarks>
        /// Checks whether item is marked with "_do_not_undeclare_record" If so then cancels record undeclaration, 
        /// otherwise contniue default record undeclaration processing.
        /// </remarks>
        /// <param name="item">the item to be undeclared</param>
        /// <returns>
        /// RecordOperationResult.CancelRecordProcessing if item should not be undeclared; 
        /// RecordOperationResult.ContinueRecordProcessing otherwise.
        /// </returns>
        RecordOperationResult IRecordUndeclarationHandler.OnUndeclare(SPListItem item)
        {
            // checks if item is marked as "_do_not_undeclare_record"
            if (CheckRecordFilterProperty(item, PROPERTY_DO_NOT_UNDECLARE))
            {
                // item mared as "_do_not_undeclare_record", cancel processing
                return RecordOperationResult.CancelRecordProcessing;
            }
            else
            {
                // continue with default processing
                return RecordOperationResult.ContinueRecordProcessing;
            }
        }

        #endregion

        /// <summary>
        /// Checks the given record filter property on the given item.
        /// </summary>
        /// <param name="item">the item</param>
        /// <param name="propertyName">the record filter property</param>
        /// <returns>the property value, false if not found.</returns>
        private static bool CheckRecordFilterProperty(SPListItem item, string propertyName)
        {
            bool result = false;
            if (item.Properties.Contains(propertyName))
            {
                object property = item.Properties[propertyName];
                if (property != null)
                {
                    result = string.Equals(bool.TrueString, (string)property, StringComparison.OrdinalIgnoreCase);
                }
            }
            return result;
        }
    }
}

See Also

Reference

IRecordDeclarationHandler Members

Microsoft.Office.RecordsManagement.RecordsRepository Namespace

RecordOperationResult