[C#/OUTLOOK] MAPIFolder 인터페이스 : PropertyAccessor 속성을 사용해 사용자 속성 추가/조회/삭제하기
C#/Outlook 2021. 7. 20. 22:33728x90
반응형
728x170
▶ RibbonContextMenu.xml
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="customUI_Load">
<contextMenus>
<contextMenu idMso="ContextMenuFolder">
<button id="setFolderUserPropertyContextMenu"
label="폴더 사용자 속성 설정하기"
getVisible="folderContextMenu_getVisible"
onAction="folderContextMenu_onAction" />
<button id="getFolderUserPropertyContextMenu"
label="폴더 사용자 속성 가져오기"
getVisible="folderContextMenu_getVisible"
onAction="folderContextMenu_onAction" />
<button id="deleteFolderUserPropertyContextMenu"
label="폴더 사용자 속성 제거하기"
getVisible="folderContextMenu_getVisible"
onAction="folderContextMenu_onAction" />
</contextMenu>
</contextMenus>
</customUI>
728x90
▶ RibbonContextMenu.cs
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace TestProject
{
/// <summary>
/// 리본 컨텍스트 메뉴
/// </summary>
[ComVisible(true)]
public class RibbonContextMenu : IRibbonExtensibility
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 리본 UID
/// </summary>
private IRibbonUI ribbonUI;
/// <summary>
/// 폴더 속성 스키마입니다.
/// </summary>
private string folderPropertySchema = "http://schemas.microsoft.com/mapi/string/{F0974C06-6AD7-4ADF-B334-78C4B3B15B87}/CUSTOM-TAG";
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - RibbonContextMenu()
/// <summary>
/// 생성자
/// </summary>
public RibbonContextMenu()
{
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
//////////////////////////////////////////////////////////////////////////////// Event
#region 커스텀 UI 로드시 처리하기 - customUI_Load(ribbonUI)
/// <summary>
/// 커스텀 UI 로드시 처리하기
/// </summary>
/// <param name="ribbonUI">리본 UI</param>
public void customUI_Load(IRibbonUI ribbonUI)
{
this.ribbonUI = ribbonUI;
}
#endregion
#region 폴더 컨텍스트 메뉴 표시 여부 구하기 - folderContextMenu_getVisible(ribbonControl)
/// <summary>
/// 폴더 컨텍스트 메뉴 표시 여부 구하기
/// </summary>
/// <param name="ribbonControl">리본 컨트롤</param>
/// <returns>처리 결과</returns>
public bool folderContextMenu_getVisible(IRibbonControl ribbonControl)
{
string id = ribbonControl.Id;
if(id == "setFolderUserPropertyContextMenu")
{
try
{
Folder folder = ribbonControl.Context as Folder;
PropertyAccessor propertyAccessor = folder?.PropertyAccessor;
dynamic propertyValue = propertyAccessor?.GetProperty(folderPropertySchema);
}
catch
{
return true;
}
return false;
}
else if(id == "getFolderUserPropertyContextMenu" || id == "deleteFolderUserPropertyContextMenu")
{
try
{
Folder folder = ribbonControl.Context as Folder;
PropertyAccessor propertyAccessor = folder?.PropertyAccessor;
dynamic propertyValue = propertyAccessor?.GetProperty(folderPropertySchema);
}
catch
{
return false;
}
return true;
}
return false;
}
#endregion
#region 폴더 컨텍스트 메뉴 작업시 처리하기 - folderContextMenu_onAction(ribbonControl)
/// <summary>
/// 폴더 컨텍스트 메뉴 작업시 처리하기
/// </summary>
/// <param name="ribbonControl">리본 컨트롤</param>
public void folderContextMenu_onAction(IRibbonControl ribbonControl)
{
string id = ribbonControl.Id;
if(id == "setFolderUserPropertyContextMenu")
{
try
{
Folder folder = ribbonControl.Context as Folder;
PropertyAccessor propertyAccessor = folder?.PropertyAccessor;
propertyAccessor?.SetProperty(folderPropertySchema, DateTime.Now.ToString());
dynamic propertyValue = propertyAccessor?.GetProperty(folderPropertySchema);
System.Windows.Forms.MessageBox.Show($"사용자 속성을 설정하였습니다.\r\n폴더 : {folder.Name}\r\n사용자 속성값 : {propertyValue}");
}
catch(System.Exception exception)
{
System.Windows.Forms.MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}");
}
}
else if(id == "getFolderUserPropertyContextMenu")
{
try
{
Folder folder = ribbonControl.Context as Folder;
PropertyAccessor propertyAccessor = folder?.PropertyAccessor;
dynamic propertyValue = propertyAccessor?.GetProperty(folderPropertySchema);
System.Windows.Forms.MessageBox.Show($"사용자 속성을 가져왔습니다.\r\n폴더 : {folder.Name}\r\n사용자 속성값 : {propertyValue}");
}
catch(System.Exception exception)
{
System.Windows.Forms.MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}");
}
}
else if(id == "deleteFolderUserPropertyContextMenu")
{
try
{
Folder folder = ribbonControl.Context as Folder;
PropertyAccessor propertyAccessor = folder?.PropertyAccessor;
propertyAccessor?.DeleteProperty(folderPropertySchema);
System.Windows.Forms.MessageBox.Show($"사용자 속성을 제거하였습니다.\r\n폴더 : {folder.Name}");
}
catch(System.Exception exception)
{
System.Windows.Forms.MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}");
}
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region (IRibbonExtensibility) 커스텀 UI 구하기 - GetCustomUI(ribbonID)
/// <summary>
/// 커스텀 UI 구하기
/// </summary>
/// <param name="ribbonID"></param>
/// <returns>커스텀 UI</returns>
public string GetCustomUI(string ribbonID)
{
return GetResourceText("TestProject.RibbonContextMenu.xml");
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Function
#region 리소스 텍스트 구하기 - GetResourceText(resourceName)
/// <summary>
/// 리소스 텍스트 구하기
/// </summary>
/// <param name="resourceName">리소스명</param>
/// <returns>리소스 텍스트</returns>
private static string GetResourceText(string resourceName)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string[] resourceNameArray = assembly.GetManifestResourceNames();
for(int i = 0; i < resourceNameArray.Length; ++i)
{
if(string.Compare(resourceName, resourceNameArray[i], StringComparison.OrdinalIgnoreCase) == 0)
{
using(StreamReader resourceReader = new StreamReader(assembly.GetManifestResourceStream(resourceNameArray[i])))
{
if(resourceReader != null)
{
return resourceReader.ReadToEnd();
}
}
}
}
return null;
}
#endregion
}
}
300x250
▶ CustomAddIn.cs
using Microsoft.Office.Core;
using System;
namespace TestProject
{
/// <summary>
/// 커스텀 애드인
/// </summary>
public partial class CustomAddIn
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 리본 확장성 객체 생성하기 - CreateRibbonExtensibilityObject()
/// <summary>
/// 리본 확장성 객체 생성하기
/// </summary>
/// <returns>리본 확장성 인터페이스 객체</returns>
protected override IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new RibbonContextMenu();
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 커스텀 애드인 시작시 처리하기 - CustomAddIn_Startup(sender, e)
/// <summary>
/// 커스텀 애드인 시작시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void CustomAddIn_Startup(object sender, EventArgs e)
{
}
#endregion
#region 커스텀 애드인 셧다운시 처리하기 - CustomAddIn_Shutdown(sender, e)
/// <summary>
/// 커스텀 애드인 셧다운시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void CustomAddIn_Shutdown(object sender, EventArgs e)
{
}
#endregion
#region VSTO에서 생성한 코드
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마세요.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(CustomAddIn_Startup);
this.Shutdown += new System.EventHandler(CustomAddIn_Shutdown);
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
댓글을 달아 주세요