Tuesday, December 7, 2010

Install missing templates in the installed Visual Studio templates

One day I tried to create new WCF Service Application and when i opened the Visual studio and clicked on Add new project I found that this project option is missing from my Visual studio templates. To add missing templates to your current installed templates you need to run following command on Visual studio command prompt after closing all the instances of the Visual studio.
devenv/installvstemplates
Press Enter.
Let the processing to be completed and then open visual studio. Now all the missing templates would be visible under Visual Studio installed templates.

Wednesday, December 1, 2010

Custom Assembly in Sql Server Reporting Services (SSRS).

Most of the times we need to write code in our SSRS reports. So, the goal of this article, to show you how easy it really is to write custom code for SQL Server Reporting Services. We can write the code in two ways:
1. Embedded code
2. Custom Assemblies

Embedded Code:
So let's get started and add some embedded code to a report. Open Visual Studio and create a new report project and a new report (Right click the project -> Add New Item -> Report).

Follow the steps:

1. Go to Report menu and click Report Properties.
(If the Report menu is not available, click within the report design area.)
2. On the Code tab, in Custom Code, type the code.
e.g
Public Function Hello() as String
Return "Hello All"
End Function

Now add a textbox to the report and add the code =Code.Hello() to the expression of the textbox. When you will preview the report you can see "Hello All" on the report. That is as far as we are going to take embedded code.

Custom Assemblies:
This is a much more useful feature since you aren't constrained to writing code in VB.Net on a small dialog in the report designer. The need for custom assemblies lies when we need to write code in such a way that is not possible in the embedded code. e.g. In my code I was trying to use the dynamic connection string for my reports. Firstly i tried to write the code as embedded but that had very limited functionality. So, I used custom asseblies to achieve this. It is not a big deal to write a custom assebly in own way. but the only thing is that we need to cope with the Code Access Security (CAS).

Firstly let me explain you how to create a custom assembly.

1. Open the Visual Studio and Create a Class library project in C# or VB (either of the langauages you are comfortable with). Say it CustomAssembly and rename Class1.cs to MyCustomClass.cs
2. Write the code in the class file e.g.
public static string LoadXML(string strenv, string Path)
{
XmlDocument xmldoc = new XmlDocument();
XmlNode node = default(XmlNode);
string XMLFilePath = Path;
XmlUrlResolver objResolver = new XmlUrlResolver();
objResolver.Credentials = CredentialCache.DefaultCredentials;
xmldoc.XmlResolver = objResolver;
xmldoc.Load(XMLFilePath);
if (strenv.ToLower() == "development")
      {
         node = xmldoc.SelectSingleNode("ConnectionString/Development");
      }
if (node != null)
                {
                    con = node.InnerXml;
                }
return con;
}
3. Now you are done with the code part for the custom assembly.
4. Copy the assembly from the bin folder to the C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies and C:\Program Files\Microsoft SQL Server\MSRS10_50.SQLSERVER2008R2\Reporting Services\ReportServer\bin
Note: I am using SQLServer 2008 R2 here so my folder is MSRS10_50.SQLSERVER2008R2, please check your folder and place the assembly file there.
4. The next step is to use this assembly within you SSRS. To acheive this Open Visual Studio and create a new report project and a new report (Right click the project -> Add New Item -> Report). Go to Report menu and click Report Properties.On the references tab, locate the assembly and add the assembly.
5. On the code tab write the code =CustomAssembly.MyCustomClass.LoadXML("development","some path")
This is the last step of custom assembly.
Now comes the Code Access Security part
Code Access Security
This code would not work until we dont have some permissions to run the DLL. The permissions could be IO permissions, environment permissions and security permissions. Add the following lines for the assert permissions.
System.Net.WebPermission webPermission;
webPermission = new System.Net.WebPermission(System.Net.NetworkAccess.Connect, Path);
webPermission.Assert();

For security permissions you can use the following method just after the class name.

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Assert, Unrestricted = true)]

Also, we need to change the rssrvpolicy configuration file. This file can be located under the Report server installation folder.

Open the file and add the following code snippet under the code group tag.
  
<CodeGroup class="UnionCodeGroup" version="1" PermissionSetName="FullTrust" Name="NewCodeGroup" Description="A special code group for my custom assembly.">
<IMembershipCondition class="UrlMembershipCondition" version="1"  Url="C:\Program Files\Microsoft SQL Server\MSRS10_50.SQLSERVER2008R2\Reporting Services\ReportServer\bin\CustomAssembly.dll" />
</CodeGroup>

This code generally provides the Full Trust permission to our custom assembly and enables it to override any permission barrier.