zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Bzoj 1878: [SDOI2009]HH的项链 莫队
    BZOJ 2140: 稳定婚姻 Tarjan Map
    Bzoj 2190 : [SDOI2008]仪仗队 数论 特判
    bzoj 16801740 : [Usaco2005 Mar]Yogurt factory 贪心 双倍经验
    BZOJ 5387: [Lydsy1806月赛]质数拆分
    BZOJ 1379: [Baltic2001]Postman 水题
    Bzoj : 1823: [JSOI2010]满汉全席
    4952: [Wf2017]Need for Speed 二分
    BZOJ 2301: [HAOI2011]Problem b 2045: 双亲数 同BZOJ 1101 Zap 莫比乌斯反演 3倍经验
    BZOJ 1030: [JSOI2007]文本生成器 AC自动机
  • 原文地址:https://www.cnblogs.com/mingmingruyuedlut/p/2913891.html
Copyright © 2011-2022 走看看