کد:
http://msdn.microsoft.com/en-us/library/ms545122.aspx

Creating a user profile in Microsoft Office SharePoint Server 2007 is similar to creating a user profile in Microsoft Office SharePoint Portal Server 2003. Unless configured differently, Office SharePoint Server 2007 automatically detects the domain controller and imports the user information from the Active Directory directory service. However, you can configure the data source configuration settings to import from a domain controller hosting Active Directory, an LDAP server, or Business Data Catalog. The first code example creates a user profile from the master connection configured on the server.
Sometimes, you may want to have different values for certain properties in the user profile store, while importing other properties from Active Directory or an LDAP server. Office SharePoint Server 2007 provides an overloaded method for creating user profiles that takes an additional string parameter that represents the preferred name of the user. This method allows you to easily provide a different preferred name for a user when creating the user profile. The second code example shows you how to override the preferred name of a user and also overwrite the cell phone number property.
Before running the code examples, replace servername, domainname, preferredname, username, and nnnnnnnnnn with actual values.
Also add the following references in your Microsoft Visual Studio project:

  • Microsoft.Office.Server
  • Microsoft.SharePoint
  • System.Web

Example

This example creates a user profile from the master connection configured on the server. It displays the message 'User profile created' after a successful execution.


کد:
//Example #1
//Creates a user profile. Obtains the property values from the default 
//domain controller or the master connection that is configured on the 
//server
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using System.Web;

namespace UserProfilesConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (SPSite site = new SPSite("http://servername"))
                {
                    ServerContext context =
                            ServerContext.GetContext(site);
                    UserProfileManager profileManager = new
                        UserProfileManager(context);
                    string sAccount = "domainname\\username";

                    if (!profileManager.UserExists(sAccount))
                    {
                    UserProfile profile = profileManager.CreateUserProfile(sAccount);

                        if (null == profile)
                            throw new Exception("Failed to Create User with account :" + sAccount);

                        else
                            Console.WriteLine("User profile created.");
                    }
                    else
                        Console.WriteLine("User profile already exists.");

                }
            }
            catch (UserNotFoundException exception)
            {
                Console.WriteLine(exception.ToString());
            }

        }
    }


}
This example overrides the preferred name of a user and also overwrites the cell phone number property. It displays the message 'User profile created' after a successful execution.


کد:
//Example #2
//Creates a user profile. Obtains the property values from the default 
//domain controller or the master connection that is configured on the 
//server.
//Also overwrites the Preferred Name and the Cell Phone property for the user.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using System.Web;

namespace UserProfilesApp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (SPSite site = new SPSite("http://servername"))
                {
                    ServerContext context = ServerContext.GetContext(site);

                    UserProfileManager profileManager = new UserProfileManager(context);
                    UserProfile u = null;
                    string sAccount = "domainname\\username";
                    string sPrefered = "preferedname";

                    if (!profileManager.UserExists(sAccount))
                    {
                        u = profileManager.CreateUserProfile(sAccount, sPrefered);
                        u[PropertyConstants.CellPhone].Value = "nnnnnnnnnn";
                        u.Commit();


                        if (null == u)
                            throw new Exception("Failed to Create User with account :" +
                                sAccount);
                        else
                            Console.WriteLine("User profile created.");

                    }
                    else
                        Console.WriteLine("User profile already exists.");



                }
            }
            catch (UserNotFoundException exception)
            {
                Console.WriteLine(exception.ToString());
            }

        }
    }


}

See Also

Other Resources

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




موضوعات مشابه: