如何:创建新的活动类型

上次修改时间: 2010年3月30日

适用范围: SharePoint Server 2010

Microsoft SharePoint Server 2010 允许您通过创建新活动类型扩展"我的网站"宿主上的"我的新闻源"页面,您可以基于新活动类型创建事件并将其插入到用户新闻源中。下面的示例方法演示如何创建新 ActivityType 对象并将其与 ActivityTemplate 对象相关联,然后通过该 ActivityTemplate 将其与资源文件相关联,资源文件定义事件在新闻源中的显示方式。要获取演示如何执行本主题中所含任务的代码示例,请参阅代码示例:多播活动事件控制台应用程序代码示例:向您的同事活动事件发送链接。本主题假定您已经创建资源 (.resx) 文件并将其部署到 \Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Resources 目录中,同时您已经向您的 Microsoft Visual Studio 2010 项目添加了以下引用:

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • System.Web

创建新活动类型

每个 ActivityType 对象都包含一个 ActivityTemplates 属性。此属性包含 ActivityTemplate 对象的集合。ActivityTemplate 类包含连接到属性以显示资源文件中定义的模板的属性,这些模板必须使用构建用于在活动源中创建和收集活动事件的任何自定义应用程序创建和部署。有关如何创建资源文件的说明,请参阅创建资源文件(该链接可能指向英文页面)。因为特定 ActivityType 只创建一次,所以您的解决方案应确保它不会创建重复项。以下示例方法演示如何创建新的 ActivityType 对象并定义其显示方式。

private void SetupCustomActivity(SPSite site)
{

    string resFile = "name of your resource (.resx) file";
    //Get an SPServiceContext from the site.
    SPServiceContext context = SPServiceContext.GetContext(site);
    //Create a UserProfileManager.
    UserProfileManager upm = new UserProfileManager(context);
    //Get the current user's profile.
    UserProfile p = upm.GetUserProfile(true);

    //Create an activity manager.
    ActivityManager am = new ActivityManager(p, context);
    //Make sure that the current user has permission to do this
    bool hasrights = am.PrepareToAllowSchemaChanges();

    // Create an activity application.
    ActivityApplication app = am.ActivityApplications["Name of Activity Application"];
    if (app == null)
    {
      app = am.ActivityApplications.Create("Name of Activity Application");
      app.Commit();
    }

    //Create an ActivityType; check to see whether it already exists.
    ActivityType activityType = app.ActivityTypes["Name of Activity Type"];
    if (activityType == null)
    {
      activityType = app.ActivityTypes.Create("Name of Activity Type");
//Associate the ActivityType with the resource file that you have deployed and with the
//localized string name stored in the resource file.
      activityType.ActivityTypeNameLocStringResourceFile = resFile;
      activityType.ActivityTypeNameLocStringName = "Activity Type Localized String Name";
      activityType.IsPublished = true;
      activityType.IsConsolidated = true;
      activityType.Commit();
    }
    //Create a single value ActivityTemplate; check to see whether it already exists.
    ActivityTemplate urlTemplateSV = activityType.ActivityTemplates[ActivityTemplatesCollection.CreateKey(false)];
    if (urlTemplateSV == null)
    {
      urlTemplateSV = activityType.ActivityTemplates.Create(false);
//Associate the template with the resource file that you have deployed and with the
//localized string name stored in the resource file.
      urlTemplateSV.TitleFormatLocStringResourceFile = resFile;
      urlTemplateSV.TitleFormatLocStringName = "Activity Template Localized String Name ";
      urlTemplateSV.Commit();
     }
}

请参阅

引用

Microsoft.Office.Server.ActivityFeed

概念

如何:获取用户的事件

如何:在用户的新闻流中创建和插入事件

其他资源

Microsoft SharePoint Server 2010:活动源控制台应用程序(该链接可能指向英文页面)

创建资源文件(该链接可能指向英文页面)