javascript SharePoint 获取当前用户属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5358035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:55:26  来源:igfitidea点击:

SharePoint get current user attribute

javascriptsharepointsoapcaml

提问by Eric Di Bari

I have a page that needs to retrieve the department for the logged-in user. Ideally I'd like to be able to do it through a JavaScript SOAP CAML query. I have the user's id (which I'm assuming doubles as the GUID), so it seems like a simple matter of retrieving the row that matches the ID.

我有一个页面需要为登录用户检索部门。理想情况下,我希望能够通过 JavaScript SOAP CAML 查询来完成。我有用户的 ID(我假设它是 GUID 的两倍),所以检索与 ID 匹配的行似乎很简单。

I'm looking into using the 'GetUserProfileByIndex' or 'GetUserProfileByGuid' function in SOAP, but I can't seem to find any solid documentation or decent examples using them. I'm hoping to do something like this in JavaScript - (which isn't working):

我正在考虑在 SOAP 中使用“GetUserProfileByIndex”或“GetUserProfileByGuid”函数,但我似乎找不到任何可靠的文档或使用它们的体面示例。我希望在 JavaScript 中做这样的事情 - (这是行不通的):

 var userId = 194;
 "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
     <soapenv:Body> \
         <GetUserProfileByIndex  xmlns='http://microsoft.com/webservices/SharePointPortalServer/UserProfileService'> \
             <index>userId</index> \
         </GetUserProfileByIndex > \
     </soapenv:Body> \
 </soapenv:Envelope>";

回答by Tom Resing

I recommend you check out the jQuery Library for SharePoint 2007 and 2010 called SPServices. $().SPServices.SPGetCurrentUsercan retrieve the Department in an effecient 4 lines of code:

我建议您查看名为 SPServices 的 SharePoint 2007 和 2010 jQuery 库。$().SPServices.SPGetCurrentUser可以用有效的 4 行代码检索部门:

var thisDepartment = $().SPServices.SPGetCurrentUser({
fieldName: "Department",
debug: false
});

回答by Eric Di Bari

I've never answered my own question, but I managed to come up with a simple solution. Here is how I went about it

我从来没有回答过我自己的问题,但我设法想出了一个简单的解决方案。这是我如何去做的

  1. When a user is logged in, there is a global JavaScript variable that includes that user's ID, - __spUserId
  2. The user profile list is accessible via a CAML query through SOAP, it's titled 'User Information List'.
  3. When accessing this list, you have to set the top-level site to point to _catalogs instead of Lists (or whatever it is exactly). You do this by adding the element _catalogs \
  4. Since it is accessing the user profile list as a list and not through a userprofile function, set the query type (?) to 'GetListItems'
  1. 当用户登录时,有一个包含该用户 ID 的全局 JavaScript 变量 - __spUserId
  2. 用户配置文件列表可通过 SOAP 的 CAML 查询访问,其标题为“用户信息列表”。
  3. 访问此列表时,您必须将顶级站点设置为指向 _catalogs 而不是 Lists(或确切的任何内容)。您可以通过添加元素 _catalogs \
  4. 由于它将用户配置文件列表作为列表而不是通过 userprofile 函数访问,因此将查询类型 (?) 设置为“GetListItems”

Overall, here is the SOAP call and CAML query

总的来说,这里是 SOAP 调用和 CAML 查询

$(document).ready(function() {
    var soapEnv =
        "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soapenv:Body> \
                <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                    <listName>User Information List</listName> \
                    <topLevelSite>_catalogs</topLevelSite> \
                    <query> \
                        <Query> \
                            <Where> \
                                <Contains> \
                                    <FieldRef Name='ID' /> \
                                        <Value Type='Text'>"+_spUserId+"</Value> \
                                </Contains> \
                            </Where> \
                        </Query> \
                    </query> \
                    <viewFields> \
                        <ViewFields> \
                            <FieldRef Name='Name' /> \
                            <FieldRef Name='Department' /> \
                            <FieldRef Name='ID' /> \
                        </ViewFields> \
                    </viewFields> \
                    <query> \
                        <Query /> \
                    </query> \
                </GetListItems> \
            </soapenv:Body> \
        </soapenv:Envelope>";

    $.ajax({
        url: "/_vti_bin/Lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResult,
        contentType: "text/xml; charset=\"utf-8\""
    });
});

回答by djeeg

You should probably use GetUserProfileByName, site collection ids can't be used to lookup anything in the user profile store.

您可能应该使用GetUserProfileByName,网站集 ID 不能用于在用户配置文件存储中查找任何内容。

http://amitkumarmca04.blogspot.com/2008/07/how-access-sharepoint-moss-user-profile.html

http://amitkumarmca04.blogspot.com/2008/07/how-access-sharepoint-moss-user-profile.html