Thursday, May 5, 2016

Add / remove users to Access team dynamically

Sometime we need to add users into access teams when record is created or based on some business logic.

From MS CRM 2013 Microsoft introduced access teams.

For information about access team
 Use access teams and owner teams to collaborate and share information

You can enable access team for entity and create access team templates.

Here is more information on how to create access team template and add to form.
Create Access Teams

when you created access team template you can add it to form and add users manually to give access to record, but some time we need to it automatically.

To do automatically, you can use  AddUserToRecordTeamRequest messge.

AddUserToRecordTeamRequest addReq = new AddUserToRecordTeamRequest()
       {
         Record = Record Entity Reference,
         SystemUserId = UserId,
         TeamTemplateId = TemplateId

       };

service.Execute(addReq);

and use Execute to pass this request.

If want to do add user into multiple records access Team.

public void AddUserToAccessTeam(IOrganizationService service, List<EntityReference> lstentref, string templateName, Guid loggedInUser, Guid? TemplateId)
        {
         using (XrmServiceContext Xrmcontext = new XrmServiceContext(service))                                  {
                Guid teamTemplate = new Guid();

                if (TemplateId == null || TemplateId.Value == Guid.Empty)
                    /*Get Access Team Template from Template Name*/
                    teamTemplate = Xrmcontext.TeamTemplateSet.Where(ttm => ttm.TeamTemplateName == templateName).Select(ttm => ttm.TeamTemplateId.Value).FirstOrDefault();
                else
                    teamTemplate = TemplateId.Value;

                int count = 0;
                int TotalRecord = lstentref.Count();
                int recordRemaining = TotalRecord;

                /*Initialize multiple request*/
                ExecuteMultipleRequest requestWithResults = new ExecuteMultipleRequest()
                {
                    Settings = new ExecuteMultipleSettings()
                    {
                        ContinueOnError = true,
                        ReturnResponses = true
                    },
                    Requests = new OrganizationRequestCollection()
                };

                foreach (EntityReference entRef in lstentref)
                {
                    recordRemaining--;
                    /*Add user to Record Team request*/
                    AddUserToRecordTeamRequest addReq = new AddUserToRecordTeamRequest()
                    {
                        Record = entRef,
                        SystemUserId = loggedInUser,
                        TeamTemplateId = teamTemplate
                    };

                    requestWithResults.Requests.Add(addReq);
                    count++;
                    if (count == 999 || recordRemaining == 0)
                    {
                        ExecuteMultipleResponse responseWithResults = (ExecuteMultipleResponse)Xrmcontext.Execute(requestWithResults);
                        requestWithResults.Requests.Clear();
                        count = 1;
                    }
                }
            }
        }


To remove user from access team you can use RemoveUserFromRecordTeamRequest messge.

RemoveUserFromRecordTeamRequest addReq = new RemoveUserFromRecordTeamRequest()
    {
      Record = Record Entity Reference,,
      SystemUserId = User Id,
      TeamTemplateId = Access Team Template Id

    };

Use similar method as AddUserToAccessTeam, if you want to remove user from multiple records.




Thursday, April 7, 2016

Insert Image in knowledge base article

Insert Image in knowledge base article

1.       Create one Image (PNG/JPG/GIF) type web resource into CRM system.
2.       Upload image you want to show in article into Web resource.
3.       Save your image web resource.
4.       Once you save your web source, CRM shows direct URL for that web resource.

5.       Open your article where you want to insert image, and also open image resource in another browser window using web resource URL.
6.       Now simply drag image to Article.

7.       Save and publish article
8.       Now article will show image.



Tuesday, May 19, 2015

Hide / Show Export to Excel button for specific Entity / Specific View

In MS CRM, Export to excel feature is enabled or disabled from security role.
You can have this feature for all applicable entities or none of the entity.
But sometime we have requirement to hide export to excel from particular entity, or show only for particular entity.
With Security role this requirement is not possible, but will little JavaScript code and ribbon customization we can achieve this.

Case 1: Hide Export to Excel button from contact Home grid.

1.      Create one custom solution
2.      Add Application Ribbons – from client Extensions





3.      Create one JavaScript web resource in this solution and add following code

function ShowHideExport2Excel(SelectedEntityTypeName) {
    if (SelectedEntityTypeName == 'contact')
        return false// hide button
    else
        return true//show button
}

4.       Now Open this custom solution into Ribbon workbench
5.      In the Entities list you will see ApplicationRibbon
6.      Select Export to Excel button from Home, right click and Customize command



7.      In Commands, you will see Mscrm.ExportToExcel
8.      Now add custom Enable Rule to this command. Keep existing enable rules.
9.      When adding enable rule, add Custom JavaScript rule.
Default: True
FunctionName: ShowHideExport2Excel
invertResult: False
Library: your JavaScript library
Parameters :  [Crm Parameter] = SelectedEntityTypeName

10.  Add Crm Parameter to Custom JavaScript rule, value of this parameter must be SelectedEntityTypeName

11.   Save and publish your customization.
12.  Now Export to Excel button is hidden for Contact home grid.

Export To Excel button is not showing now.



 Export To Excel button is showing now.



Case 2: Show Export to Excel button only for contact Home grid.

In this case you just need to change JavaScript code little bit as

function ShowHideExport2Excel(SelectedEntityTypeName) {
    if (SelectedEntityTypeName == 'contact')
        return true// show button
    else
        return false//hide button
}


Case 3: Show Export to excel button for particular entity and particular view only

[To do this need to use Unsupported JavaScript code]
To do this, we need to add one more Crm parameter to enable rule, and select value to this parameter is SelectedControl



And modify JavaScript function as

For MS CRM 2013 :

function ShowHideExport2Excel(SelectedEntityTypeName, selectedCtrl) {
    if (SelectedEntityTypeName == 'contact') {
        var view = selectedCtrl.get_$1X_3();
        var viewName = view.selectedViewName;
        if (viewName == 'My Active Contacts') //Change "My Active Contacts" with Your view name to show Export To Excel button
            return true; // show button
        else
            return false; // hide button
    }
    else
        return true;
       
}

For MS CRM 2015:

function ShowHideExport2Excel(SelectedEntityTypeName, selectedCtrl) {
    if (SelectedEntityTypeName == 'contact') {        
        var viewName = selectedCtrl.get_viewTitle();
        if (viewName == 'My Active Contacts'//Change "My Active Contacts" with Your view name to show Export To Excel button
            return true// show button
        else
            return false// hide button
    }
    else
        return true;
       
}