• SharePoint 2013 开发——获取用户配置文件属性内容(User Profile)


    博客地址:http://blog.csdn.net/FoxDave

    本篇我们应用SharePoint CSOM(.NET)来读取用户配置文件的信息,个人开始逐渐倾向于客户端模型,因为不用远程登录到服务器去开发,在本机就可以玩了。

    打开本地的Visual Studio 2015,选择新建项目,我们新建一个Windows Form应用程序吧,好久没用了,命名为WindowsFormsSPUserProfile。

    应用CSOM,我们首先要对我们的项目添加客户端的引用。右键点击项目节点, 选择添加引用,在弹出窗体的右上角输入sharepoint进行搜索,选择Client、Client.Runtime、Client.UserProfile这三个dll添加,注意版本要选择15.0.0.0。

    然后我们拖一个Label,一个TextBox,一个Button,一个DataGridView到窗体上,作为输入参数,输入网站集的URL,然后用DataGridView显示出所有的用户配置文件。

    双击按钮控件,后台将自动生成button_Click事件方法,我们就在此处写我们的逻辑代码部分。

    首先对输入框的Site URL部分做一下判定,这里用作演示我们就只判断一下非空条件,实际过程可能会涉及到诸如地址是否合法等问题。

    接下来就在else分支中写主要的获取逻辑代码。在这个例子中,我们大致的思路是为:将某个网站集的用户读取出来,进而获取该用户的配置文件的属性集合。首先将用户列表加载到DataGridView中,然后在选择具体的某个用户时显示所选择用户的配置文件的属性集合信息。

    首先将用户信息加载到控件上,WinForm好久不用了,所以方法较为笨拙。

    然后配置DataGridView控件的Click事件,获取选中的行得到用户名信息,进而获得属性集合信息。

    下面附上完整代码,比较粗糙,仅作示例用。

    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint.Client.UserProfiles;
    using System;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsSPUserProfile
    {
        public partial class MainForm : System.Windows.Forms.Form
        {
            ClientContext mClientContext = null;
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void buttonOK_Click(object sender, EventArgs e)
            {
                if (txtSiteURL.Text == "")
                {
                    MessageBox.Show("请输入网站集地址。");
                }
                else
                {
                    //todo
                    //获取输入的网站集URL
                    string siteUrl = txtSiteURL.Text;
                    //构建上下文对象
                    if (mClientContext == null)
                    {
                        mClientContext = new ClientContext(siteUrl);
                    }
                    //获取网站网站集的所有用户
                    UserCollection users = mClientContext.Web.SiteUsers;
                    mClientContext.Load(users);
                    mClientContext.ExecuteQuery();
                    //构建用户表
                    DataTable table = new DataTable();
                    table.Columns.Add("User Name", typeof(string));
                    foreach (User u in users)
                    {
                        DataRow row = table.NewRow();
                        row[0] = u.LoginName;
                        table.Rows.Add(row);
                    }
                    dataGridView.DataSource = table;
                }
            }
    
            private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                //窗体关闭前释放资源
                if (mClientContext != null)
                {
                    mClientContext.Dispose();
                }
            }
    
            private void dataGridView_MouseClick(object sender, MouseEventArgs e)
            {
                //获取双击行的用户
                string userName = dataGridView.SelectedRows[0].Cells[0].Value.ToString();
                //获取人员管理器
                PeopleManager peopleManager = new PeopleManager(mClientContext);
                //获取用户属性对象
                PersonProperties personProperties = peopleManager.GetPropertiesFor(userName);
                mClientContext.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);
                mClientContext.ExecuteQuery();
                StringBuilder propertiesStr = new StringBuilder(1300);
                foreach (var property in personProperties.UserProfileProperties)
                {
                    propertiesStr.AppendLine(string.Format("{0}: {1}", property.Key.ToString(), property.Value.ToString()));
                }
                MessageBox.Show(propertiesStr.ToString());
            }
        }
    }

    本例的运行效果如下所示:


  • 相关阅读:
    EKLM3S8962之LED
    [uClinuxdev] detecting stack overflow
    Eclipse换行符
    EKLM3S8962之OLED
    Windows 环境下 GNU ARM 开发环境建立
    关于阻焊层和助焊层的理解
    MISRA C 2004中文版
    对话框托盘程序实现源码
    VC++中四种进程或线程同步互斥的控制方法
    Windows线程同步与互斥技术总结
  • 原文地址:https://www.cnblogs.com/justinliu/p/5961610.html
Copyright © 2020-2023  润新知