SharePoint Tip #23: Creating a Personal Site

The User Profile is the central concept behind the personalization features of SharePoint (MOSS only). In addition to the User Profile, each user can have a My Site. A My Site is composed of:

  • A Profile Page that shows the user profile data, according to the user viewing it and the privacy options of each profile field. You can find that page (Person.aspx) in the root web site of the My Site host site collection (e.g http://mysites/Person.aspx?accountname=domain\username).
  • A Personal Site which is a site collection where you can show personal and public information and documents.

The Personal Site is automatically provisioned for you when you try to access your My Site (using the My Site link or clicking your username). However, you might want to create it programmatically for one or more users, before they even try to access it. See below an example of how to do it.

// open a site collection
using (SPSite site = new SPSite("http://myportal"))
{
    // get the server context
    ServerContext context = ServerContext.GetContext(site);

    // create the profile manager object
    UserProfileManager profileManager = new UserProfileManager(context);

    // get the user profile
    UserProfile profile = profileManager.GetUserProfile("domain\\username");

    // create the user's personal site
    profile.CreatePersonalSite();
}

The really important line of code here is the last one. There rest was already explained in one of my previous posts (WSS Tip #20: Get User Profile).

SharePoint Tip #22: Create a User Profile

The best way to create SharePoint User Profiles is to import them from somewhere (Active Directory for instance). This is specially true if you want to create a lot of them at once. However, if you really want to do it manually, one at a time, you can. Just do it like in the snippet below.

// open a site collection
using (SPSite site = new SPSite("http://myportal"))
{
    // get the server context
    ServerContext context = ServerContext.GetContext(site);

    // create the profile manager object
    UserProfileManager upm = new UserProfileManager(context);

    // create the user profile
    UserProfile profile = upm.CreateUserProfile("domain\\username");
}

Just like getting a User Profile from the User Profile Store, to create one you need a UserProfileManager object. After that, you just need to call the CreateUserProfile method and pass it the user account name (including the domain).

Attention: User Profiles are a MOSS only feature. You cannot use them with WSS.

SharePoint Tip #21: Importing User Profiles

One of the best things of SharePoint's User Profiles, is that the profile data can be imported from several data sources (Active Directory or other LDAP directories, Databases and web services). It gets even better since you can schedule these imports to be performed automatically every given period of time.

But what if you want to start a profile import programmatically, without having to wait for the next scheduled import? Well, that can be done using the following code snippet.

// open a site collection
using (SPSite site = new SPSite("http://myportal"))
{
    // get the server context
    ServerContext context = ServerContext.GetContext(site);

    // create the profile configuration manager object
    UserProfileConfigManager upcm = new UserProfileConfigManager(context);

    // check if there is already an import in progress
    if (!upcm.IsImportInProgress())
    {
        // if not, start a new one
        // pass "true" as a parameter, if the import is incremental
        // pass "false" as a parameter, if you want a full import
        upcm.StartImport(true);
    }
}

The profile import process is managed using the UserProfileConfigManager object, which can be created using a ServerContext object. Before starting an import, using the StartImport method, you should always check if there is already an import in progress (using the IsImportInProgress method).

The StartImport method accepts a single boolean parameter which specifies if you want an incremental import (true) or a full import (false).

If necessary, you can build a while cycle to wait for the import to be over, testing the IsImportInProgress return value, until it returns false.

Attention: User Profiles are a MOSS only feature. You cannot use them with WSS.

SharePoint Tip #20: Get User Profile

User Profiles are a central concept to Office SharePoint Server 2007. They store all the information about the SharePoint users in a central location, the User Profile Store, allowing that information to be indexed and searched. The User Profile store can be populated and synchronized with several data sources:

  • Active Directory directory services
  • LADP Directory (other than Active Directory)
  • Databases and web services, via Business Data Catalog
  • Custom sources, via the User Profiles object model

You can also add users to it manually. Additionally, you can add custom properties to the User Profile and map some of them to the data sources you have. This great flexibility allows a multitude of scenarios. In this post I'll show you how you can get access a User Profile programmatically.

Everything is starts with the UserProfileManager object. Here is how you use it to get a UserProfile object.

// open a site collection
using (SPSite site = new SPSite("http://myportal"))
{
    // get the server context
    ServerContext context = ServerContext.GetContext(site);

    // create the profile manager object
    UserProfileManager profileManager = new UserProfileManager(context);

    // get the user profile
    UserProfile profile = profileManager.GetUserProfile("domain\\username");
}

There constructor for the UserProfileManager class is overloaded, which means there are other ways to create an instance but this is the way I usually do it. The GetUserProfile method is also overloaded, allowing you to get a user profile using its ID or just getting the current user's profile.

Attention: User Profiles are a MOSS only feature. You cannot use them with WSS.