SharePoint中添加或者修改Item时调用EventReceiver(Event Handler)处理额外的逻辑

SharePoint中添加或者修改Item时调用EventReceiver(Event Handler)处理额外的逻辑。取名:EricSunArticlesListItemEventReceiver

Elements.xml

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="10000">
      <Receiver>
        <Name>EricSunArticlesListItemEventReceiverItemAdding</Name>
        <Type>ItemAdding</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>EricSunSharePointProject.ListInstances.EricSunArticlesListItemEventReceiver.EricSunArticlesListItemEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>EricSunArticlesListItemEventReceiverItemUpdating</Name>
        <Type>ItemUpdated</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>EricSunSharePointProject.ListInstances.EricSunArticlesListItemEventReceiver.EricSunArticlesListItemEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
  </Receivers>
</Elements>

EricSunArticlesListItemEventReceiver.cs

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Text.RegularExpressions;

namespace EricSunSharePointProject.ListInstances.EricSunArticlesListItemEventReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EricSunArticlesListItemEventReceiver : SPItemEventReceiver
    {
        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdding(SPItemEventProperties properties)
        {
            try
            {
                if (IsTargetContentType(properties.List))
                {
                    SPListItem listItem = properties.ListItem;
                    InitAttachmentString(listItem, "EricSunArticleAttachments");
                    InitCategoryString(listItem, "EricSunCategories", "EricSunCategoryString");
                }
            }
            catch (SPException ex)
            {
            }
        }

        /// <summary>
        /// An item is being updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            try
            {
                if (IsTargetContentType(properties.List))
                {
                    SPListItem listItem = properties.ListItem;
                    InitAttachmentString(listItem, "EricSunArticleAttachments");
                    InitCategoryString(listItem, "EricSunCategories", "EricSunCategoryString");
                }
            }
            catch (SPException ex)
            {
                
            }
        }

        /// <summary>
        /// Judge target content type.
        /// </summary>
        private bool IsTargetContentType(SPList List)
        {
            bool isTargetContentType = false;
            if (List == null)
            {
                throw new ArgumentNullException("List is null.");
            }
            SPContentTypeCollection contentTypeCollection = List.ContentTypes;
            foreach (SPContentType contentType in contentTypeCollection)
            {
                if (contentType.Name.Equals("EricSunArticleContentType", StringComparison.Ordinal))
                {
                    isTargetContentType = true;
                    break;
                }
            }
            return isTargetContentType;
        }

        /// <summary>
        /// Initial category string from category lookup.
        /// </summary>
        private void InitCategoryString(SPListItem listItem, string fromField, string toField)
        {
            if (listItem == null)
            {
                throw new ArgumentNullException("List item is null.");
            }
            if (listItem[fromField] == null)
            {
                throw new ArgumentNullException("List field is null.");
            }
            SPFieldLookupValue categoryLookup = new SPFieldLookupValue(listItem[fromField].ToString());
            string[] choices = Regex.Split(categoryLookup.ToString(), ";#");
            string categories = string.Empty;
            for (int i = 1; i < choices.Length; i += 2)
            {
                categories += choices[i] + ";";
            }
            listItem[toField] = categories;
            listItem.Update();
        }

        /// <summary>
        /// Initial attachment string when add attachment to item
        /// </summary>
        private void InitAttachmentString(SPListItem listItem, string toField)
        {
            if (listItem == null)
            {
                throw new ArgumentNullException("List item is null.");
            }
            SPAttachmentCollection attachments = listItem.Attachments;
            string attachmentList = string.Empty;
            foreach (string attachment in attachments)
            {
                string name = attachment;
                string url = SPUrlUtility.CombineUrl(attachments.UrlPrefix, attachment);
                attachmentList += name + "," + url + ";";
            }
            listItem[toField] = attachmentList;
            listItem.Update();
        }

    }
}

http://msdn.microsoft.com/en-us/library/gg252010(v=office.14).aspx

原文地址:https://www.cnblogs.com/mingmingruyuedlut/p/2913891.html