在SharePoint中無代碼開發InfoPath應用: 獲取當前用戶信息

不少種不一樣的場景下,會須要獲得當前的用戶信息,例如須要根據當前用戶判斷組,進而控制權限。 html

首先InfoPath提供了一個userName方法,來實現這個目的,不過這個方法的問題是隻能得到不包含域名的用戶名,例如,一個contoso\danj的用戶,這個方法返回的是danj。 工具

對於只有一個域的場景,可使用concat方法來拼湊出完整的用戶名,例如:concat("contoso\", userName())。 google

若是上面的方法不足以解決你的問題,那就要求助於SharePoint Web Service了。 spa

例以下面兩篇文章都是說的這個問題。 設計

Thttp://jaliyaudagedara.blogspot.com/2011/05/getting-current-users-sharepoint-group.html orm

http://info.akgroup.com/blog-0/bid/69277/InfoPath-Restrict-visibility-to-users-in-a-SharePoint-Group xml

無一例外的,都是使用了UserProfileService.asmx Web Service中的GetUserProfileByName方法。 htm

這個方法很簡單,也很好用,通常狀況下這個方法就足夠了。 對象

這個方法也有一個缺點就是挑環境,若是你google "GetUserProfileByName infopath error",你會發現有各類各樣的錯誤討論。基本上都是和Server配置環境相關的。 blog

例如,我朋友的這篇文章曾經也討論過相似問題:http://www.cnblogs.com/fanwenxuan/archive/2011/03/14/1984159.html

 

若是你不幸的沒法使用上面方法,能夠嘗試使用UserGroup.asmx中的GetCurrentUserInfo方法。使用這個方法有些複雜,下面會作介紹:

  1. InfoPath添加一個數據鏈接,選擇Receive data.

  1. 數據源選擇來自於SOAP Web Service

  1. 輸入SOAP Web Service路徑,http://yoursite/_vti_bin/usergroup.asmx

  1. 在解析出來的方法列表中找到GetCurrentUserInfo方法。

  1. 根據須要選擇是否容許離線使用數據,通常狀況下,不要選中。

  1. 給這個數據連接命名,根據狀況選擇是否一旦表單打開就自動獲取數據,因爲這個方法通常是其餘的使用的基礎,通常都選中。

  1. 這樣,就添加好了GetCurrentUserInfo數據鏈接。

  1. 添加好的數據鏈接在高級視圖中以下所示,這個時候其實根本沒法使用,由於默認InfoPath沒有對這個數據進行解析。

  1. 下面來手動增長這個數據解析。先將InfoPath模板導出爲源文件。

  1. 導出的文中,關於這個鏈接的大概會有以下類型的文件。

  1. 打開GetCurrentUserInfo1.xsd文件(也多是其餘文件,也能夠尋找文件包含<s:element name="GetCurrentUserInfoResponse">)。
  2. 在文件的開始位置。緊接着<s:import namespace="http://www.w3.org/2001/XMLSchema"></s:import>的後面。

<s:schema elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/sharepoint/soap/directory/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://schemas.microsoft.com/sharepoint/soap/directory/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/">

    <s:import namespace="http://www.w3.org/2001/XMLSchema"></s:import>

<s:element name="GetUserCollectionFromSite">

        <s:complexType></s:complexType>

    </s:element>

  1. 插入以下的一段定義,以下所示。

<s:schema elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/sharepoint/soap/directory/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://schemas.microsoft.com/sharepoint/soap/directory/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/">

<s:import namespace="http://www.w3.org/2001/XMLSchema"></s:import>

<s:complexType name="User">

<s:attribute name="Notes" type="s:string"></s:attribute>

<s:attribute name="Name" type="s:string"></s:attribute>

<s:attribute name="IsSiteAdmin" type="s:string"></s:attribute>

<s:attribute name="Sid" type="s:string"></s:attribute>

<s:attribute name="Flags" type="s:string"></s:attribute>

<s:attribute name="ID" type="s:string"></s:attribute>

<s:attribute name="LoginName" type="s:string"></s:attribute>

<s:attribute name="Email" type="s:string"></s:attribute>

<s:attribute name="IsDomainGroup" type="s:string"></s:attribute>

</s:complexType>

<s:element name="GetUserCollectionFromSite">

        <s:complexType></s:complexType>

    </s:element>

  1. 在這個文件中查找<s:element name="GetUserInfo">,並插入一段定義,以下高亮所示

    <s:element name="GetUserInfo">

        <s:complexType>

            <s:sequence>

                <s:element minOccurs="0" maxOccurs="1" name="userLoginName" type="s:string"></s:element>

            <s:element name="User" type="tns:User" />

            </s:sequence>

        </s:complexType>

    </s:element>

  1. 保存文件。在一樣的文件夾下,右鍵單擊manifest文件,選擇Design打開。

  1. 從新在高級視圖中看這個數據源,你能夠發現User對象已經能夠解析出來了。

  1. 添加控件,就能夠看到結果了。

最後,來講說這個方法的缺點。

因爲咱們對數據鏈接作了修改,因此修改好後,不能再對這個數據鏈接再次修改了,要不一切都會丟失,你須要從頭再來。

 

後記:

不知道有沒有人感興趣那一段定義是從哪裏來的,爲何要添加到GetUserInfo裏面。下面簡單介紹下。

那一段定義來自於GetCurrentUserInfoX.xsd的最後一個文件(X根據這個數據鏈接修改的次數會有不一樣,通常爲2,5,8….)。

還有一個出處是微軟SharePoint協議文檔:[MS-UGS]: UserGroup Web Service Protocol.

 

在協議文檔中,GetCurrentUserInfoResponse有一個GetCurrentUserInfo的說明,看起來應該是返回這個字段纔對,爲何跑去修改GetUserInfo

在協議文檔中,有一段附錄的WSDL說明:

<s:element name="GetCurrentUserInfoResponse">

<s:complexType>

<s:sequence>

<s:element name="GetCurrentUserInfoResult">

<s:complexType>

<s:sequence>

<s:element name="GetUserInfo">

<s:complexType>

<s:sequence>

<s:element name="User" type="tns:User" />

</s:sequence>

</s:complexType>

</s:element>

</s:sequence>

</s:complexType>

</s:element>

</s:sequence>

</s:complexType>

</s:element>

或者利用第一篇文章提到的工具,也能夠看到相似的結果。

至於微軟爲何這麼設計,我就不知道了。

因此應該找GetUserInfo去修改。不過爲何不能直接把GetCurrentUserInfo1.xsd文件的<s:any></s:any>替換掉?我也不知道,替換掉會報錯的。

    <s:element name="GetCurrentUserInfoResponse">

        <s:complexType>

            <s:sequence>

                <s:element minOccurs="0" maxOccurs="1" name="GetCurrentUserInfoResult">

                    <s:complexType>

                        <s:sequence>

                    <s:any></s:any>

                    </s:sequence>

                    </s:complexType>

                </s:element>

            </s:sequence>

        </s:complexType>

    </s:element>

相關文章
相關標籤/搜索