locked
MS CRM 2011 ONLINE - Automate team creating RRS feed

  • Question

  • How to automate creating teams? When a user creates a new team, user need to set administrator and security roles.

    Is there any possibility to create a team somehow that administrator is set automatically and also specified security role is set?

    Monday, August 19, 2013 6:16 AM

All replies

  • If you want to just simplify the team form, you can autopopulate the 'Administrator' to the user creating the team. I've done an example below which makes the admin field not required, and sets it to the current user if it is not specified on save:

    //run on load
    function setAdminNotRequired() {
        if (Xrm.Page.ui.getFormType() == 1) { //create
            Xrm.Page.getAttribute("administratorid").setRequiredLevel("none");
        }
    }
    
    //run on save
    function setAdminIfNull() {
        if (Xrm.Page.ui.getFormType() == 1) { //create
            var admin = Xrm.Page.getAttribute("administratorid");
            if (admin.getValue() == null) { //if null on save set to current user
                admin.setValue([{ "entityType": "systemuser", "name": "", "id": Xrm.Page.context.getUserId() }]);
            }
        }
    }

    If you want to automatically add a role to the team, you can write a plugin on create of team which assigns a default role; or better yet, you could add a lookup to the team form called 'Default Security Role' which is a lookup to the security role entity. This can then be filtered by the Business Unit lookup to only allow results from the teams Business Unit. In your plugin you can then assign the team that role by default if it has been specified. You could also autopopulate this lookup using JavaScript if you wanted to streamline it even more.

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Monday, August 19, 2013 7:29 AM
  • If you want to just simplify the team form, you can autopopulate the 'Administrator' to the user creating the team. I've done an example below which makes the admin field not required, and sets it to the current user if it is not specified on save:

    //run on load
    function setAdminNotRequired() {
        if (Xrm.Page.ui.getFormType() == 1) { //create
            Xrm.Page.getAttribute("administratorid").setRequiredLevel("none");
        }
    }
    
    //run on save
    function setAdminIfNull() {
        if (Xrm.Page.ui.getFormType() == 1) { //create
            var admin = Xrm.Page.getAttribute("administratorid");
            if (admin.getValue() == null) { //if null on save set to current user
                admin.setValue([{ "entityType": "systemuser", "name": "", "id": Xrm.Page.context.getUserId() }]);
            }
        }
    }

    If you want to automatically add a role to the team, you can write a plugin on create of team which assigns a default role; or better yet, you could add a lookup to the team form called 'Default Security Role' which is a lookup to the security role entity. This can then be filtered by the Business Unit lookup to only allow results from the teams Business Unit. In your plugin you can then assign the team that role by default if it has been specified. You could also autopopulate this lookup using JavaScript if you wanted to streamline it even more.

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Thanks, I have to test your javascript code.

    What comes to your suggestion to use a plugin, can you provide more info about that? I am just learning plugins and just  got the basic autonumber plugin to work.

    Monday, August 19, 2013 8:05 AM
  • For the plugin I was suggesting you add a lookup to security role like this:

    The lookup can be filtered by Business Unit to ensure security roles from other business units are not selected.

    And then have a plugin running on Create of a Team (from my testing it needs to run asynchronously).

    The plugin will check if there is a 'Default Security Role' defined, and if so associate the role with the team.

            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
    
                Entity target = context.InputParameters["Target"] as Entity;
                if (target != null && target.Contains("new_defaultsecurityroleid"))
                {
                    //get the default role from target and make sure it has been set
                    EntityReference defaultRole = target.GetAttributeValue<EntityReference>("new_defaultsecurityroleid");
                    if (defaultRole != null)
                    {
                        //create the collection of roles with our default role
                        EntityReferenceCollection roles = new EntityReferenceCollection();
                        roles.Add(defaultRole);
    
                        //associate the default role with the team
                        service.Associate("team", target.Id, new Relationship("teamroles_association"), roles);
                    }
                }
            }

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Monday, August 19, 2013 10:20 AM