Thursday, April 28, 2011

SSRS Reporting

I recently was asked to figure out to use a more complex security model for reporting in CRM 2011.  The issue is that CRM doesn't have a way of hiding reports from users.  The only choices you have is the security roles that are implemented by the filtered views and by removing the ability for the user to see reports from their roles.  This functionality was not granular enough to meet the security requirement.  To solve the problem I suggested adding an entry to the sitemap that would allow the user access to SSRS reports from inside the CRM application.  We would then use the SSRS security model to get the desired level of granularity.  Here's how I did it:

Edit the site map:

<Area Id="SSRSReports" ResourceId="Area_SSRSReports" Icon="/_imgs/bar_bottom_ico_reports.gif" DescriptionResourceId="SSRSReports_Area_Description" Title ="SSRS Reports">
    <Group Id="Reports" ResourceId="Homepage_SSRSReports" DescriptionResourceId="Homepage_SSRSReports" >
        <Titles>
            <Title Title="Reports" LCID="1033" />
        </Titles>
        <SubArea Id="report_link" ResourceId="report_link_resource" DescriptionResourceId="Reports_SubArea_Description" Icon="/_imgs/bar_bottom_ico_reports.gif" Url="http://crm2011/reports" AvailableOffline="false">
            <Titles>
                <Title Title="SSRS Reports: Accounts" LCID="1033" />
            </Titles>
          </SubArea>
          <SubArea Id="report_link2" ResourceId="report_link_resource2" DescriptionResourceId="Reports_SubArea_Description" Icon="/_imgs/bar_bottom_ico_reports.gif" Url="http://crm2011/reports" AvailableOffline="false">
            <Titles>
                <Title Title="SSRS Reports: Contacts" LCID="1033" />
            </Titles>
        </SubArea>
    </Group>
</Area>

The above xml will add a new entity to the lower left hand corner of the menu.  It will allow users to browse to the SSRS reports.  To edit the sitemap you need to add it to a solution, export the solution, open the .zip, remove the customizations.xml file and make the appropriate edits.  To import the changes put the customizations.xml file back into the .zip and import into CRM.  See this link for more details on the finer points of importing/exporting changes to the sitemap.  If you have users that you don't want to see this link you'll need to apply the appropriate 'Privilege' tag.  See this link for more details.  The tag would allow you to suppress the account subarea for all users unless they had account read privileges.    

Here's a screen shot of the SSRS reports added to the lower left hand corner:


Here's a screen shot of what the user would see once they click the 'SSRS Reports' link:


You can now use the SSRS security model to manage the report permissions to a much more ganular level.  See this link for more details.

Cheers...

Monday, April 18, 2011

Adding custom JavaScript in CRM 2011

Hello Gang,

In this post I want to cover adding JavaScript phone number formatting to a CRM form.  We'll go through adding the library as a web resources and attaching the function to a form attribute.

The first thing that we need to do is create the web resources:

  • Open the customizations tool in crm (opens in new windows)
  • Find the web resources node and open it
  • Click new in the toolbar.  You'll see the below screen pop up:
  •  Name the library, set the desplay name and description if needed.  Set the type to Script (JScript) and the Language to 'English'.  Click the 'Text Editor' button and paste in the following JavaScript:
function DC_FormatPhoneNumber(e) {
    /*Figure out what attribute called this*/
    if(!e) {e = window.event;}
    var element = e.srcElement;
    var value = Xrm.Page.data.entity.attributes.get(element.id).getValue(); 

    /*remove the non alpha characters*/
    value = value.replace(/[^0-9]/gi, "");


    if(value.length == 10) {
        var re = /^(\d{3})(\d{3})(\d{4})$/
        if(re.test(value) == true)
        {
            value = "("+RegExp.$1 + ") " + RegExp.$2 + "-" + RegExp.$3;
        }
    }else if(value.length == 11) {
        var re = /^(\d{1})(\d{3})(\d{3})(\d{4})$/
        if(re.test(value) == true)
        {
            value = RegExp.$1+" ("+RegExp.$2 + ") " + RegExp.$3 + "-" + RegExp.$4;
        }
    }else if(value.length == 7) {
        var re = /^(\d{3})(\d{4})$/
        if(re.test(value) == true)
        {
            value =  RegExp.$1 + "-" + RegExp.$2;
        }
    }

    Xrm.Page.data.entity.attributes.get(element.id).setValue(value);

}
  • Here's a screen shot of the text editor:
  • Now click 'Ok' and save the web resource.  Click publish after the save is complete
  • We are now ready to reference the newly created web resource from a CRM form.  It this case we'll use the account object.  Open the account object inside of the customizations tool:
  • Double click on the 'main form' line and open up the main form for editing.
  • At the top of the form there is a button labeled 'Form Properties.'  Click on this button.  The below window will open:
  •  We need to add the newly created web resource.  Click the first 'add' button in the 'Manage libraries that will be available in the form' section.  The below window will open: 

  • Select the web resource that you created and click 'OK'.  Back on the Form properties screen set the 'Control' field to 'Main Phone' and the 'Event' to 'OnChange.'  Now click the 'Add' button

  • This will bring up the below screen.  In the 'Library' field set the picklist to the web resource name.  In the Function field set the value to 'DC_FormatPhoneNumber' or what ever function you'd like to call.  

  • Click 'Ok.'  You can now save the form and publish it.  Phone numbers will be formated for the 'Main Phone' attribute.
Cheers and happy coding...