代码改变世界

MOSS User Profile(二):用户配置文件属性

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

MOSS User Profile(二):用户配置文件属性

上文讲述了如何创建用户配置文件并遍历或获取指定用户的配置文件,本文将讲述用户配置文件属性的操作。

用户配置文件和其他传统的应用系统类似,都有关于用户方面的描述,例如传统应用系统中的用户数据表中就会有对于用户的姓名、年龄、职位、部门、Email、生日等这些信息的描述,用户配置文件也是如此,默认情况下会有一些预设好的描述字段来存储这些信息,但是这些属性字段也可以进行自定义,创建一些自定义的配置文件属性字段来存储需要的信息。

查看这些属性字段可以在创建的共享服务管理网站中找到。

打开共享服务管理网站,在“用户配置文件和我的网站”部分找到“用户配置文件和属性”的设置,进入。
    

找到“查看用户配置文件”链接,进入。
    

这里可以看到所有用户配置文件的列表。在“帐户名”上点击鼠标,会出现一个下拉菜单,选中“编辑”即可进入到此用户的配置文件属性编辑页面。
    

这里可以看到默认提供的配置文件属性字段。在这里可以对其进行修改。

 

下面通过代码来访问用户配置文件的属性。

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

                    UserProfile user1 = profileManager.GetUserProfile(@"eoffice\user1");

                    Console.WriteLine("Profile {0}", user1.MultiloginAccounts[0]);

                    foreach (Property prop in profileManager.Properties)

                    {

                        Console.WriteLine("\t{0} : {1}", prop.DisplayName, RenderProperty(user1, prop));

                    }

                    Console.ReadLine();

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

            Console.ReadLine();

        }

 

        static string RenderProperty(UserProfile profile, Property prop)

        {

            UserProfileValueCollection values = profile[prop.Name];

            if (values.Value == null)

                return "(NULL)";

            if (prop.IsMultivalued)

            {

                StringBuilder sb = new StringBuilder();

                foreach (object o in values)

                {

                    sb.AppendFormat("{0} ", o);

                }

                return sb.ToString();

            }

            else

            {

                return values.ToString();

            }

        }

    }

}

 

修改配置文件的属性有两种方法,分别针对不同类型的属性,修改预定义的属性则既可以通过对象模型来修改
    

也可以通过另一种直接写属性的方法修改

user1["Department"] = "软件中心";

这里发现一个问题,就是如果写中文名称部门会提示“未定义属性: 部门。管理员必须使用配置文件管理工具创建此属性。”这个错误,只能用其英文名称取已存在的属性。

记得修改后,调用user1.Commit();方法将修改的内容提交,不然不会有效果的。

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

                    UserProfile user1 = profileManager.GetUserProfile(@"eoffice\user1");

                    Console.WriteLine("Profile {0}", user1.MultiloginAccounts[0]);

                    foreach (Property prop in profileManager.Properties)

                    {

                        Console.WriteLine("\t{0} : {1}", prop.DisplayName, RenderProperty(user1, prop));

                    }

                    user1[PropertyConstants.Office].Value = null;

                    user1[PropertyConstants.Department].Value = "软件中心";

                    user1["FirstName"].Value = "测试用户";

                    user1.Commit();

                    Console.WriteLine("Success!");

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

            Console.ReadLine();

        }

    }

}

 参考资料:Sams Microsoft SharePoint 2007 Development Unleashed