代码改变世界

MOSS User Profile(一):获取和遍历

2007-10-29 16:04  努力学习的小熊  阅读(2906)  评论(5编辑  收藏  举报
 

MOSS User Profile(一):获取和遍历

操作Microsoft Office SharePoint Server 2007User Profile首先需要添加3DLL组件的引用:

Microsoft.SharePoint

Microsoft.Office.Server

System.Web

然后就可以从SPSite类下的UserProfileManager类来进行访问了。

 

获取User Profile有两个方法。

一个是通过枚举场中的所有User Profile。另一个是获取单独的User Profile

但是访问这些User Profile之前,需要在管理中心中的共享服务管理中创建好用户配置文件,其实就是在管理中心中“创建或配置此服务器场的共享服务”,创建一个共享服务管理的网站。然后用户第一次访问他们的“我的网站”时,会自动创建他们的User Profile

在共享服务管理网站中这个位置可以查看所有的用户配置文件:

首先打开“用户配置文件和属性”。
    

在我的实验环境中,可以看到已经存在了6个用户配置文件了,点击下面的查看用户配置文件即可看到所有的用户配置文件。
    

用户配置文件列表

 

下面来介绍一下如何通过对象模型来进行访问。遍历所有的用户配置文件,根据用户名找到对应的用户配置文件。

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);

                    Console.WriteLine("All Profiles:");

                    foreach (UserProfile p in profileManager)

                    {

                        Console.WriteLine("{0} : {1}", p.ID, p.MultiloginAccounts[0]);

                    }

                    Console.WriteLine("");

                    string userName = @"eoffice\Administrator";

                    if (profileManager.UserExists(userName))

                    {

                        UserProfile profile = profileManager.GetUserProfile(userName);

                        Console.WriteLine("Found User Profile {0}", profile.ID);

                        Console.WriteLine("\tPersonal Site: {0} ({1})", profile.PersonalSite.RootWeb.Title, profile.PersonalSite.Url);

                    }

                    else

                    {

                        Console.WriteLine("No account found for " + userName);

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

            Console.ReadLine();

        }

    }

}

 

参考资料:Sams Microsoft SharePoint 2007 Development Unleashed