代码改变世界

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

  努力学习的小熊  阅读(1585)  评论(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

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示