Partilhar via


How to: Create User Profiles and Organization Profiles

Applies to: SharePoint Server 2010

The following code examples demonstrate how to create user profiles and organization profiles by using the object model. Before running the code examples, replace servername with an actual value.

Also add the following references in your Microsoft Visual Studio project:

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • Microsoft.SharePoint

  • System.Web

Example

This example creates a user profile.

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

This example creates an organization profile

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

See Also

Tasks

How to: Use the Web Service to Modify User Profile Data