在SharePoint中无代码开发InfoPath应用: 获取当前用户信息

很多种不同的场景下,会需要得到当前的用户信息,例如需要根据当前用户判断组,进而控制权限。

首先InfoPath提供了一个userName方法,来实现这个目的,不过这个方法的问题是只能获得不包含域名的用户名,例如,一个contoso\danj的用户,这个方法返回的是danj。

对于只有一个域的场景,可以使用concat方法来拼凑出完整的用户名,例如:concat("contoso\", userName())。

如果上面的方法不足以解决你的问题,那就要求助于SharePoint Web Service了。

例如下面两篇文章都是说的这个问题。

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

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

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

这个方法很简单,也很好用,一般情况下这个方法就足够了。

这个方法也有一个缺点就是挑环境,如果你google "GetUserProfileByName infopath error",你会发现有各种各样的错误讨论。基本上都是和Server配置环境相关的。

例如,我朋友的这篇文章曾经也讨论过类似问题: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对象已经可以解析出来了。

最后,来说说这个方法的缺点。

由于我们对数据连接做了修改,所以修改好后,不能再对这个数据连接再次修改了,要不一切都会丢失,你需要从头再来。

 

后记:

不知道有没有人感兴趣那一段定义是从哪里来的,为什么要添加到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>

 

posted @ 2013-10-18 12:00  Lambert Qin  Views(1457)  Comments(0Edit  收藏  举报