User Profile Service или как создавать свойства профилей из приложения

Появилась задача создания свойств пользователей централизованным решением, можно было бы создавать и на сайте администрирования, но для простоты обращения с этим приложением в будущем. Чтоб можно было добавлять в каждой среде одинаковые свойства и не смотреть постоянно как они там в профиле называются и т.д.
Короче для удобства пользования =)

Было сделано следующее:

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
namespace UserServiceProp
{
    class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(args[0]);
                string url = doc.DocumentElement.Attributes["Url"].Value;
                using (SPSite site = new SPSite(url))
                {
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    ProfileSubtypeManager pstm = ProfileSubtypeManager.Get(context);
                    UserProfileConfigManager upcm = new UserProfileConfigManager(context);
                    ProfilePropertyManager ppm = upcm.ProfilePropertyManager;
                    System.Xml.XmlNodeList sections = doc.DocumentElement.SelectNodes("//Site/Sections/Section");
                    foreach (System.Xml.XmlNode sectionNode in sections)
                    {
                        string sectionName = sectionNode.Attributes["Name"].Value;
                        string sectionDName = sectionNode.Attributes["Title"].Value;
                        //Создаем новую секцию
                        PropertyCollection pcol = upcm.GetPropertiesWithSection();
                        Property mysection = pcol.Create(true);
                        mysection.Name = sectionName;
                        mysection.DisplayName = sectionDName;
                        mysection.Commit();
                        System.Xml.XmlNodeList properties = sectionNode.SelectNodes("Properties/Property");
                        foreach (System.Xml.XmlNode propertyNode in properties)
                        {
                            string propertyName = propertyNode.Attributes["Name"].Value;
                            string propertyDName = propertyNode.Attributes["Title"].Value;
                           
                            // Создаем новое свойство
                            CorePropertyManager cpm = ppm.GetCoreProperties();
                            CoreProperty cp = cpm.Create(false);
                            cp.Name = propertyName;
                            cp.DisplayName = propertyDName;
                            cp.Type = PropertyDataType.StringSingleValue;
                            cp.Length = 100;
                            cpm.Add(cp);
                           
                            // Создаем тип свойства
                            ProfileTypePropertyManager ptpm = ppm.GetProfileTypeProperties(ProfileType.User);
                            ProfileTypeProperty ptp = ptpm.Create(cp);
                            ptpm.Add(ptp);
                            // Создаем подтип свойства
                            ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context);
                            ProfileSubtype ps = psm.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));
                            ProfileSubtypePropertyManager pspm = ps.Properties;
                            ProfileSubtypeProperty psp = pspm.Create(ptp);
                            psp.PrivacyPolicy = PrivacyPolicy.OptIn;
                            psp.DefaultPrivacy = Privacy.Public;
                            pspm.Add(psp);

                        }
                    }
                    Console.WriteLine("Cвойства созданы");
                    Console.Read(); 
                }
            }
            catch (DuplicateEntryException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

А в конфигурационном файле (типа: config.xml) уже описание всех секции (групп свойств) и свойст пользователя.

Вид конфигурационного файла:

<?xml version="1.0" ?>
<Site Url="
<URL Site>">
  <Sections>
    <Section Name="Testsss" Title="Т1">
      <Properties>
        <Property Name="Pr1s" Title="ПР1"></Property>
        <Property Name="Pr2s" Title="ПР2"></Property>
      </Properties>
    </Section>
  </Sections>
</Site>


Комментарии

Популярные сообщения из этого блога

Полезности под рукой: Приятные SharePoint уведомления для пользователя

Полезности под рукой: Отправка писем при помощи REST API в SharePoint 2013