Share via


Adding Users to a Cross-Site Group

Adding Users to a Cross-Site Group

To add multiple users to a cross-site group in Microsoft Windows SharePoint Services, use either the AddUserToGroup method, when adding new or existing individual users, or the AddUserCollectionToGroup method, when adding a collection of existing users.

  • Start Microsoft Visual Studio .NET.
  • On the File menu, point to New, and then click Project.
  • In the New Project dialog box, click Visual C# Projects or Visual Basic Projects, and then select the Windows Application template.
  • Type "AddUsers" as the name and specify the location for the project files, and then click OK.
To add a reference to the UserGroup Web Service
  • In Solution Explorer, right-click Web References, and then click Add Web Reference.

  • In the Add Web Reference dialog box, enter the URL of the Lists Web Service on the server running Windows SharePoint Services:

    http://Server_Name/_vti_bin/UserGroup.asmx

    and then click Add Reference.

To add users to a group from a site or subsite

Use the GetUserCollectionFromSite method or the GetUserCollectionFromWeb method to return all the users from a site or subsite. Then iterate through the collection of users and add each one to a specified group by using the AddUserCollectionToGroup method.

  • Open Form1 in Design view, display the Toolbox, and then drag a Button control onto the form.

  • Double-click the button to display the code editor, and add the following lines of code to the Click event handler:

    // Declare and initialize a variable for the UserGroup Web Service.
    Server_Name.UserGroup userGroup = new Server_Name.UserGroup();
    
    /* Authenticate the current user by passing their default
    credentials to the Web Service from the system credential cache and, if adding users
    from a subsite, set the Url property for the service.*/
    userGroup.Credentials = System.Net.CredentialCache.DefaultCredentials;
    userGroup.Url = "http://Server_Name/Subsite_Name/_vti_bin/UserGroup.asmx";
    
    /* Declare an XmlNode object and initialize it with the XML response from either
    the GetUserCollectionFromWeb method or the GetUserCollectionFromSite method*/
    System.Xml.XmlNode usersSite = userGroup.GetUserCollectionFromWeb();
    
    /* Declare another XmlNode object and initialize it with the first child node
    returned from the above method.*/
    System.Xml.XmlNode userNode = usersSite.FirstChild;
    
    /*Pass the first child node and its contents as the XMLNode object parameter
    for the method.*/
    userGroup.AddUserCollectionToGroup("Cross-site_Group_Name",userNode);
    
  • On the Debug menu, click Start to test the form. Click the button to add the users to the specified group.

To add a single user to a cross-site group and display the group members

Use the AddUserToGroup method to add a new or existing user to a cross-site group, and use the GetUserCollectionFromGroup method to return information about all the users in a cross-site group.

  • After creating a Windows Application project and adding a Web reference as described in the previous example, add five TextBox controls, a Label control, and a Button control to the form.

  • Double-click the button to display the code editor, and add the following lines of code to the Click event handler:

    /* Declare and initialize a variable for the UserGroup Web Service, and authenticate the current user by passing their default
    credentials to the Web Service from the system credential cache*/
    Server_Name.UserGroup userGroup = new Server_Name.UserGroup();
    userGroup.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
    // Gather data from the text boxes.
    string groupName = textBox1.Text;
    string userName = textBox2.Text;
    string userLoginName = textBox3.Text;
    string userEmail = textBox4.Text;
    string userNotes = textBox5.Text;
    
    //Add the specified user to the group.
    userGroup.AddUserToGroup(groupName, userName, userLoginName, userEmail, userNotes);
    
    /* Declare an XmlNode object and initialize it with the XML response from the
    GetUserCollectionFromGroup method, declare a second XmlNode object and
    initialize it with the first child of the first node returned, and then declare and initialize
    an XmlNodeList object with the child nodes of the second node.*/
    System.Xml.XmlNode usersNode1 = userGroup.GetUserCollectionFromGroup(groupName);
    System.Xml.XmlNode usersNode2 = usersNode1.FirstChild;
    System.Xml.XmlNodeList userNodes = usersNode2.ChildNodes;
    
    /* Iterate through the collection of user nodes and parse out the Name, LoginName, Email,
    and Notes attribute values for each item and then display the values returned.*/
    foreach (System.Xml.XmlNode user in userNodes)
    {
        string name = user.Attributes["Name"].Value;
        string loginName = user.Attributes["LoginName"].Value;
        string email = user.Attributes["Email"].Value;
        string notes = user.Attributes["Notes"].Value;
    
        label1.Text += "\n\n" + name + " : " + loginName + " : " + email + " : " + notes;
    }
    
  • On the Debug menu, click Start to test the form. Click the button to add the user to the specified group and to display the group members.

To add users to a group from information in an XML file

Load the collection of user data into a DataSet object, and then use the AddUserToGroup method to iterate through the collection and add each user to the group.

  • After creating a Windows application and adding a Web reference as described in the previous example, add a TextBox control and a Button control to the form.

  • Double-click the button to display the code editor, and add the following lines of code to the Click event handler:

    /* Declare and initialize a variable for the UserGroup Web Service, and authenticate the current user by passing their default
    credentials to the Web Service from the system credential cache*/
    Server_Name.UserGroup userGroup = new Server_Name.UserGroup();
    userGroup.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
    /* Instantiate a DataSet control, declare and initialize a string specifying the full path to the
    XML file to use as a source*/
    DataSet dataSet = new DataSet();
    string xmlPath = "Full_Path_To_XML_File";
    
    // Read the data from the XML file into the dataset object.
    dataSet.ReadXml(xmlPath);
    
    // Declare a variable for the group name typed in the text box.
    string groupName = textBox1.Text;
    
    /* Iterate through each row in the data set object,  assign their values to variables
    for each parameter, and add each user to the specified group.*/
    foreach (DataRow row in dataSet.Tables[0].Rows)
    {
        string userName = row["name"].ToString();
        string userLoginName = row["loginname"].ToString();
        string userEmail = row["email"].ToString();
        string userNotes = row["notes"].ToString();
    
        userGroup.AddUserToGroup(groupName, userName, userLoginName, userEmail, userNotes);
    }
    
  • On the Debug menu, click Start to test the form. Click the button to add the user to the specified group and to display the group members.