zoukankan      html  css  js  c++  java
  • SharePoint2013的头像显示和读取

     

    前言

    有个时候SP的二次开发,需要用代码获取头像显示,如SP基于AD验证,AD有头像属性,做为头像数据来源(因为Exchange和lync的头像也来自AD),说道这里大家都知道有2种办法,一种从AD属性读取。一个从SP的user profile中去读取(也从AD来)。

    方法1:从AD读取

    AD有个属性叫thumbnailPhoto,而此属性存取的是二进制文件,因此读取的时候需要转换文件显示。首先需要在AD上传头像,可以用powershell或vbscript等很多方式上传照片(上传省略)。下面将采用C#代码如何获取AD的头像,做过验证码的人都知道,可以用Response.BinaryWrite 打印出来,然后图片控件访问这个打印出来的页面

    1. 打印页面代码如下:

    建议一个空白页面,后台代码如下:

    using System;
    
    using System.Collections.Generic;
    
    using System.DirectoryServices;
    
    using System.Linq;
    
    using System.Web;
    
    using System.Web.UI;
    
    using System.Web.UI.WebControls;
    
    namespace WebApplication1
    
    {
    
    public partial class photo : System.Web.UI.Page
    
    {
    
    protected void Page_Load(object sender, EventArgs e)
    
    {
    
    String myUser = Request.QueryString["u"];
    
    if (myUser == null)
    
    Response.Redirect("app_graphics/user.jpg");
    
    Response.ContentType = "image/jpeg";
    
    Response.Clear();
    
    Response.BufferOutput = true;
    
    DirectoryEntry de = new DirectoryEntry("LDAP://127.0.0.1/DC=contoso,DC=com", "mossadmin", "Passw0rd!");
    
    DirectorySearcher search = new DirectorySearcher();
    
    search.SearchRoot = de;
    
    search.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + myUser + "))";
    
    search.PropertiesToLoad.Add("samaccountname");
    
    search.PropertiesToLoad.Add("thumbnailPhoto");
    
    SearchResult user;
    
    user = search.FindOne();
    
    String userName;
    
    if (user == null)
    
    Response.Redirect("app_graphics/user.jpg");
    
    else
    
    userName = (String)user.Properties["sAMAccountName"][0];
    
    try
    
    {
    
    byte[] bb = (byte[])user.Properties["thumbnailPhoto"][0];
    
    Response.BinaryWrite(bb);
    
    Response.Flush();
    
    }
    
    catch
    
    {
    
    Response.Redirect("app_graphics/user.jpg"); 
    
    }
    
    }
    
    }
    
    }
    

    2. 访问页面代码如下:

    建议一个显示页面

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head runat="server">
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    
    <title></title>
    
    </head>
    
    <body>
    
    <form id="form1" runat="server">
    
    <div>
    
    <img src="photo.aspx?u=mossadmin" id="myPhoto" onclick="getPhoto()" 
    
    style="float: left; cursor: pointer;" />
    
    <script type="text/javascript">
    
    function getPhoto() {
    
    document.getElementById("myPhoto").src = "photo.aspx?u=mossadmin";
    
    }
    
    </script>
    
    </div>
    
    </form>
    
    </body>
    
    </html>
    

    注意:还可以点击刷新页面,用onclick事件,如果不需要可以干掉。最终查看显示结果即可

    方法二:从user profile读取头像

    1. 首先需要启动user profile的2个服务,如下图:

    clip_image001

    clip_image003

    SA需要创建,如下图:

    clip_image005

    2. 然后配置同步连接,如下图:

    clip_image007

    3. 配置同步属性,如下图:

    大家知道PictureUrl为SP2013的头像字段,如下图:

    image

    image

    点编辑配置如下:如果没有,选择导入添加属性,对照AD属性thumbnailPhoto

    image

    确定即可

    4. 启动同步服务。如下图:

    image

    5. 查看同步后的结果

    image

    6. 此时用代码读取,不一定读取到,因为还没有存储到个人信息的“用户照片”库里头,

    新的照片会放在“我的网站宿主”的“用户照片”库中,并更新配置文件数据库中的属性值

    具体原理大家可以我博客:

    http://www.cnblogs.com/love007/archive/2012/08/23/2652798.html

    7、运行powershell脚本

    Update-SPProfilePhotoStore -CreateThumbnailsForImportedPhotos 1

    -MySiteHostLocation %MySiteURL%

    image

    在SharePoint Server 2013,运行 Update-SPProfilePhotoStore cmdlet 以确保 SharePoint 配置文件照片存储与 SharePoint Server 2013兼容。只能在从 Office SharePoint Server 2007 的升级完成后使用 Update-SPProfilePhotoStore cmdlet。使用 Update-SPProfilePhotoStore cmdlet 时,会从原始照片创建三个具有可预测大小和名称的缩略图版本,新的照片会放在“我的网站宿主”的“用户照片”库中,并更新配置文件数据库中的属性值。

    在操作期间,原始图像会保持原样。如果特定用户因任何原因而导致操作失败,则会直接移到下一用户。将配置文件图片从一台服务器 URL 迁移到另一台服务器 URL 期间,用户可以使用 OldBaseUriNewBaseUri 参数。您只需指定已从旧 URL 更改为新 URL 的 URL 的起始部分,系统就会尝试为配置文件图片 URL 重新设定基址。

    例如,OldBaseUri: http://server1/my/ProfilePhotos; NewBaseUri: http://server1/my/NewLocation/ProfilePhotos

    然后编写代码读取PictureUrl如下图:

    #region//读取userprofile
    
    ///<summary>
    
    ///根据用户名获取UserProfile
    
    ///</summary>
    
    ///<param name="loginName">登录名</param>
    
    ///<param name="site">当前站点</param>
    
    ///<param name="web">当前web</param>
    
    ///<returns></returns>
    
    public static UserProfile GetUserProfileByLoginName(string loginName, SPSite site, SPWeb web)
    
    {
    
    UserProfile userProfile = null;
    
    SPSecurity.RunWithElevatedPrivileges(delegate()
    
    {
    
    SPUser user = web.EnsureUser(loginName); //当用户不存在时,自动将改用户添加到sharepoint中
    
    SPServiceContext serviceContext = SPServiceContext.GetContext(site);
    
    UserProfileManager profileManager = new UserProfileManager(serviceContext);
    
    if (profileManager.UserExists(loginName))
    
    {
    
    userProfile = profileManager.GetUserProfile(loginName); //读取用户配置文件
    
    }
    
    });
    
    return userProfile;
    
    }
    
    #endregion
    
    读取代码如下:
    
    static void Main(string[] args)
    
    {
    
    using(SPSite site = new SPSite("http://sp2013:8001"))
    
    {
    
    using(SPWeb web = site.OpenWeb(""))
    
    {
    
    UserProfile upf = userBLL.GetUserProfileByLoginName("contoso\mossadmin", site, web);
    
    //
    
    if (upf != null)
    
    {
    
    //byte[] buffer = (byte[])upf[PropertyConstants.PictureUrl][0];
    
    string temp = upf[PropertyConstants.PictureUrl].Value.ToString();
    
    }
    
    }
    
    }
    
    }
    

    clip_image021

  • 相关阅读:
    Inno Setup执行SQL脚本的方法
    批处理命令篇--配置免安装mysql
    nsis安装包_示例脚本语法解析
    全方位掌握nsis脚本
    dos批处理知识
    mysql alter 语句用法,添加、修改、删除字段等
    .Net WebAPI 增加Swagger
    CentOS 7 Docker
    四:Ionic Framework不支持Android4.2.2的解决方法
    二:Ionic Framework支持Android开发
  • 原文地址:https://www.cnblogs.com/love007/p/3871252.html
Copyright © 2011-2022 走看看