简体   繁体   English

尝试使用 SPServices 获取用户的组列表

[英]Trying to get List of Groups for a user using SPServices

I came across this code and I'm trying to use it in SP 2010 Foundation.我遇到了这段代码,并试图在 SP 2010 Foundation 中使用它。 When I load the page, I get the error saying "User not found".当我加载页面时,出现“找不到用户”的错误消息。 I'm using Jquery 1.10.X and the latest SPServices version.我正在使用 Jquery 1.10.X 和最新的 SPServices 版本。 I assume it has something to do with the "userLoginName" but I'm having trouble figuring it out.我认为它与“userLoginName”有关,但我无法弄清楚。

$(document).ready(function() {
  //Populate the users pick list
  var strHTMLSiteUsers = "";
  $().SPServices({
      operation: "GetUserCollectionFromSite",
      async: false,
      completefunc: function(xData, Status) {
        $(xData.responseXML).find("User").each(function() {
          strHTMLSiteUsers += "<option value='" + $(this).attr("LoginName") + "'>" + $(this).attr("Name") + "</option>";
        });
        $("#my_SiteUsers").append(strHTMLSiteUsers);
      }
  });
  RefreshGroupLists();
});

function RefreshGroupLists(){
  var strHTMLAvailable = "";
  var strHTMLAssigned = "";
  var arrOptionsAssigned = new Array();
  var intOpts = 0;
  var booMatch;
  var booErr = "false";

  $("#my_SPGroupsAssigned").html("");
  $("#my_SPGroupsAvailable").html("");

  if($("#my_SiteUsers").attr("value") == 0){
    alert("You must select a user");
    return;
  }

  //Populate the Groups Assigned
  $().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $("#my_SiteUsers").attr("value"),
      async: false,
      completefunc: function(xData, Status) {
        $(xData.responseXML).find("errorstring").each(function() {
          alert("User not found");
          booErr = "true";
          return;
        });
        $(xData.responseXML).find("Group").each(function() {
          strHTMLAvailable += "<option value='" + $(this).attr("Name") + "'>" + $(this).attr("Name") + "</option>";
          arrOptionsAssigned[intOpts] = $(this).attr("Name");
          intOpts = intOpts + 1;
        });
        $("#my_SPGroupsAssigned").append(strHTMLAvailable);
      }
  });

  //Populate available site groups
  if(booErr == "false"){
    $().SPServices({
        operation: "GetGroupCollectionFromSite",
        async: false,
        completefunc: function(xData, Status) {
          $(xData.responseXML).find("Group").each(function() {
            booMatch = "false";
            for (var i=0;i<=arrOptionsAssigned.length;i++){
              if($(this).attr("Name") == arrOptionsAssigned[i]){
                booMatch = "true";
                break;
              }
            }
            if(booMatch == "false"){
              strHTMLAssigned += "<option value='" + $(this).attr("Name") + "'>" + $(this).attr("Name") + "</option>";
            }
          });
          $("#my_SPGroupsAvailable").append(strHTMLAssigned);
        }
    });
  }
}

function AddGroupsToUser(){
  var i;

  if($("#my_SiteUsers").attr("value") == 0){
    alert("You must select a user");
    return;
  }

  if($("#my_SPGroupsAvailable").val() == null){
    alert("You haven't selected any groups to add");
    return;
  }
  else{
    var arrGroups = $("#my_SPGroupsAvailable").val();
    for (i=0;i<arrGroups.length;i++){
      $().SPServices({
          operation: "AddUserToGroup",
          groupName: arrGroups[i],
          userLoginName: $("#my_SiteUsers").attr("value"),
          async: false,
          completefunc: null
      });
    }
    RefreshGroupLists();
  }
}

function RemoveGroupsFromUser(){
  var i

  if($("#my_SiteUsers").attr("value") == 0){
    alert("You must select a user");
    return;
  }

  if($("#my_SPGroupsAssigned").val() == null){
    alert("You haven't selected any groups to remove");
    return;
  }
  else{
    var arrGroups = $("#my_SPGroupsAssigned").val();
    for (i=0;i<arrGroups.length;i++){
      $().SPServices({
          operation: "RemoveUserFromGroup",
          groupName: arrGroups[i],
          userLoginName: $("#my_SiteUsers").attr("value"),
          async: false,
          completefunc: null
      });
    }
    RefreshGroupLists();
  }
}

This is the source这是来源

You should use Firebug (addon of Firefox) to look at the AJAX requests that are done. 您应该使用Firebug(Firefox的附件)查看已完成的AJAX请求。 Like that you can see the exact error message. 这样,您可以看到确切的错误消息。

I know that usually you have to pass an username with "-1;#" in front of it to make Sharepoint to recognize it as an username (for example: "-1;#john_doe@test.com")... maybe it's the reason?! 我知道通常您必须在用户名前传递一个带有“ -1;#”的用户名,以使Sharepoint能够将其识别为用户名(例如:“-1; #john_doe@test.com”)...也许这是原因吗?

Instead of SPServices you could use SharepointPlus and its groupMembers function: 可以使用SharepointPlus及其groupMembers函数代替SPServices:

$SP().groupMembers("my group", function(members) {
  for (var i=0; i < members.length; i++) console.log(members[i]);
  // -> {ID:"1234", Name:"Doe, John", LoginName:"mydomain\john_doe", Email:"john_doe@rainbow.com"}
});

This solution was written using an earlier version of jQuery;此解决方案是使用早期版本 jQuery 编写的; the identification of an element's "properties" (vs its "attributes") changed at some point.元素的“属性”(相对于它的“属性”)的标识在某个时候发生了变化。 I had your exact problem and this was the issue:我遇到了你的确切问题,这就是问题所在:

If you're using jQuery 1.10.X+ you'll need to replace every occurrence of .attr("value") with .prop("value") in your source code for this to work.如果您使用的是 jQuery 1.10.X+,则需要在源代码中将每次出现的.attr("value")替换为.prop("value")才能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM