代码示例:向您的同事活动事件发送链接
上次修改时间: 2015年3月9日
适用范围: SharePoint Server 2010
本文内容
SharePoint 活动简介
创建和发布活动事件
生成并运行示例
该示例演示如何创建 ActivityType 对象和 ActivityEvent 对象,以及如何将新 ActivityEvent 对象插入到用户的新闻源中。它创建了一个简单的表单,使用户能够通过几个简单步骤与其同事共享链接。用户界面包括一个弹出框,用户可使用它将 URL 和注释插入到其同事的新闻源中。
示例代码提供者:Catapult Systems 的 Mathew McDermott | Critical Path Training, LLC 的 Andrew Connell
通过下载 Microsoft SharePoint 2010 软件开发工具包(该链接可能指向英文页面) (SDK) 或通过从代码库(该链接可能指向英文页面)下载示例,将此代码示例安装到您自己的计算机上。如果下载的是 SharePoint 2010 SDK,则示例将安装在您的文件系统中的以下位置:C:\Program Files\Microsoft SDKs\SharePoint 2010\Samples\Social Data and User Profiles。
SharePoint 活动简介
Microsoft SharePoint Server 2010 通过"我的网站宿主"上的新闻源提供受监控的事件和通知的合并。此新页面和新功能使用户能够在一个位置关注整个企业的更新。用户可以"选择加入"新闻和友好的信息显示,其格式采用用户图片和指向相关信息的链接。这使新闻源成为对其他应用程序通知有用的位置。每个新闻源项显示 ActivityEvent 对象的内容。
活动模板
活动的设计以一个简单的句子开头。例如,如果我们创建一个新 ActivityType 对象来指示同事希望与您共享链接,则这个句子与以下内容类似:
Jim Brown has sent you the following link of interest: Great SharePoint Article
活动的变量部分是 Jim Brown,链接文本是"Great SharePoint Article"。在活动类型模板的开发中,使用标记替换变量部分,如以下示例模板中所示:
{Publisher} has sent you the following link of interest: {Link}
您开发的自定义应用程序使用所需信息填充标记。模板文本作为资源文件部署到服务器以便于本地化为所需的目标语言。有关创建和使用资源文件的详细信息,请参阅使用资源文件(该链接可能指向英文页面)。
部署此解决方案
表 1 详细介绍了此活动源解决方案所需的组件。
表 1. 活动源解决方案所需的组件
项目资产 |
目标 |
---|---|
项目程序集 (*.dll) |
全局程序集缓存 |
活动模板资源文件 (*.resx) |
资源目录 |
应用程序页面 (*.aspx) |
模板/布局/MSDN |
部署活动资产后,新的 ActivityType 对象及相关的 ActivityTemplate 对象必须由使 ActivityType 对象和 ActivityTemplate 对象与资源文件相关联的事件接收器来创建。示例项目在部署解决方案后执行此操作。
private void SetupCustomActivity(SPSite site)
{
try
{
//Get a Service Context from the site.
SPServiceContext context = SPServiceContext.GetContext(site);
//We need a User Profile Manager to work with Activities.
UserProfileManager upm = new UserProfileManager(context);
//Grab the current user's profile.
UserProfile p = upm.GetUserProfile(true);
Debug.WriteLine("Got the User Profile for: " + p.DisplayName);
//Grab an activity manager.
ActivityManager am = new ActivityManager(p, context);
//Ensure that we have permission to do this.
bool hasrights = am.PrepareToAllowSchemaChanges();
Debug.WriteLine("Does installer have Admin rights to change the schema? " + hasrights);
// Create our activity application.
Debug.WriteLine("Create our Activity Application");
ActivityApplication app = am.ActivityApplications["Send Link to Colleague"];
if (app == null)
{
app = am.ActivityApplications.Create("Send Link to Colleague");
app.Commit();
}
Debug.WriteLine("Send Link to Colleague App id: " + app.ApplicationId);
//Create our Colleagues Activity Type, but first check to see whether it already exists.
Debug.WriteLine("Activity Type: SendColleagueUrlNote");
ActivityType activityType = app.ActivityTypes["SendColleagueUrlNote"];
if (activityType == null)
{
activityType = app.ActivityTypes.Create("SendColleagueUrlNote");
activityType.ActivityTypeNameLocStringResourceFile = resFile;
activityType.ActivityTypeNameLocStringName = "ActivityFeed_SendColleagueUrlNote_Type_Display";
activityType.IsPublished = true;
activityType.IsConsolidated = true;
activityType.Commit();
}
//SendColleagueUrlNote Activity Template.
Debug.WriteLine("SendColleagueUrlNote Activity Single Value Template");
ActivityTemplate urlTemplateSV = activityType.ActivityTemplates[ActivityTemplatesCollection.CreateKey(false)];
if (urlTemplateSV == null)
{
urlTemplateSV = activityType.ActivityTemplates.Create(false);
urlTemplateSV.TitleFormatLocStringResourceFile = resFile;
urlTemplateSV.TitleFormatLocStringName = "ActivityFeed_SendColleagueUrlNote_SV_Template";
urlTemplateSV.Commit();
}
创建和发布活动事件
示例应用程序的设计目标是为用户提供一个简单表单,以使他们能够通过几个简单步骤与其同事共享链接。在"标签和注释"功能之后建模的"Send Link to Colleagues"功能向个人菜单中添加了一个菜单项,如图 1 中所示。
图 1."Send link to Colleagues"菜单详细信息
在用户选择此菜单项后,将显示"Send link to my Colleagues"表单。
图 2."Send Link to Colleague"页
用于启动表单的 ECMAScript(JavaScript、JScript)提供当前窗口的 URL 并将其作为参数传递给表单,表单在调试部分显示它传递的值。为将自定义活动发送给同事,用户在文本框中添加注释并单击"Post"。
创建活动
示例应用程序在 CreateActivity 方法中为用户创建活动。此方法采用活动所有者的用户配置文件和活动发布者的用户配置文件作为参数。然后,它为每个用户创建一个 MinimalPerson 对象,并将这两个对象传递给 CreateActivityEvent 方法。活动链接是基于 URL 和用户提供的注释建立的。
public ActivityEvent CreateActivity(UserProfile ownerProfile, UserProfile publisherProfile, ActivityType activityType, string linkText, string linkUrl)
{
//Get the Owner and Publisher Profile.
Entity ownerMP = new MinimalPerson(ownerProfile).CreateEntity(activityManager);
Entity publisherMP = new MinimalPerson(publisherProfile).CreateEntity(activityManager);
//Create the activity and set some properties.
ActivityManager am = new ActivityManager();
ActivityEvent activityEvent = ActivityEvent.CreateActivityEvent(am, activityType.ActivityTypeId, ownerMP, publisherMP);
Debug.WriteLine("Got my ActivityEvent");
activityEvent.Name = activityType.ActivityTypeName;
activityEvent.ItemPrivacy = (int)Privacy.Public;
activityEvent.Owner = ownerMP;
activityEvent.Publisher = publisherMP;
//Create the link for the activity.
Link link = new Link();
link.Href = linkUrl;
link.Name = linkText;
activityEvent.Link = link;
//Save your work.
activityEvent.Commit();
Debug.WriteLine("End of Activity event: " + activityType.ActivityTypeName);
return activityEvent;
}
将活动事件发送给用户的同事
此解决方案使用 ActivityFeedGatherer 对象提供的批处理方法来将活动多播给用户的所有同事。
//Send activity to the Owner’s Colleagues.
//Create Dictionary objects that will be output parameters for the batch gatherer methods.
Dictionary<long, MinimalPerson> owners;
Dictionary<long, List<MinimalPerson>> colleaguesOfOwners;
//Create List objects that contain the ActivityEvent and its publisher.
List<ActivityEvent> events = new List<ActivityEvent>();
events.Add(activityEvent);
List<long> publishers = new List<long>();
publishers.Add(activityEvent.Owner.Id);
ActivityFeedGatherer.GetUsersColleaguesAndRights(activityManager, publishers, out owners, out colleaguesOfOwners);
Dictionary<long, List<ActivityEvent>> eventsConsolidatedPerOwner;
ActivityFeedGatherer.MulticastActivityEvents(activityManager, events, colleaguesOfOwners, out eventsConsolidatedPerOwner);
List<ActivityEvent> eventsConsolidated;
ActivityFeedGatherer.CollectActivityEventsToConsolidate(eventsConsolidatedPerOwner, out eventsConsolidated);
ActivityFeedGatherer.BatchWriteActivityEvents(eventsConsolidated, 0, events.Count);
生成并运行示例
以下步骤演示如何在开发或测试网站上测试此项目。
生成示例
创建一个名为 Microsoft.SDK.Server.Samples 的文件夹,然后在其中解压缩 MSDN SharePoint 2010 Activity Feeds - Code.zip 文件。
启动 Visual Studio 2010,然后打开您在步骤 1 中创建的文件夹中的 SendColleagueURLNote.sln 文件。
在"属性"窗口中,指定您的开发或测试网站的绝对地址的网站 URL 值(例如 http://mysite/)。确保包含结束左斜线。
在 SendColleagueURLNote.EventReceiver.cs 文件中的 SendColleagueURLNoteEventReceiver 方法中,指定您的 SharePoint 管理中心网站的 URL。
将对以下程序集的引用添加到项目中(如果它们尚不存在):
Microsoft.SharePoint.dll
Microsoft.Office.Server.dll
Microsoft.Office.Server.UserProfiles.dll
将 Resources 目录中的资源文件(URLNote.en-US.resx 和 URLNote.resx)添加到您的 \Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Resources 目录中。
在"生成"菜单上,选择"部署解决方案"。生成完成后,应用程序页面将安装到您的开发或测试网站上。
运行示例
在生成并部署了解决方案后,转到您的 SharePoint Server 2010 网站上的一个页面。在页面右上角的下拉菜单上,选择"Send Link to Colleagues"。
在文本框中键入可选注释,然后单击"Post"以将注释和链接插入到您的同事的新闻源中。
请参阅
引用
Microsoft.Office.Server.ActivityFeed