不少種不一樣的場景下,會須要獲得當前的用戶信息,例如須要根據當前用戶判斷組,進而控制權限。 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方法。使用這個方法有些複雜,下面會作介紹:
<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>
<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>
<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>
最後,來講說這個方法的缺點。
因爲咱們對數據鏈接作了修改,因此修改好後,不能再對這個數據鏈接再次修改了,要不一切都會丟失,你須要從頭再來。
後記:
不知道有沒有人感興趣那一段定義是從哪裏來的,爲何要添加到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:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>