如何在Sharepoint Online 2013上更新UserProfile属性(O365)

时间:2014-10-21 01:53:37

标签: c# sharepoint-2013 office365 sharepoint-userprofile

我一直在尝试使用Sharepoint Online 2013上的c#更新用户个人资料属性。 我找不到怎么做,有人能帮助我吗?

以下是我必须做的事情:

我在User Profile上有很多自定义属性,我需要在Provider-Hosted应用程序上编辑它。

我正在使用PersonProperties和PeopleManager来获取数据,那么如何更新呢?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

This will probably be of some help

使用UserProfileService,此类应该有助于解决您的问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using O365ProfileUpdate.UserProfileServiceRef;

    public class O365Helper
    {
        private readonly UserProfileService _userProfileService;
        private readonly Uri _targetAdminSite;

        public O365Helper(UserProfileService userProfileService, Uri targetAdminSite, string adminUsername,
                          string adminPassword)
        {
            _userProfileService = userProfileService;
            _targetAdminSite = targetAdminSite;


            var authenticated = AuthenticateAdministrator(adminUsername, adminPassword);

            if (!authenticated)
                throw new UnauthorizedAccessException("Unable to authenticate administrator");
        }

        public PropertyData GetProfileProperty(string login, string propertyName)
        {
            var memLogin = GetMembershipLogin(login);

            return _userProfileService.GetUserPropertyByAccountName(memLogin, propertyName);
        }

        public bool UpdateProfileProperty(string login, string key, string value)
        {
            try
            {
                var valueData = new ValueData {Value = value};

                var newdata = new PropertyData[1];
                newdata[0] = new PropertyData {Name = key, Values = new ValueData[1]};
                newdata[0].Values[0] = valueData;
                newdata[0].IsValueChanged = true;

                var memLogin = GetMembershipLogin(login);

                _userProfileService.ModifyUserPropertyByAccountName(memLogin, newdata);
            }
            catch
            {
                return false;
            }

            return true;
        }

        private bool AuthenticateAdministrator(string login, string password)
        {
            try
            {
                var securePassword = new SecureString();
                foreach (char c in password)
                {
                    securePassword.AppendChar(c);
                }

                var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

                string authCookieValue = onlineCredentials.GetAuthenticationCookie(_targetAdminSite);
                var cookieVal = authCookieValue.TrimStart("SPOIDCRL=".ToCharArray());

                _userProfileService.CookieContainer = new CookieContainer();
                _userProfileService.CookieContainer.Add(new Cookie(
                                                            "FedAuth",
                                                            cookieVal,
                                                            String.Empty,
                                                            _targetAdminSite.Authority));
            }
            catch
            {
                return false;
            }

            return true;
        }

        private string GetMembershipLogin(string login)
        {
            return "i:0#.f|membership|" + login;
        }
    }

adminUsername和adminPassword是您实例中具有管理权限(可能是您)的用户的凭据

可以在O365 ADMIN站点的UserProfileService.asmx端点中找到UserProfileService