2013년 4월 2일 화요일

sharepoint email


using System;
using System.Security.Permissions;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

//Code courtesy of @timferro, @dannyjessee, MSDN, and the internet
namespace SharePointProject1.EventReceiver1
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);
       }

       /// <summary>
       /// An item is being updated.
       /// </summary>
       public override void ItemUpdating(SPItemEventProperties properties)
       {
           base.ItemUpdating(properties);
           if ((string)properties.ListItem["Status"] != (string)properties.AfterProperties["Status"])
           {
               properties.AfterProperties["Notes"] = "Status Change";
           }
       }

       /// <summary>
       /// An item is being deleted.
       /// </summary>
       public override void ItemDeleting(SPItemEventProperties properties)
       {
           base.ItemDeleting(properties);
           //Get user object
           SPUser user = properties.Web.SiteUsers.GetByID(properties.CurrentUserId);
           //If the user is not a Site Collection Admin, don't delete the item
           if (!user.IsSiteAdmin)
           {
               //Cancel the request with an error and set the error message
               properties.Status = SPEventReceiverStatus.CancelWithError;
               properties.ErrorMessage = "Only Site Collection Administrators can delete items from this list!";
           }
           
       }

       /// <summary>
       /// An attachment is being added to the item.
       /// </summary>
       public override void ItemAttachmentAdding(SPItemEventProperties properties)
       {
           base.ItemAttachmentAdding(properties);
       }

       /// <summary>
       /// An item was added.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           base.ItemAdded(properties);
           //Get the list item that was added
           SPListItem item = properties.ListItem;

           //Set the Notes field with ItemAdded
           item["Notes"] = "ItemAdded";

           //Get a reference to the TestDocument document library
           SPList testDoc = properties.Web.Lists["TestDocument"];

           //Create a new folder in the document library
           SPListItem folder = testDoc.Folders.Add("", SPFileSystemObjectType.Folder, item.Title);
           folder.Update();

           //Get the document library name and folder url
           string[] splitFolderUrl = folder.Url.Split('/');

           //Encode the url string
           string folderUrlEncode = SPHttpUtility.UrlKeyValueEncode("/" + folder.Url);

           //Update the list item with the link
           item["Documents"] = properties.WebUrl + "/" + splitFolderUrl[0].ToString() + "/Forms/AllItems.aspx?RootFolder=" + folderUrlEncode + ", Click Me";
           
           //Disable event firing in this thread to avoid loops
           this.EventFiringEnabled = false;
           //Update the list item
           item.Update();
           //Reenable event firing
           this.EventFiringEnabled = true;
       }

       /// <summary>
       /// An item was updated.
       /// </summary>
       public override void ItemUpdated(SPItemEventProperties properties)
       {
           base.ItemUpdated(properties);
           //Get the updated list item object
           SPListItem item = properties.ListItem;
           //Set the Notes field to ItemUpdated
           item["Notes"] += " - ItemUpdated";

           //If the SetAlert checkbox is checked
           if ((Boolean)item["SetAlert"])
           {
               //Set it back to false
               item["SetAlert"] = false;
               //Get the current user's object
               SPUser user = properties.Web.SiteUsers.GetByID(properties.CurrentUserId);
               //Create a new user alert
               SPAlert alert = user.Alerts.Add();
               alert.Title = item.Title;
               alert.AlertType = SPAlertType.Item;
               alert.AlertFrequency = SPAlertFrequency.Immediate;
               alert.EventType = SPEventType.All;
               alert.Item = item;
               alert.Update(false);
           }

           //If the SendEmail checkbox is checked
           if ((Boolean)item["SendEmail"])
           {
               //Set it back to false
               item["SendEmail"] = false;
               //Send an email using SPUtility.SendEmail
               SPUtility.SendEmail(properties.Web, false, false, "timothy.ferro@gmail.com", "FEDSPUG Test Email", "TEST");
           }

           //Disable event firing in this thread to avoid loops
           this.EventFiringEnabled = false;
           //Update the list item
           item.Update();
           //Reenable event firing
           this.EventFiringEnabled = true;
       }

       /// <summary>
       /// An item was deleted.
       /// </summary>
       public override void ItemDeleted(SPItemEventProperties properties)
       {
           base.ItemDeleted(properties);
       }

       /// <summary>
       /// An attachment was added to the item.
       /// </summary>
       public override void ItemAttachmentAdded(SPItemEventProperties properties)
       {
           base.ItemAttachmentAdded(properties);
       }


    }
}

댓글 없음: