代码改变世界

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

2007-11-06 10:43  努力学习的小熊  阅读(1577)  评论(0编辑  收藏  举报
 

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

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

同前面的方法一样,首先要从上下文获取这个很重要的管理类的对象UserProfileManager

SPSite site = new SPSite("http://mossweb:1111/sites/Publish");

ServerContext context = ServerContext.GetContext(site);

UserProfileManager profileManager = new UserProfileManager(context);

然后,我们有几个方法可以使用:

方法名

描述

CreateUserProfile

创建一个用户配置文件

GetChanges

获取用户配置文件的修改历史记录

GetUserProfile

获取一个用户配置文件

RemoveUserProfile

移除一个用户配置文件

UserExists

判断用户是否已经具有配置文件

这里我们用到的是第一个方法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();

        }

    }

}

文中代码来源自参考资料。

参考资料:Sams Microsoft SharePoint 2007 Development Unleashed