Freigeben über


Gewusst wie: Hinzufügen von Benutzern zu einer Gruppe

Letzte Änderung: Mittwoch, 7. Juli 2010

Gilt für: SharePoint Foundation 2010

Um einer Gruppe in Microsoft SharePoint Foundation mehrere Benutzer hinzuzufügen, verwenden Sie die AddUserToGroup-Methode, wenn Sie neue oder vorhandene einzelne Benutzer hinzufügen, oder die AddUserCollectionToGroup-Methode, wenn Sie eine Auflistung von vorhandenen Benutzern hinzufügen.

Verfahren

Erstellen Sie zuerst eine Windows Forms-Anwendung in Microsoft Visual Studio. Weitere Informationen zum Festlegen eines Webverweises auf einen SharePoint Foundation-Webdienst finden Sie unter Richtlinien für Webdienste.

Hinzufügen von mehreren Benutzern aus einer Website oder Unterwebsite

Verwenden Sie die GetUserCollectionFromSite-Methode oder die GetUserCollectionFromWeb-Methode, um alle Benutzer aus einer Website zurückzugeben. Durchlaufen Sie dann die Auflistung der Benutzer, und fügen Sie mit der AddUserCollectionToGroup-Methode jeden Benutzer einer angegebenen Gruppe hinzu.

So fügen Sie einer Gruppe Benutzer aus einer Website oder Unterwebsite hinzu

  1. Nachdem Sie ein Windows Forms-Anwendungsprojekt erstellt und einen Webverweis hinzugefügt haben, öffnen Sie in der Entwurfsansicht Form1, öffnen Sie die Toolbox, und ziehen Sie dann ein Button-Steuerelement auf das Formular.

  2. Doppelklicken Sie auf die Schaltfläche, um den Code-Editor zu öffnen, und fügen Sie dem Button1_Click-Ereignishandler die folgenden Codezeilen hinzu.

    'Declare and initialize a variable for the UserGroup Web service.
    Dim userGroup As New Web_Reference.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.
    Dim usersSite As System.Xml.XmlNode = userGroup.GetUserCollectionFromWeb()
    
    'Declare another XmlNode object and initialize it with the first 
    'child node returned from the previous method.
    Dim userNode As System.Xml.XmlNode = usersSite.FirstChild
    
    'Pass the first child node and its contents as the XMLNode object 
    'parameter for the method.
    userGroup.AddUserCollectionToGroup("Group_Name", userNode)
    
    /*Declare and initialize a variable for the UserGroup Web service.*/
    Web_Reference.UserGroup userGroup = new Web_Reference.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 previous 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
       ("Group_Name",userNode);
    
  3. Klicken Sie im Menü Debuggen auf Debuggen starten, um das Formular zu testen. Klicken Sie auf die Schaltfläche im Formular, um der angegebenen Gruppe die Benutzer hinzuzufügen.

Hinzufügen eines einzelnen Benutzers

Verwenden Sie die AddUserToGroup-Methode, um einer Gruppe einen neuen oder vorhandenen Benutzer hinzuzufügen, und verwenden Sie dieGetUserCollectionFromGroup-Methode, um Informationen über alle Benutzer in einer Gruppe zurückzugeben.

So fügen Sie einer Gruppe einen einzelnen Benutzer hinzu und zeigen die Gruppenmitglieder an

  1. Erstellen Sie eine Windows Forms-Anwendung, fügen Sie einen Webverweis hinzu, und fügen Sie dem Formular fünf TextBox-Steuerelemente, ein Label-Steuerelement und ein Button-Steuerelement hinzu.

  2. Doppelklicken Sie auf die Schaltfläche, um den Code-Editor zu öffnen, und fügen Sie dem Button1_Click-Ereignishandler die folgenden Codezeilen hinzu.

    '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.
    Dim userGroup As New Web_Reference.UserGroup()
    userGroup.Credentials = System.Net.CredentialCache.DefaultCredentials
    
    'Gather data from the text boxes.
    Dim groupName As String = textBox1.Text
    Dim userName As String = textBox2.Text
    Dim userLoginName As String = textBox3.Text
    Dim userEmail As String = textBox4.Text
    Dim userNotes As String = 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.
    Dim usersNode1 As System.Xml.XmlNode = userGroup.GetUserCollectionFromGroup(groupName)
    Dim usersNode2 As System.Xml.XmlNode = usersNode1.FirstChild
    Dim userNodes As System.Xml.XmlNodeList = 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.
    Dim user As System.Xml.XmlNode
    For Each user In  userNodes
       Dim name As String = user.Attributes("Name").Value
       Dim loginName As String = user.Attributes("LoginName").Value
       Dim email As String = user.Attributes("Email").Value
       Dim notes As String = user.Attributes("Notes").Value
    
       label1.Text += ControlChars.Lf + ControlChars.Lf + name + " : " 
          + loginName + " : " + email + " : " + notes
    Next user
    
    /*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*/
    Web_Reference.UserGroup userGroup = new Web_Reference.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;
    }
    
  3. Klicken Sie im Menü Debuggen auf Debuggen starten, um das Formular zu testen. Klicken Sie auf die Schaltfläche im Formular, um der angegebenen Gruppe die Benutzer hinzuzufügen und die Gruppenmitglieder anzuzeigen.

Hinzufügen von Benutzern aus Informationen in einer XML-Datei

Laden Sie die Auflistung von Benutzerdaten in ein DataSet-Objekt, und verwenden Sie dann die AddUserToGroup-Methode, um die Auflistung zu durchlaufen und die einzelnen Benutzer zur Gruppe hinzuzufügen.

So fügen Sie einer Gruppe Benutzer aus Informationen in einer XML-Datei hinzu

  1. Erstellen Sie eine Windows Forms-Anwendung, fügen Sie einen Webverweis hinzu, und fügen Sie dem Formular ein TextBox-Steuerelemente und ein Button-Steuerelement hinzu.

  2. Doppelklicken Sie auf die Schaltfläche, um den Code-Editor zu öffnen, und fügen Sie dem Button1_Click-Ereignishandler die folgenden Codezeilen hinzu.

    '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.
    Dim userGroup As New Web_Reference.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.
    Dim dataSet As New DataSet()
    Dim xmlPath As String = "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.
    Dim groupName As String = textBox1.Text
    
    'Iterate through each row in the DataSet object, assign their 
    'values to variables for each parameter, and add each user to the 
    'specified group.
    Dim row As DataRow
    For Each row In  dataSet.Tables(0).Rows
       Dim userName As String = row("name").ToString()
       Dim userLoginName As String = row("loginname").ToString()
       Dim userEmail As String = row("email").ToString()
       Dim userNotes As String = row("notes").ToString()
    
       userGroup.AddUserToGroup(groupName, userName, userLoginName, userEmail, userNotes)
    Next row
    
    /*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.*/
    Web_Reference.UserGroup userGroup = new Web_Reference.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 DataSet 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);
    }
    
  3. Klicken Sie im Menü Debuggen auf Debuggen starten, um das Formular zu testen. Klicken Sie auf die Schaltfläche im Formular, um der angegebenen Gruppe die Benutzer hinzuzufügen und die Gruppenmitglieder anzuzeigen.