Compartilhar via


UserCollection.AddUser Method

Adds the user specified by the user parameter to the collection.

Namespace:  Microsoft.SharePoint.Client
Assemblies:   Microsoft.SharePoint.Client.Silverlight (in Microsoft.SharePoint.Client.Silverlight.dll);  Microsoft.SharePoint.Client (in Microsoft.SharePoint.Client.dll)

Syntax

'Declaration
<RemoteAttribute> _
Public Function AddUser ( _
    user As User _
) As User
'Usage
Dim instance As UserCollection
Dim user As User
Dim returnValue As User

returnValue = instance.AddUser(user)
[RemoteAttribute]
public User AddUser(
    User user
)

Parameters

Return Value

Type: Microsoft.SharePoint.Client.User
Returns a User instance representing the user to add.

Exceptions

Exception Condition
[Microsoft.SharePoint.SPException]

The user does not exist or is not unique. Error code: -2130575276. The collection is read-only. Error code:-1.

[System.Security.SecurityException]

The collection is the farm administrators group and the specified user is not a computer administrator. Error code: -1.

[System.UnauthorizedAccessException]

The current user has insufficient permissions. Error code: -2147024891.

Examples

This code example adds the current user to the visitors group on the current site.

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointFoundation.Samples
{
    class UserCollection_AddUserExample
    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            Web site = clientContext.Web;
            GroupCollection collGroup = site.SiteGroups;

            // Get the visitors group, assuming its ID is 4.
            Group visitorsGroup = collGroup.GetById(4);

            User currentUser = site.CurrentUser;
            UserCollection collUser = visitorsGroup.Users;
            collUser.AddUser(currentUser);

            clientContext.Load(currentUser);
            clientContext.Load(visitorsGroup);
            clientContext.ExecuteQuery();

            Console.WriteLine(currentUser.Title + " added to group " + visitorsGroup.Title);
        }
    }
}

See Also

Reference

UserCollection Class

UserCollection Members

Microsoft.SharePoint.Client Namespace