marius_

All groups from ConfigDataStore

Recommended Posts

Hello,

 

at first I want to get all groups that are exist. With powershell moduel the command is: Get-IpPbxGroup | Sort-Object Name | Where-Object { ($_.IsEveryoneGroup -eq $false) -and ($_.Name -ne "Konfig-Gruppe")}.

How can i get the groups in c# with the CDS SDK?

 

After that I want to query the group of each user. I am already getting all users.

 

Can anybody help me?

Edited by marius_

Share this post


Link to post
Share on other sites

Give me an idea of what result you're trying to get. Maybe I can help you with an even more complete set of Code.

Share this post


Link to post
Share on other sites

Hello Marius,

 

 I quickly create a example class that should fit your needs:

using SWConfigDataClientLib;
using SWConfigDataClientLib.Data;
using SWConfigDataClientLib.Proxies.Admin;
using SWConfigDataClientLib.Proxies.Users;
using System;

namespace IpPbx.Samples.Shared
{
    public class CDSLib
    {
        /// <summary>
        /// The CDS Client LibManager is the central class of the CDS Client API.
        /// It contains methods to address the server and to authenticate users.
        /// You use LibManager to create instances of classes for retrieval and
        /// manipulation of IpPbx configuration items. The LibManager should be the
        /// first class to instantiate. It checks the configuration and initiates
        /// the tracing of the client application. 
        /// </summary>
        private LibManager libManager;

        /// <summary>
        /// Initializes a new instance of the <see cref="CDSLib" /> class.
        /// </summary>
        /// <param name="userName">Username (SwyxIt Login)</param>
        /// <param name="password">Password (SwyxIt Login)</param>
        /// <param name="serverName">SwyxServer Address (FQDN or IP)</param>
        public CDSLib(string userName = "", string password = "", string serverName = "")
        {
            Init(userName, password, serverName);
        }

        /// <summary>
        /// Initialisation
        /// </summary>
        /// <param name="userName">Username (SwyxIt Login)</param>
        /// <param name="password">Password (SwyxIt Login)</param>
        /// <param name="serverAddress">SwyxServer Address (FQDN or IP)</param>
        private void Init(string userName, string password, string serverAddress)
        {
            // Check if server address is empty
            if (string.IsNullOrWhiteSpace(serverAddress) ||
                serverAddress.Equals("localhost", StringComparison.InvariantCultureIgnoreCase))
            {
                serverAddress = "127.0.0.1";
            }

            // Set default authentication mode to trusted
            SProxyObject.AuthenticationMode authenticationMode = SProxyObject.AuthenticationMode.Trusted;
            if (serverAddress == "127.0.0.1")
            {
                authenticationMode = SProxyObject.AuthenticationMode.TrustedLocal;
            }

            // If username and password are available the authentication
            // mode must be changed to UsernamePassword
            if (!string.IsNullOrWhiteSpace(userName) &&
                !string.IsNullOrWhiteSpace(password))
            {
                authenticationMode = SProxyObject.AuthenticationMode.UsernamePassword;
            }

            // Create and configure new LibManager object
            libManager = new LibManager()
            {
                BaseProtocol = SProxyObject.BaseProtocol.TCP,
                Username = userName,
                Password = password,
                WSBaseUrl = serverAddress,
                AuthenticationMode = authenticationMode
            };
        }

        /// <summary>
        /// Returns a collection of ippbx users filtered by name
        /// </summary>
        /// <param name="nameFilter">Name of the IpPbx user your are
        /// searching for (SQL Wildcards e.g. "%" can be used here)</param>
        public UserPrimaryCollection GetUsers(string nameFilter)
        {
            // Get UserEnum object from LibManager
            UserEnum userEnum = libManager.GetUserEnum();

            try
            {
                userEnum.ExecuteNameFilter(nameFilter, new OrderByList());
                return userEnum.PrimaryCollection;
            }
            catch (Exception)
            {
                // Add your tracing here
                throw;
            }
            finally
            {
                // IMPORTANT
                // If you free resources here you will NOT be able
                // to save changes anymore. Every UserEntry has a
                // reference to his parent UserEnum to save changes.
                // If the Enum is released the reference will be broken.

                // PERFORMANCE NOTE
                // For "read-only" purposes the AdminFacade should be
                // used because it is MUCH faster and lightweight.

                /* Free resources
                if (userEnum != null)
                {
                    userEnum.Clear();
                    userEnum.FreeForReuse();
                }
                */
            }
        }

        /// <summary>
        /// Returns all users in one collection (READ ONLY -> BETTER PERFORMANCE)
        /// </summary>
        /// <param name="includeUserFileSize">If true the size of the user files in the database is calculated and added</param>
        public UserAdminView1EntrySortableCollection GetUserView(bool includeUserFileSize)
        {
            var adminFacade = libManager.GetAdminFacade();

            try
            {
                return adminFacade.GetUserAdminView1(includeUserFileSize, out UserDeviceSmallMap userDeviceSmallMap).CreateSortableFilterCollection();
            }
            catch (Exception)
            {
                // Add your tracing here
                throw;
            }
        }

        /// <summary>
        /// Returns a collection of IpPbx groups filtered by name
        /// </summary>
        /// <param name="nameFilter">Name of the IpPbx group your are
        /// searching for (SQL Wildcards e.g. "%" can be used here)</param>
        public SWConfigDataClientLib.Proxies.Groups.GroupPrimaryCollection GetGroups(string nameFilter)
        {
            // Get enum object from LibManager
            var groupEnum = libManager.GetGroupEnum();

            try
            {
                groupEnum.ExecuteNameFilter(nameFilter, new OrderByList());
                return groupEnum.PrimaryCollection;
            }
            catch (Exception)
            {
                // Add your tracing here
                throw;
            }
            finally
            {
                // IMPORTANT
                // If you free resources here you will NOT be able
                // to save changes anymore. Every entry has a
                // reference to his parent UserEnum to save changes.
                // If the Enum is released the reference will be broken.

                /* Free resources
                if (groupEnum != null)
                {
                    groupEnum.Clear();
                    groupEnum.FreeForReuse();
                }
                */
            }
        }

        /// <summary>
        /// Returns all users in one collection (READ ONLY -> BETTER PERFORMANCE)
        /// </summary>
        public GroupAdminView1EntrySortableCollection GetGroupView()
        {
            var adminFacade = libManager.GetAdminFacade();

            try
            {
                return adminFacade.GetGroupAdminView1().CreateSortableFilterCollection();
            }
            catch (Exception)
            {
                // Add your tracing here
                throw;
            }
        }
    }
}

 

Kind regards

 

Sebastian Dreier

 

Share this post


Link to post
Share on other sites

Hello Marius,

 

the group member information is available as property on every UserEntry:

var cdsLib = new CDSLib();
var userList = cdsLib.GetUsers("%");
for (var userEntry in userList)
{
    // userEntry.MembershipEntryCollection
}

Important performance note
Do not call the "GetUsers" method too often in a row. It is far better to get all the users in one collection and to work on that.

 

Kind regards

 

Sebastian Dreier
 

 

Share this post


Link to post
Share on other sites

Hello,

I use your method but it is failing here:

On 12/6/2018 at 2:54 AM, Sebastian Dreier said:

userEnum.ExecuteNameFilter(nameFilter, new OrderByList());

It throws an "System.ArgumentNullException" by using the nameFilter "%". If I use other nameFilter like a name, I got no exception but also it returne no entry.

I have tried to use the method GetUserView but the returned "UserAdminView1Entry" has not attribut MembershipEntryCollection to get usersGroup.

I think there's something wrong with the  UserEnum.

 

Error message: Value cannot be null.Parameter name: key

Any ideas??

 

Best regards,

Marius

 


 

 
Edited by marius_

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now