UserCollection.AddUser method
Adds the user specified by the user parameter to the collection.
Namespace: Microsoft.SharePoint.Client
Assembly: Microsoft.SharePoint.Client (in Microsoft.SharePoint.Client.dll)
Syntax
'Declaration
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)
public User AddUser(
User user
)
Parameters
user
Type: Microsoft.SharePoint.Client.UserA user to add.
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);
}
}
}