1

The Subsite has unique permissions. It does not inherit from parent. The Code just fall over on production server.

    /// <summary>
    /// Method to add user to subsite
    /// </summary>
    /// <param name="userID"  userID="domain\username"></param>
    protected void AddUserToSubsite(string userID)
    {
        try
        {
            SPUserToken sysToken = SPContext.Current.Site.SystemAccount.UserToken;
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite("http://servername/sites/rootSiteCollection/SubSite", sysToken))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPGroup subSiteGroup = web.Groups.GetByName(DropDownListGroups.SelectedItem.Text.ToString());
                        web.AllowUnsafeUpdates = true;
                        using (SPSite rootSite = new SPSite("http://servername", sysToken))                  // Make sure user exists for root site..
                        {
                            using (SPWeb rootWeb = rootSite.OpenWeb())
                            {
                                SPUser addUser = rootWeb.EnsureUser(userID);                                //Validate the user with Root Site Collection before adding to subsite .

                                subSiteGroup.AddUser(addUser);
                            }
                        }
                        subSiteGroup.Update();
                        web.AllowUnsafeUpdates = false;
                    }
                }
            });
        }

        catch (SPException ex) { throw ex; }
    }
  • Can you post exact exception stack trace or message here? – Kalpesh Vaghela Jul 27 '15 at 4:04
  • UserAgent not available, file operations may not be optimized. at Microsoft.SharePoint.SPFileStreamManager.CreateCobaltStreamContainer(SPFileStreamStore spfs, ILockBytes ilb, Boolean copyOnFirstWrite, Boolean disposeIlb) – mahesh pullareddy Jul 27 '15 at 4:11
  • SPRequest.UpdateMembers: UserPrincipalName=, AppPrincipalName= ,bstrUrl=servername ,dwObjectType=0 ,bstrObjId= ,lGroupID=-1 ,lGroupOwnerId=-1 ,bRemoveFromCurrentScopeOnly=False ,bSendEmail=True d6fd1d9d-2aa8-d04c-4847-4c8eedcff008 – mahesh pullareddy Jul 27 '15 at 4:17
  • Does current user have permission to perform group operation to site ?because ultimately you are running code using Currentl User only even if you have used RunWithElevatedPrivileges because you are passing current user's token while opening site – Kalpesh Vaghela Jul 27 '15 at 4:21
  • Site is opened with System.Account. (Current.Site.SystemAccount.UserToken;) its falling off while addUser – mahesh pullareddy Jul 27 '15 at 4:26
1
 /// <summary>
/// Method to add user to subsite
/// </summary>
/// <param name="userID"  userID="domain\username"></param>
protected void AddUserToSubsite(string userID)
{
    try
    {
        SPUserToken sysToken = SPContext.Current.Site.SystemAccount.UserToken;
        SPSecurity.RunWithElevatedPrivileges(delegate
        {
            using (SPSite site = new SPSite("http://servername/sites/rootSiteCollection/SubSite"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPGroup subSiteGroup = web.SiteGroups[DropDownListGroups.SelectedItem.Text.ToString()];
                    web.AllowUnsafeUpdates = true;                        
                    SPUser addUser = web.EnsureUser(userID);                                
                    subSiteGroup.AddUser(addUser);                                                                       
                    web.AllowUnsafeUpdates = false;
                }
            }
        });
    }
    catch (SPException ex) { throw ex; }
}
  1. There is no need of passing user token if you want to perform above action using Elevation
  2. There is no need of opening web again as RootWeb.EnsureUser and SubSite.EnsureUser will be same always as UserInformationList is on RootSite. EnsureUser method just add user's entry in "UserInformationList" if not available.
  3. There is no need of updating SharePoint group as you are adding users on that group

Hope above explanation helps to you.

  • Yep thanks, I modified the code. its now working.. – mahesh pullareddy Jul 27 '15 at 5:27
  • If above codes works for you then you can mark it as answer if you want so that other user can trust on it. – Kalpesh Vaghela Jul 27 '15 at 5:28
  • will post the correct code.. – mahesh pullareddy Jul 27 '15 at 5:31
0
         /// Method to update users for a selected group and subsite.
    /// </summary>
    /// <param name="userID"></param>
    /// <param name="updatetype"></param>
 private void UpdateUserInSharePointGroup(string userID, string updatetype)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        { using (SPSite site = new SPSite(DropDownListProjects.SelectedValue.ToString()))
            { using (SPWeb web = site.OpenWeb())
                { web.AllowUnsafeUpdates = true;
                  SPGroup Group = web.Groups[DropDownListGroups.SelectedItem.Text];
                  SPUser user = web.EnsureUser(userID);
                    if (user != null)
                    { switch (updatetype.ToLower())
                        {   case "add":
                                Group.AddUser(user);
                                break;
                            case "remove":
                                Group.RemoveUser(user);
                                Group.Update();
                                break;
                        }
                    }
                    web.AllowUnsafeUpdates = true;
                }
            }
        });
    }
0
/// <summary>
        /// Method to update users for a selected group for a given subsite (.
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="updatetype"></param>
        private void UpdateUserInSharePointGroup(string userID, string updatetype)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite("subsiteUrl")
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;
                        SPGroup Group = web.Groups[DropDownListGroups.SelectedItem.Text];
                        SPUser user = web.EnsureUser(userID);

                        if (user != null)
                        {
                            switch (updatetype.ToLower())
                            {
                                case "add":
                                    Group.AddUser(user);
                                    break;
                                case "remove":
                                    Group.RemoveUser(user);
                                    Group.Update();
                                    break;
                            }
                        }
                        web.AllowUnsafeUpdates = true;
                    }
                }
            });
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.