Поделиться через


Работа с профилями пользователей и организации с использованием серверной объектной модели в SharePoint

Узнайте, как программно создать, восстановить и изменить профиль пользователя SharePoint и свойства профиля с помощью серверной объектной модели SharePoint.

Профили пользователей в SharePoint

В SharePoint профили пользователей представляют пользователей SharePoint. Свойства профиля пользователя отображают информацию о пользователях, а также отдельно о свойствах. Например, к свойствам относятся имя учетной записи или электронный адрес пользователя и тип данных свойства. С помощью серверной объектной модели вы можете программными средствами создавать, получать и изменять профили пользователей, подтипы и свойства профилей.

Примечание.

Дополнительные сведения о распространенных задачах программирования для работы с профилями пользователей и API, используемых для выполнения задач, см. в статье Работа с профилями пользователей в SharePoint.

Необходимые компоненты для настройки среды разработки для работы с профилями пользователей с использованием серверной объектной модели SharePoint

Чтобы создать консольное приложение, использующее серверную объектную модель для работы с профилями пользователей и свойствами профиля пользователя, необходимы:

  • SharePoint с профилем, созданным для текущего пользователя;

  • Visual Studio 2012;

  • разрешения на создание, извлечение и изменение объектов профилей пользователей. (Для создания и изменения профилей требуется разрешение Изменение профилей пользователей.)

Создание консольного приложения, работающего с профилями пользователей, с помощью серверной объектной модели SharePoint

  1. Откройте Visual Studio и в меню Файл последовательно выберите элементы Создать и Проект.

  2. В диалоговом окне Новый проект выберите .NET Framework 4.5 из раскрывающегося списка в верхней части окна.

  3. В списке Шаблоны выберите Windows и Консольное приложение.

  4. Назовите проект UserProfilesSSOM и нажмите кнопку OK.

Примечание. Убедитесь, что в свойствах построения проекта не выбран параметр Предпочитать 32-разрядн.

  1. Добавьте ссылки на следующие сборки:
  • Microsoft.Office.Server
  • Microsoft.Office.Server.UserProfiles
  • Microsoft.SharePoint
  • System.Web
  1. Замените содержимое класса Program в примере кода на класс из следующих сценариев:
  1. Чтобы протестировать консольное приложение, в строке меню выберите Отладка и щелкните Начать отладку. Вы можете проверить изменения на странице Управление службой профилей для приложения службы профилей пользователей в центре администрирования.

Пример кода. Создание профилей пользователей с помощью серверной объектной модели SharePoint

В SharePoint профили представляют пользователей SharePoint. Типы и подтипы профилей позволяют разбить профили на группы, например на сотрудников или клиентов. Типы и подтипы профилей используются для установки общих свойств и атрибутов профиля на уровне подтипа. В SharePoint Server есть подтип профиля пользователя по умолчанию.

В следующем примере кода создается объект UserProfile , связанный с подтипом профиля пользователя по умолчанию. Некоторые свойства профиля пользователя автоматически заполняются сведениями, импортированными из каталога, содержащего учетные записи пользователей, например доменные службы Active Directory. Пример кода, который создает пользовательский подтип, см. в разделе ProfileSubtype .

Примечание.

Перед выполнением кода измените значения заполнителей domain\username иservername.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
namespace UserProfilesSSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace "domain\\username" and "servername" with actual values.
            string newAccountName = "domain\\username";
            using (SPSite site = new SPSite("http://servername"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                try
                {

                    // Create a user profile that uses the default user profile
                    // subtype.
                    UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
                    UserProfile userProfile = userProfileMgr.CreateUserProfile(newAccountName);

                    Console.WriteLine("A profile was created for " + userProfile.DisplayName);
                    Console.Read();
                }

                catch (System.Exception e)
                {
                    Console.WriteLine(e.GetType().ToString() + ": " + e.Message);
                    Console.Read();
                }
            }
        }
    }
}

Пример кода: создание свойств профиля пользователя с помощью серверной объектной модели SharePoint

Свойства профиля пользователя описывают личные и организационные сведения о пользователях. Вы можете создать настраиваемое свойство профиля и добавить его в набор свойств профиля SharePoint по умолчанию.

Свойство профиля и его атрибуты представлены набором связанных объектов: объектом CoreProperty , объектом ProfileTypeProperty и объектом ProfileSubtypeProperty .

В следующем примере кода создается coreProperty с типом данных URL-адреса (или при необходимости CoreProperty с многозначным строковым типом данных). Кроме того, он создает ProfileTypeProperty и ProfileTypeProperty , которые определяют доступность, конфиденциальность и другие параметры свойства. Свойство ProfileSubtypeProperty.DefaultPrivacy управляет видимостью свойств и другого содержимого личного сайта. Полный список возможных типов данных для значений свойств профиля см. в разделе PropertyDataType .

Примечание.

Измените значение заполнителя servername перед запуском кода.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
namespace UserProfilesSSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace "servername" with an actual value.
            using (SPSite site = new SPSite("http://servername"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                try
                {
                    ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
                    CorePropertyManager corePropMgr = profilePropMgr.GetCoreProperties();

                    // Create a URL property.
                    CoreProperty coreProp = corePropMgr.Create(false);
                    coreProp.Name = "AppsWebsite";
                    coreProp.DisplayName = "Apps site";
                    coreProp.Type = PropertyDataType.URL;
                    coreProp.Length = 100;
                    corePropMgr.Add(coreProp);

                    //// Create a multivalue property.
                    //// To create this property, comment out the previous
                    //// block of code and uncomment this block of code.
                    //CoreProperty coreProp = corePropMgr.Create(false);
                    //coreProp.Name = "PublishedAppsList";
                    //coreProp.DisplayName = "Published apps";
                    //coreProp.Type = PropertyDataType.StringMultiValue;
                    //coreProp.IsMultivalued = true;
                    //coreProp.Length = 100;
                    //corePropMgr.Add(coreProp);

                    // Create a profile type property and make the core property 
                    // visible in the Details section page.
                    ProfileTypePropertyManager typePropMgr = profilePropMgr.GetProfileTypeProperties(ProfileType.User);
                    ProfileTypeProperty typeProp = typePropMgr.Create(coreProp);
                    typeProp.IsVisibleOnViewer = true;
                    typePropMgr.Add(typeProp);

                    // Create a profile subtype property.
                    ProfileSubtypeManager subtypeMgr = ProfileSubtypeManager.Get(serviceContext);
                    ProfileSubtype subtype = subtypeMgr.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));
                    ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties(subtype.Name);
                    ProfileSubtypeProperty subtypeProp = subtypePropMgr.Create(typeProp);
                    subtypeProp.IsUserEditable = true;
                    subtypeProp.DefaultPrivacy = Privacy.Public;
                    subtypeProp.UserOverridePrivacy = true;
                    subtypePropMgr.Add(subtypeProp);

                    Console.WriteLine("The properties were created.");
                    Console.Read();
                }

                catch (System.Exception e)
                {
                    Console.WriteLine(e.GetType().ToString() + ": " + e.Message);
                    Console.Read();
                }
            }
        }
    }
}

Пример кода: получение и изменение профилей пользователей с помощью серверной объектной модели SharePoint

В следующем примере кода извлекаются все профили пользователей в контексте и изменяется значение свойства DisplayName пользователя. Доступ к большинству свойств профилей осуществляется с помощью UserProfile.Item .

Примечание.

Перед выполнением кода измените значения заполнителей domain\username иservername.


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
namespace UserProfilesSSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace "domain\\username" and "servername" with actual values.
            string targetAccountName = "domain\\username";
            using (SPSite site = new SPSite("http://servername"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                try
                {

                    // Retrieve and iterate through all of the user profiles in this context.
                    Console.WriteLine("Retrieving user profiles:");
                    UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
                    IEnumerator userProfiles = userProfileMgr.GetEnumerator();
                    while (userProfiles.MoveNext())
                    {
                        UserProfile userProfile = (UserProfile)userProfiles.Current;
                        Console.WriteLine(userProfile.AccountName);
                    }

                    // Retrieve a specific user profile. Change the value of a user profile property
                    // and save (commit) the change on the server.
                    UserProfile user = userProfileMgr.GetUserProfile(targetAccountName);
                    Console.WriteLine("\\nRetrieving user profile for " + user.DisplayName + ".");
                    user.DisplayName = "Pat";
                    user.Commit();
                    Console.WriteLine("\\nThe user\\'s display name has been changed.");
                    Console.Read();
                }

                catch (System.Exception e)
                {
                    Console.WriteLine(e.GetType().ToString() + ": " + e.Message);
                    Console.Read();
                }
            }
        }
    }
}

Пример кода: получение и изменение атрибутов для свойств профилей пользователей с помощью серверной объектной модели SharePoint

В следующем примере кода извлекается набор свойств, представляющих конкретное свойство пользователя и его атрибуты, а затем изменяется атрибут CoreProperty.DisplayName , атрибут ProfileTypeProperty.IsVisibleOnViewer и ProfileSubtypeProperty.PrivacyPolicy . Эти изменения применяются к набору свойств глобально. ProfileSubtypeProperty.PrivacyPolicy определяет, должны ли пользователи указывать значение свойства. PrivacyPolicy применяется только к свойствам профиля пользователя.

Примечание.

Измените значение заполнителя servername перед запуском кода.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
namespace UserProfilesSSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace "servername" with an actual value.
            using (SPSite site = new SPSite("http://servername"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                try
                {
                    ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
                    ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile");

                    // Retrieve a specific property set (a profile subtype property and
                    // its associated core and profile type properties).
                    // Changing these properties affects all instances of this property set.
                    ProfileSubtypeProperty subtypeProp = subtypePropMgr.GetPropertyByName(PropertyConstants.Title);
                    CoreProperty coreProp = subtypeProp.CoreProperty;
                    ProfileTypeProperty typeProp = subtypeProp.TypeProperty;

                    Console.WriteLine("Property name: " + coreProp.DisplayName);
                    Console.WriteLine("IsVisibleOnViewer = " + typeProp.IsVisibleOnViewer);
                    Console.WriteLine("PrivacyPolicy = " + subtypeProp.PrivacyPolicy);
                    Console.WriteLine("Press Enter to change the values.");
                    Console.Read();

                    // Change attributes on the properties and save (commit) the changes
                    // on the server.
                    coreProp.DisplayName = "Position";
                    coreProp.Commit();
                    typeProp.IsVisibleOnViewer = true;
                    typeProp.Commit();
                    subtypeProp.PrivacyPolicy = PrivacyPolicy.OptOut;
                    subtypeProp.Commit();
                    Console.WriteLine("The property attributes have been changed.");
                    Console.Read();
                }

                catch (System.Exception e)
                {
                    Console.WriteLine(e.GetType().ToString() + ": " + e.Message);
                    Console.Read();
                }
            }
        }
    }
}

Пример кода: получение и изменение значений для свойств пользователей с помощью серверной объектной модели SharePoint

Следующий пример кода возвращает все свойства типа UserProfile и извлекает значения свойств для определенного пользователя. Затем изменяется однозначное свойство PictureUrl и многозначное свойство PastProjects . Полный список констант имен свойств профиля см. в разделе PropertyConstants .

Примечание.

Перед выполнением кода измените значения заполнителей domain\username, http://servername/docLib/pic.jpg, и servername.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
namespace UserProfilesSSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace "domain\\username," "http://servername/docLib/pic.jpg," and "servername" with actual values.
            string accountName = "domain\\username";
            string newPictureUrl = "http://servername/docLib/pic.jpg";
            using (SPSite site = new SPSite("http://servername"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);

                try
                {
                    UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
                    ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
                   
                    // Retrieve all properties for the "UserProfile" profile subtype,
                    // and retrieve the property values for a specific user.
                    ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile");
                    UserProfile userProfile = userProfileMgr.GetUserProfile(accountName);
                    IEnumerator<ProfileSubtypeProperty> userProfileSubtypeProperties = subtypePropMgr.GetEnumerator();
                    while (userProfileSubtypeProperties.MoveNext())
                    {
                        string propName = userProfileSubtypeProperties.Current.Name;
                        ProfileValueCollectionBase values = userProfile.GetProfileValueCollection(propName);
                        if (values.Count > 0)
                        {

                            // Handle multivalue properties.
                            foreach (var value in values)
                            {
                                Console.WriteLine(propName + ": " + value.ToString());
                            }
                        }
                        else
                        {
                            Console.WriteLine(propName + ": ");
                        }
                    }
                    Console.WriteLine("Press Enter to change the values.");
                    Console.Read();

                    // Change the value of a single-value user property.
                    userProfile[PropertyConstants.PictureUrl].Value = newPictureUrl;

                    // Add a value to a multivalue user property.
                    userProfile[PropertyConstants.PastProjects].Add((object)"Team Feed App");
                    userProfile[PropertyConstants.PastProjects].Add((object)"Social Ratings View web part");

                    // Save the changes to the server.
                    userProfile.Commit();
                    Console.WriteLine("The property values for the user have been changed.");
                    Console.Read();
                }

                catch (System.Exception e)
                {
                    Console.WriteLine(e.GetType().ToString() + ": " + e.Message);
                    Console.Read();
                }
            }
        }
    }
}

См. также