In-Process API GroupUserMembers.Create GET Works, POST Fails

I'm putting together a Plugin handle some data and then add a user to a group programmatically. I'm using a Widget to call the method on a page. When I send the data via querystring everything works fine. When I send it via POST everything works except for the call to Telligent.Evolution.Extensibility.Api.Version1.PublicApi.GroupUserMembers.Create(). Both methods pass in identical data to the Create method, but only when using GET does it actually add the user to the group. Using POST it returns a null GroupUser.

Is this expected behavior? Should you not be able to post to a Page and use InProcess calls? If that's the case, what is a better way to go about doing this?

  • In reply to Luke D:

     public class JoinGroupAction
        {
            /// <summary>
            /// Adds a user as a member to the specified group.
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="groupId"></param>
            public void Add(int userId, int groupId)
            {
                var user = Evolution.Users.GetUser(userId);
                if (user == null)
                {
                    return;
                }
    
                try
                {
                    SetContextUser();
                    PublicApi.GroupUserMembers.Create(groupId, userId, new GroupUserMembersCreateOptions() {GroupMembershipType = "Member" });
                }
                catch (Exception)
                {
                    //eat exception 
                }
                finally
                {
                    ReverseContextUser(user);
                }
            }
    
            /// <summary>
            /// Set the current user to the community service account.
            /// </summary>
            private void SetContextUser()
            {
                Telligent.Evolution.Components.User serviceAccount = Telligent.Evolution.Users.GetUser(Telligent.Evolution.Users.ServiceUserName);
                CSContext.Current.User = serviceAccount;
            }
    
            /// <summary>
            /// Reverse the current context user back to the current contextual user.
            /// </summary>
            /// <param name="user"></param>
            private void ReverseContextUser(Telligent.Evolution.Components.User user)
            {
                CSContext.Current.User = user;
            }
    
        }
  • In reply to Chad L:

    On 7.1, you can replicate the RunAs method with the something like the following (note, I typed this straight into the forurm - there may be some typos etc.): 

    \\ DO NOT USE THE FOLLOWING CODE ON VERSIONS 7.6 OR HIGHER 
    \\ INSTEAD USE PublicApi.Users.RunAs(...) WHICH PROVIDES THE SAME FUNCTIONALITY THROUGH A SUPPORTED API
    public static void RunAs(string newUsername, Action work) { User originalUser = CSContext.Current.User try { var user = Users.Get(newUsername); CSContext.Current.User = newUser; work(); } finally { CSContext.Current.User = originalUser; } }

    Whilst this method is very similar to Luke's suggestion, and is no better from using recommended & supported APIs, it does make it easier for you to get onto the supported path once you upgrade to 7.6 or higher.

    You could even add this as an extension method for PublicApi.Users then all you need to do after upgrading to 7.6 is remove the extension class and recompile :)

  • In reply to Alex Crome:

    To do this supported in 7.1 you can use the REST Api and use impersonation, that is supported and upgrade safe.  You can choose to change it later if you like but will not be required.

    I still highly discourage using CSContext in any capacity, especially for the active user.

Related