Export (0) Print
Expand All

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)

[RemoteAttribute]
public User AddUser(
	User user
)

Parameters

user
Type: Microsoft.SharePoint.Client.User

A user to add.

Return Value

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

ExceptionCondition
[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.

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);
        }
    }
}



Show:
© 2014 Microsoft