SharePoint提供程序托管加载项 - 安装加载项时多次触发应用程序事件接收器。 [英] SharePoint Provider Hosted Add-in - App Event receiver triggered multiple times when Add-in is Installed.
问题描述
我创建了提供商托管的加载项,我想在添加时通过托管代码创建列表在安装中。所以我创建了应用程序事件接收器并在其中添加了列表创建代码。
但是当我安装了加载项时,事件接收器被触发了4次。不要不,为什么会发生这种情况。
请帮助。
您好,
以下AppEventReceiver类代码供您参考。
< pre class ="prettyprint"> public class AppEventReceiver:IRemoteEventService
{
///< summary>
///处理安装或升级应用程序后或卸载应用程序时发生的应用程序事件。
///< / summary>
///< param name =" properties">保存有关应用事件的信息。< / param>
///< returns>保存从应用事件返回的信息。< / returns>
public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{
SPRemoteEventResult result = new SPRemoteEventResult();
String listTitle =" TestListDennis" ;;
Guid listID = Guid.Empty;
switch(properties.EventType)
{
case SPRemoteEventType.AppInstalled:
try
{
listID = CreateList( listTitle,properties);
//取消注释下一行以测试异常处理。
//抛出新的异常("我的测试异常");
//也尝试将前一行放在CreateList调用之上。
}
catch(例外e)
{
//告诉SharePoint取消该事件。
result.ErrorMessage = e.Message;
result.Status = SPRemoteEventServiceStatus.CancelWithError;
//删除列表(如果已创建)。
DeleteList(listID,properties);
}
休息;
case SPRemoteEventType.AppUpgraded:
break;
case SPRemoteEventType.AppUninstalling:
try
{
RecycleList(listTitle,properties);
//取消注释下一行以测试异常处理。
//抛出新的异常("我的测试异常");
//同时尝试将前一行放在RecycleList调用之上。
}
catch(例外e)
{
//告诉SharePoint取消该事件。
result.ErrorMessage = e.Message;
result.Status = SPRemoteEventServiceStatus.CancelWithError;
//恢复列表,如果它在回收站中。
RestoreList(listTitle,properties);
}
休息;
}
返回结果;
}
///< summary>
///此方法是必需的占位符,但不会被应用事件使用。
///< / summary>
///< param name =" properties"> Unused。< / param>
public void ProcessOneWayEvent(SPRemoteEventProperties properties)
{
throw new NotImplementedException();
}
私人Guid CreateList(String listTitle,SPRemoteEventProperties属性)
{
Guid listID = Guid.Empty;
using(ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties,useAppWeb:false))
{
if(clientContext!= null)
{
/ / SharePoint可能会在超时后重试该事件。它
//可能已经在这个处理程序的最后一次尝试中创建了列表,所以
//检查是否已经有一个具有该名称的列表。
列出targetList = GetListByTitle(listTitle,clientContext);
//如果没有,请创建它。
if(targetList == null)
{
ListCreationInformation listInfo = new ListCreationInformation();
listInfo.Title = listTitle;
listInfo.TemplateType =(int)ListTemplateType.GenericList;
listInfo.Url = listTitle;
targetList = clientContext.Web.Lists.Add(listInfo);
clientContext.Load(targetList,l => l.Id);
clientContext.ExecuteQuery();
listID = targetList.Id;
}
}
}
返回listID;
}
private void DeleteList(Guid listID,SPRemoteEventProperties属性)
{
using(ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties,useAppWeb:false))
{
if(clientContext!= null)
{
//检查"尝试"是否块代码足以在错误出现之前创建列表。
列出targetList = GetListByID(listID,clientContext);
//如果是,请删除列表。
if(targetList!= null)
{
targetList.DeleteObject();
clientContext.ExecuteQuery();
}
}
}
}
private List GetListByTitle(String listTitle,ClientContext clientContext)
{
ListCollection allLists = clientContext.Web。名单;
IEnumerable< List> matchingLists = clientContext.LoadQuery(allLists.Where(list => list.Title == listTitle));
clientContext.ExecuteQuery();
返回matchingLists.FirstOrDefault();
}
private List GetListByID(Guid listID,ClientContext clientContext)
{
ListCollection allLists = clientContext.Web.Lists;
IEnumerable< List> matchingLists = clientContext.LoadQuery(allLists.Where(list => list.Id == listID));
clientContext.ExecuteQuery();
返回matchingLists.FirstOrDefault();
}
private void RecycleList(字符串标题,SPRemoteEventProperties属性)
{
using(ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties,useAppWeb:false))
{
if(clientContext!= null)
{
//检查用户是否尚未在SharePoint UI中回收该列表。
列出targetList = GetListByTitle(title,clientContext);
//如果它仍然存在,请回收它。
if(targetList!= null)
{
targetList.Recycle();
clientContext.ExecuteQuery();
}
}
}
}
private void RestoreList(字符串标题,SPRemoteEventProperties属性)
{
using(ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties,useAppWeb:false))
{
if(clientContext!= null)
{
//检查用户是否未手动从回收站中删除了列表
RecycleBinItemCollection bin = clientContext.Web.RecycleBin;
IEnumerable< RecycleBinItem> matchingItems = clientContext.LoadQuery(bin.Where(item => item.Title == title));
clientContext.ExecuteQuery();
RecycleBinItem recycledList = matchingItems.FirstOrDefault();
//如果有,请恢复它。
if(recycledList!= null)
{
recycledList.Restore();
clientContext.ExecuteQuery();
}
}
}
}
}完整示例代码:
https://github.com/SharePoint/PnP/tree/master/Samples/Core.AppEvents
最好的问候,
Dennis
Hi,
I have created provider hosted add-in and I want to create list through managed code when add-in gets installed. So i have created app event receiver and added list creation code in it.
But when i installed add-in, event receiver gets triggered 4 times. Don't no why this is happened.
Kindly Help.
Hi,
The following AppEventReceiver class code for your reference.
public class AppEventReceiver : IRemoteEventService { /// <summary> /// Handles app events that occur after the app is installed or upgraded, or when app is being uninstalled. /// </summary> /// <param name="properties">Holds information about the app event.</param> /// <returns>Holds information returned from the app event.</returns> public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties) { SPRemoteEventResult result = new SPRemoteEventResult(); String listTitle = "TestListDennis"; Guid listID = Guid.Empty; switch (properties.EventType) { case SPRemoteEventType.AppInstalled: try { listID = CreateList(listTitle, properties); // Uncomment the next line to test exception handling. // throw new Exception("My test exception"); // Also try putting the preceding line above the CreateList call. } catch (Exception e) { // Tell SharePoint to cancel the event. result.ErrorMessage = e.Message; result.Status = SPRemoteEventServiceStatus.CancelWithError; // Delete the list if it was created. DeleteList(listID, properties); } break; case SPRemoteEventType.AppUpgraded: break; case SPRemoteEventType.AppUninstalling: try { RecycleList(listTitle, properties); // Uncomment the next line to test exception handling. // throw new Exception("My test exception"); // Also try putting the preceding line above the RecycleList call. } catch (Exception e) { // Tell SharePoint to cancel the event. result.ErrorMessage = e.Message; result.Status = SPRemoteEventServiceStatus.CancelWithError; // Recover the list if it is in Recycle Bin. RestoreList(listTitle, properties); } break; } return result; } /// <summary> /// This method is a required placeholder, but is not used by app events. /// </summary> /// <param name="properties">Unused.</param> public void ProcessOneWayEvent(SPRemoteEventProperties properties) { throw new NotImplementedException(); } private Guid CreateList(String listTitle, SPRemoteEventProperties properties) { Guid listID = Guid.Empty; using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, useAppWeb: false)) { if (clientContext != null) { // SharePoint might be retrying the event after a time-out. It // may have created the list on the last try of this handler, so // check to see if there's already a list with that name. List targetList = GetListByTitle(listTitle, clientContext); // If there isn't one, create it. if (targetList == null) { ListCreationInformation listInfo = new ListCreationInformation(); listInfo.Title = listTitle; listInfo.TemplateType = (int)ListTemplateType.GenericList; listInfo.Url = listTitle; targetList = clientContext.Web.Lists.Add(listInfo); clientContext.Load(targetList, l => l.Id); clientContext.ExecuteQuery(); listID = targetList.Id; } } } return listID; } private void DeleteList(Guid listID, SPRemoteEventProperties properties) { using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, useAppWeb: false)) { if (clientContext != null) { // Check to see if the "try" block code got far enough to create the list before it errored. List targetList = GetListByID(listID, clientContext); // If it did, delete the list. if (targetList != null) { targetList.DeleteObject(); clientContext.ExecuteQuery(); } } } } private List GetListByTitle(String listTitle, ClientContext clientContext) { ListCollection allLists = clientContext.Web.Lists; IEnumerable<List> matchingLists = clientContext.LoadQuery(allLists.Where(list => list.Title == listTitle)); clientContext.ExecuteQuery(); return matchingLists.FirstOrDefault(); } private List GetListByID(Guid listID, ClientContext clientContext) { ListCollection allLists = clientContext.Web.Lists; IEnumerable<List> matchingLists = clientContext.LoadQuery(allLists.Where(list => list.Id == listID)); clientContext.ExecuteQuery(); return matchingLists.FirstOrDefault(); } private void RecycleList(String title, SPRemoteEventProperties properties) { using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, useAppWeb: false)) { if (clientContext != null) { // Check to see that a user hasn't already recycled the list in the SharePoint UI. List targetList = GetListByTitle(title, clientContext); // If its still there, recycle it. if (targetList != null) { targetList.Recycle(); clientContext.ExecuteQuery(); } } } } private void RestoreList(String title, SPRemoteEventProperties properties) { using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, useAppWeb: false)) { if (clientContext != null) { // Check to see that a user hasn't manually deleted the list from the Recycle Bin RecycleBinItemCollection bin = clientContext.Web.RecycleBin; IEnumerable<RecycleBinItem> matchingItems = clientContext.LoadQuery(bin.Where(item => item.Title == title)); clientContext.ExecuteQuery(); RecycleBinItem recycledList = matchingItems.FirstOrDefault(); // If it is there, restore it. if (recycledList != null) { recycledList.Restore(); clientContext.ExecuteQuery(); } } } } }Full example code: https://github.com/SharePoint/PnP/tree/master/Samples/Core.AppEvents
Best Regards,
Dennis
这篇关于SharePoint提供程序托管加载项 - 安装加载项时多次触发应用程序事件接收器。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!