技术开发 频道

MOSS User Profile:代码创建用户配置文件


【IT168技术文档】

  户配置文件的创建在前面介绍过,只要用户登录过自己的“我的网站”就会被创建好了。其实,通过对象模型也是可以提前创建好用户的配置文件的,可以省去用户必须登录我的网站才能创建自己的配置文件的过程。而且在对象模型中创建用户的配置文件其实也是比较简单的。只需要具备权限的用户并提供需要创建配置文件的用户帐户名就OK了。

  同前面的方法一样,首先要从上下文获取这个很重要的管理类的对象UserProfileManager。
SPSite site = new SPSite("http://mossweb:1111/sites/Publish"); ServerContext context = ServerContext.GetContext(site); UserProfileManager profileManager = new UserProfileManager(context);
  然后,我们有几个方法可以使用:

  这里我们用到的是第一个方法CreateUserProfile和最后一个UserExists。

  代码如下:
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using System.Web; using Microsoft.Office.Server; using Microsoft.Office.Server.Administration; using Microsoft.Office.Server.UserProfiles; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { try { using (SPSite site = new SPSite("http://mossweb:1111/sites/Publish")) { ServerContext context = ServerContext.GetContext(site); UserProfileManager profileManager = new UserProfileManager(context); string accountId = @"eoffice"administrator"; if (!profileManager.UserExists(accountId)) { UserProfile profile = profileManager.CreateUserProfile(accountId); if (profile == null) Console.WriteLine("Could not create profile, an error occurred."); else Console.WriteLine("User profile created."); } else Console.WriteLine("User profile for account " + accountId + " already exists."); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); } } }
0
相关文章