如何:创建用户配置文件和组织配置文件

上次修改时间: 2010年3月23日

适用范围: SharePoint Server 2010

下面的代码示例演示如何使用对象模型创建用户配置文件和组织配置文件。在运行代码示例之前,请将 servername 替换为实际值。

另外在您的 Microsoft Visual Studio 项目中添加以下引用:

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • Microsoft.SharePoint

  • System.Web

示例

此示例创建一个用户配置文件。

using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
 
namespace CreateUserProfile
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://<servername>"))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
 
                ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context);
 
                // choose default user profile subtype as the subtype
                string subtypeName = ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User);
                ProfileSubtype subType = psm.GetProfileSubtype(subtypeName);
 
                UserProfileManager upm = new UserProfileManager(context);
 
                // create a user profile and set properties
                UserProfile profile = upm.CreateUserProfile("domain\\alias");
 
                profile.DisplayName = "Display Name";
                profile.ProfileSubtype = subType;
 
                profile.Commit();
            }
        }
    }
}

此示例创建一个组织配置文件

using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
 
namespace CreateOrganization
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://<servername>"))
            {
               SPServiceContext context = SPServiceContext.GetContext(site);
 
                ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context);
 
                // choose default organization profile subtype as the subtype
                string subtypeName = ProfileSubtypeManager.GetDefaultProfileName(ProfileType.Organization);
                ProfileSubtype subType = psm.GetProfileSubtype(subtypeName);
 
                OrganizationProfileManager opm = new OrganizationProfileManager(context);
 
                // choose Root Organization as the parent
                OrganizationProfile parentOrg = opm.RootOrganization;
 
                // create an organization profile and set its display name
                OrganizationProfile profile = opm.CreateOrganizationProfile(subType, parentOrg);
                profile.DisplayName = "Test Org1";
 
                // commit to save changes
                profile.Commit();
            }
        }
    }
}

请参阅

任务

如何:使用 Web 服务修改用户配置文件数据