【问题标题】:How to update UserProfile Properties on Sharepoint Online 2013 (O365)如何在 Sharepoint Online 2013 (O365) 上更新 UserProfile 属性
【发布时间】:2014-10-21 10:31:23
【问题描述】:

我一直在尝试在 Sharepoint Online 2013 上使用 c# 更新用户配置文件属性。 我找不到怎么做,有人可以帮我吗?

这是我必须做的:

我在用户配置文件上有很多自定义属性,我需要在提供商托管的应用程序上对其进行编辑。

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

感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    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

    【讨论】:

    • 感谢业力的回答。我会试试这个,让你知道!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    相关资源
    最近更新 更多