SharePoint-기능 \ 이벤트 수신기
이 장에서 우리는 code handle. 코드 핸들은 기능이 활성화 또는 비활성화 될 때 발생하는 이벤트입니다. 즉, 우리는Feature Receivers.
지난 장에서 만든 Visual Studio 프로젝트에는 하나의 기능이 있었고 활성화되었을 때 연락처 목록, SitePage 및 SitePage에 대한 링크를 프로비저닝했습니다.
그러나 기능이 비활성화되면 SharePoint는 링크 만 제거하고 SitePage 및 연락처 목록은 그대로 유지됩니다.
기능이 비활성화 될 때 코드를 작성하여 원하는 경우 목록과 페이지를 제거 할 수 있습니다. 이 장에서는 기능이 비활성화되었을 때 콘텐츠와 요소를 제거하는 방법을 배웁니다.
기능에 대한 이벤트를 처리하려면 Feature Receiver.
Step 1 − 기능 수신기를 얻으려면 솔루션 탐색기에서 기능을 마우스 오른쪽 버튼으로 클릭 한 다음 Add Event Receiver.
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
namespace FeaturesAndElements.Features.Sample {
/// <summary>
/// This class handles events raised during feature activation, deactivation,
installation, uninstallation, and upgrade.
/// </summary>
/// <remarks>
/// The GUID attached to this class may be used during packaging and should not be modified.
/// </remarks>
[Guid("e873932c-d514-46f9-9d17-320bd3fbcb86")]
public class SampleEventReceiver : SPFeatureReceiver {
// Uncomment the method below to handle the event raised after a feature has been activated.
//public override void FeatureActivated(SPFeatureReceiverProperties properties)//{
//
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
//public override void FeatureDeactivating(SPFeatureReceiverProperties properties)// {
//
}
// Uncomment the method below to handle the event raised after a feature has been installed.
//public override void FeatureInstalled(SPFeatureReceiverProperties properties)// {
//
}
// Uncomment the method below to handle the event raised before a feature is uninstalled.
//public override void FeatureUninstalling(SPFeatureReceiverProperties properties)// {
//
}
// Uncomment the method below to handle the event raised when a feature is upgrading.
//public override void FeatureUpgrading(SPFeatureReceiverProperties
properties, string upgradeActionName,
System.Collections.Generic.IDictionary<string, string> parameters) // {
//
}
}
}
우리가 얻는 것은 SPFeatureReceiver.
SharePoint에는 처리 할 수있는 다양한 종류의 이벤트에 대한 다양한 클래스가 있습니다. 예를 들어 목록의 이벤트, 목록 항목의 이벤트, 사이트의 이벤트가 있습니다. 특정 이벤트 수신기에서 파생 된 클래스를 만든 다음 해당 클래스 내의 메서드를 재정 의하여 이벤트를 처리 할 수 있습니다.
기능의 이벤트는 다음과 같은 경우에 사용됩니다.
- Activated
- Deactivated
- Installed
- Uninstalled
- Upgrading
다음으로 해당 클래스를 특정 항목에 대한 이벤트 처리기로 연결해야합니다. 예를 들어 목록 이벤트를 처리하는 이벤트 처리기가있는 경우 해당 클래스를 목록에 연결해야합니다.
따라서 두 가지 기능을 처리합니다.
기능이 활성화되고
비활성화 될 때.
Step 2 − 우리는 FeatureActivated 및 FeatureDeactivated 방법은 다음과 같습니다.
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
namespace FeaturesAndElements.Features.Sample {
/// <summary>
/// This class handles events raised during feature activation, deactivation,
installation, uninstallation, and upgrade.
/// </summary>
/// <remarks>
/// The GUID attached to this class may be used during packaging and should
not be modified.
/// </remarks>
[Guid("e873932c-d514-46f9-9d17-320bd3fbcb86")]
public class SampleEventReceiver : SPFeatureReceiver {
private const string listName = "Announcements";
public override void FeatureActivated(SPFeatureReceiverProperties properties) {
var web = properties.Feature.Parent as SPWeb;
if (web == null) return;
var list = web.Lists.TryGetList(listName);
if (list != null) return;
var listId = web.Lists.Add(listName, string.Empty,
SPListTemplateType.Announcements);
list = web.Lists[listId];
list.OnQuickLaunch = true;
list.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
var web = properties.Feature.Parent as SPWeb;
if (web == null) return;
var list = web.Lists.TryGetList(listName);
if (list == null) return;
if (list.ItemCount == 0) {
list.Delete();
}
}
}
}
Note −
기능이 활성화되면 공지 사항 목록이 생성됩니다.
기능이 비활성화되면 공지 사항 목록이 비어 있는지 확인하고 비어있는 경우 삭제합니다.
Step 3− 이제 프로젝트를 마우스 오른쪽 버튼으로 클릭하고 배포를 선택합니다. 다음 배포 충돌 경고가 표시됩니다.
![](https://post.nghiatu.com/assets/tutorial/sharepoint/images/deployment_conflicts.jpg)
Visual Studio는 연락처라는 목록을 만들려고하지만 이미 연락처라는 사이트에 목록이 있다고 알려줍니다. 기존 목록을 덮어 쓸지 묻는 메시지가 표시되며이 경우에는Resolve.
Step 4 − SharePoint로 돌아간 다음 사이트를 새로 고치고 Site Actions → Site settings → Manage site features → Sample feature.
![](https://post.nghiatu.com/assets/tutorial/sharepoint/images/sample_features.jpg)
왼쪽 창에 공지 사항 목록이 없음을 알 수 있습니다.
Step 5 − 샘플 기능을 활성화하면 Announcements 목록이 표시되지만 지금은 비어 있습니다.
![](https://post.nghiatu.com/assets/tutorial/sharepoint/images/announcements_list.jpg)
Note − 샘플 기능을 비활성화하면 공지 사항 목록이 사라진다는 것을 알 수 있습니다.
Step 6− 기능을 다시 활성화하겠습니다. 공지 사항으로 이동 한 다음 새 공지 사항을 추가합니다. 이 테스트를 호출 한 다음 저장을 클릭합니다.
![](https://post.nghiatu.com/assets/tutorial/sharepoint/images/new_announcement.jpg)
공지 사항 아래에 테스트 파일이 표시됩니다.
![](https://post.nghiatu.com/assets/tutorial/sharepoint/images/test_file_under_announcements.jpg)
이제 공지 사항을 비활성화하면 공지 사항 목록이 비어 있지 않기 때문에 그대로 유지되는 것을 볼 수 있습니다.
![](https://post.nghiatu.com/assets/tutorial/sharepoint/images/deactivate_announcements.jpg)