Sunday, November 6, 2011

Add users to SP Group Programatically for a Claims Based Web Application

Here's the code to Add users to SharePoint Group Programatically for a Claims Based Web Application

Note that we need to add claims token to the user name before adding them to the group since its a claims based web application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AddUsers.UserGroup;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration.Claims;

namespace AddUsers
    class Program
        static void Main(string[] args)
            using (UserGroup.UserGroup userService = new UserGroup.UserGroup())
                String uriVal = "http://site/sites/testsite/_vti_bin/UserGroup.asmx";
                String userName = String.Empty;
                userService.Url = uriVal;
                userService.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
                SPClaimProviderManager ClaimManager = SPClaimProviderManager.Local;
                if (ClaimManager != null)
                        SPClaim claim = new SPClaim(SPClaimTypes.UserLogonName, "username", "", SPOriginalIssuers.Format(SPOriginalIssuerType.Windows));
                        userName = ClaimManager.EncodeClaim(claim);
                    catch (Exception ex)
                userService.AddUserToGroup("Approvers", userName, userName, "useremail", "");



  1. Hi,

    Where is AddedUsers.UserGroup namespace?

    1. Its right below the namespaces declaration. It's nothing but the out of the box UserGroup.asmx service.