How to: Create User Profiles and Organization Profiles

Switch View :
ScriptFree
How to: Create User Profiles and Organization Profiles

Published: May 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

Change History

Date

Description

Reason

May 2010

Initial publication

Community Content

Morten Andreasen
OrganizationProfileManager.RootOrganization is null
If you provision your User Profile Service Application using powershell, your RootOrganization property will return null, untill you edit and save it in the UI

John Paul Cheerangal
The Code Needs the Modification to Update the User Profile Subtype.
Acording to the Code mentioned in the link  http://msdn.microsoft.com/en-us/library/ms545122.aspx , it needs a slight Modification, Or else when you try to update the userprofile with your custom subtype, even after giving the commit(), The profile will not get updated with your custom subtype

Below is the Modification. Consider you have subtype named "XXXX" and your below code should be

using (SPSite site = new SPSite("http://<servername>"))
{
SPServiceContext context = SPServiceContext.GetContext(site);

ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context);

// choose the custom user profile subtype as the subtype
string subtypeName = "XXXX";
ProfileSubtype subType = psm.GetProfileSubtype(subtypeName);

UserProfileManager upm = new UserProfileManager(context);

// get a user profile and set properties
UserProfile profile = upm.GetUserProfile("domain\\alias");
===========================================================================================
add the below code to Update your Custom Subtype

profile.DisplayName = profile.DisplayName + " updated ";

Note :- Later Use some logic to remove the string "Updated"(the String can be anything)

Reason :- This is because when you try to do a Commit action for the User profile, The OOM looks for the Change happened for any of the
property, only when the Change occures in the Property the commit action will works, which will also help i updating your subtype as well.
=========================================================================================



profile.ProfileSubtype = subType;

profile.Commit();
}

Regards
John Paul Cheerangal