2013年11月11日 星期一

Custom Access SharePoint User Profile Service Application (客製化存取ShaePoint的User Profile Service Application)

SharePoint有一個User Profile Service Application服務,用來識別帳戶的相關資訊,當安裝SharePoint時,假設帳戶是來自AD的話,那麼也可以將AD中所設定的帳戶資料,直接同步至SharePoint 的User Profile Service Application中,後續使用者就可以直接在SharePoint中編輯這些資訊。

那麼,假設我們想要設計一些維護介面,或是客製化應用程式去存取User Profile Service Application的話,該怎麼做了? 以下步驟說明開發方式:

1. 首先我們先建立一個SharePoint專案,接著在此專案中加入對應的dll組件

1

2. 新增一個ApplicationPage.aspx用來測試功能,新加入控制項:一個按鈕;一個ListBox

3

3. 切換至.cs中,先匯入欲使用的Namespace

2

4. 在Page_Load中使用UserProfileManager物件載入相對應的Profile,並且我們取出AccountName、WorkEMail、PersonalSpace等屬性資料呈現:

4

5. 先看一下執行的結果,如下圖所示,在ListBox中會寫出目前在網站中有使用Profile的資料:

7

6. 取出Profile資料後,接著我們亦可撰寫更新Profile的程式碼,在按鈕的Click事件中,撰寫如下的程式碼,透過Web.CurrentUser.LoginName取得目前登入的使用者帳號,然後當作參數丟給UserProfileManager的GetUserProfile方法,即可取得目前登入者的Profile物件,最後更新Profile時記得要呼叫Commit方法:

5

7. 執行結果,使用LauraG登入,且按下按鈕後,EMail就會被更新了

7