Sunday 14 March 2010

Fix to Report viewer problems in IIS 7 or later

When we migrate web applications from IIS 6 to IIS 7 or IIS 7.5, we will face some problems in http handlers, mappings etc. I faced some problems with reportviewer control. So, below are all problems I have faced and solutions to them. And one more thing is, the server to which we migrated the application may or may not have all the report viewer dlls available in the system. If they didn't install then we need to install them. Otherwise you will get compilation error as we are using that in our application.

First of all, before proceed what are the dlls needed for the report viewer to run?
Microsoft.reportviewer.common.dll
Microsoft.reportviewer.processingobject.dll
Microsoft.reportviewer.webforms.dll
Microsoft.reportviewer.winforms.dll - For windows applications.

So, to get the dlls you need to install the redistributable packages for 2005/2008. Below are details to get that. Find the matched version[2005/2008] and isntall the correct patch. Once installed you will find all above dlls in the GAC.

Microsoft Report Viewer Redistributable 2008
File name : ReportViewer.exe
Version : 9.00.21022.08
Download Size : 2.8 MB

Microsoft Report Viewer Redistributable 2005
File name : ReportViewer.exe
Version : 2007
Download Size : 1.8 MB

Microsoft Report Viewer Redistributable 2005 SP1 (Upgrade)
File name : VS80-KB933137-X86.exe
Version : 1
Download Size : 1.7 MB

Microsoft Report Viewer Redistributable 2005 SP1 (Full Installation)
File name : ReportViewer.exe
Version : 1.0
Download Size : 1.8 MB

ReportViewer Samples for Microsoft Visual Studio 2008
File name : ReportViewerSamples2008.exe
Version : 1.0
Download Size : 172 KB

ReportViewer Samples for Visual Studio 2005
File name : ReportViewerSamples.exe
Version : 1.0
Download Size : 173 KB

With the installed patch, you can solve the compilation error. But, the report viewer control won't render correctly on the browser. What are the possible problems come?
  1. 'RSClientController' is undefined
  2. Report resource images are not coming or loading.
  3. RSParamaters  not defined.
  4. ReportViewerHoverButton not defined etc...
  5. Other javascript errors which caused the report viewer failed to load correct.
The only fix to the problems are below.
Solution:
We need to configure the report viewer auto generated axd files[Reserved.ReportViewerWebControl.axd] in the IIS.
See below pictures to understand it well.
1. Open the IIS by typing the "inetmgr" in run command.
2. Goto your site in the list of web sites and select it as shown below.
3. Now, see the Features view area. Here, you can see all options available for the web site. From all the options select "Handler Mappings".
4.  Now, click on the "Handler Mappings" section then you will see all the mappings applied for that web site. Now, on the right side panel, you are able to see all the options available for the handler mappings. Select the option "Add Managed Handler" as shown below.
5. When you click that link, you will see a popup window with the text boxes as shown below.
Fill, Path = Reserved.ReportViewerWebControl.axd,
Type = Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Name= Reserved.ReportViewerWebControl.axd
Note: Please change the version number[9.0.0.0] in the Type above given depends on your requirement.
6. Click OK and do IISRESET.
Now, browse the page where you have report viewer control. And you see no more issues and everything renders fine. Hope this will help you to understand well and please let me know, if you have any questions or issues.

Fix for Not able to connect to SharePoint 2010 server web sites through Visual Studio 2010

Today, I started working on SharePoint 2010 server side object model to delete all the files from the document library using batch command. I have written all the code but, I failed to connect to the web site using SPSite class. I tried all combination like with server name, ip address and many more. Always I am getting the exception that "not able to connect to SharePoint site".
This is the actual exception :
"The Web application at http://nb16 could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application."

Below figure gives you complete details.
Below is the code [Console application] I have used to communicate with the sharepoint server.
class Program
{
static void Main(string[] args)
{
DeleteAllItemsUsingBatch();
}
private static void DeleteAllItemsUsingBatch()
{
using (SPSite site = new SPSite("http://nb16"))
{
SPWeb web = site.OpenWeb("/");
SPList list = web.Lists["Documents"];
StringBuilder sb = new StringBuilder();
sb.Append("");
string batchCommand = "<method><setlist scope=\"Request\">" + list.ID + "</setlist><setvar name=\"ID\">{0}</setvar><setvar name=\"Cmd\">DELETE</setvar><setvar name=\"owsfileref\">{1}</setvar></method>";
foreach (SPListItem item in list.Items)
{
sb.AppendFormat(batchCommand, item.ID.ToString(), item.File.ServerRelativeUrl);
}
sb.Append("
");

web.AllowUnsafeUpdates = true;
site.RootWeb.ProcessBatchData(sb.ToString());
web.AllowUnsafeUpdates = false;
web.Close();
}
}
}

Did you find any problem with the code? Not really. There are no problems with the code at all.. Then what's wrong?
When I researched on the net for the solutions I found below information.
1. Is SharePoint site running?
2. Did you develop code on the same SharePoint server?

But, everything fine. There are no issues. Then what was the problem? I was frustrated for 3 hours to find the solution.
Solution:
It's simple that the platform target in build options was set to the x86 format by default. It should be set to x64 to work correct.
So, sometimes the simple problems will take more time to fix. Enjoy the nice series of posts on SharePoint 2010.

Tuesday 9 March 2010

Sunday 7 March 2010

SharePoint 2010 Silverlight Client Object Model - How to use

As part of the introduction series, I want to present the advantage of the client object model introduced in SharePoint 2010. There are great advantages with this model as it don't require SharePoint needs to be installed on the client machine. We just need to refer the client dlls which Microsoft SharePoint provides and based on them we will write code to communicate with SharePoint server. In this article we will go through Silverlight Client Object Model. If you want to know the other client object model types go here. ECMAScript and Managed client object models.

To communicate with the SharePoint server in Silverlight context we need to give two client SharePoint DLL references to the silver light project.

DLL's Needed: Microsoft.SharePoint.Client.Silverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll. They can be found at "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin".
OK, we understand the concept and  we will create a project and implement code for better understanding on how it works.
  1. Open Visual Studio 2010.
  2. File -> New -> Project -> Visual C# -> Silverlight -> Select Silverlight Application project template as shown below.

  3. Give some name to the project. In my example, I have given some meaningful name like "SP2010Silverlight_HelloWorld" and create the project.
  4. Now, you see the below screen.

  5. What this meaning is "Do you want to create an ASP.NET web site and host the XAP file generated to the web site". For our example, it's really not needed. But, there is no problem by using that.
  6. Now next step is getting the SharePoint Silverlight Client Dll's reference to our project. So, for this get the SharePoint dll's to the client machine [Where we created project] and paste the DLL's in some safe location. I copied them to C:\SP2010_ClientDLL\.
  7. Now, go to Visual Studio 2010 project right click on project -> select References and browse to location where client dll's copied and select both dll's and hit ok as shown in below figure.

  8. After you added all references the references folder should look like this.

  9. Now we are ready with all prerequisites and part left is with writing code. I will show you simple code on how to write the code for getting web site title and description using Silverlight Client OM.
  10. Before start coding, we need to add reference to the namespace in page by declaring using keyword as shown below.

    using Microsoft.SharePoint.Client;
  11. This is the code to get data from a SharePoint server, in this example we are retrieving Title and Description of a web site.
XAML code: MainPage.XAML













MainPage.Xaml.cs file code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;

namespace SP2010Silverlight_HelloWorld{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();

private void btnLoadSite_Click(object sender, RoutedEventArgs e)
{
context = ClientContext.Current;
web = context.Web;
context.Load(web, w => w.Title, w => w.Description, w => w.ServerRelativeUrl);
context.ExecuteQueryAsync(OnSiteLoadSuccess, OnSiteLoadFailure);
}

private void OnSiteLoadSuccess(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUI = LoadSiteData;
this.Dispatcher.BeginInvoke(updateUI);
}
private void OnSiteLoadFailure(object sender, ClientRequestFailedEventArgs e)
{
MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);
}

private void LoadSiteData()
{
canvasLabels.Visibility = System.Windows.Visibility.Visible;
label2.Content = web.Title;
label4.Content = web.ServerRelativeUrl;
label6.Content = web.Description;
}
}
}

Place all the code above given in the both files of your project.
Note: Remember to change the web url given in the code "http://nb16" to actual SharePoint server url.

Till now, what we have done is, writing and complete code for loading the site data. But, we need to understand the above code.
In the above code, there is a callback function used. Which is asynchronous and loads data. But, you may confuse at the line delegate UpdateUIMethod(). I copied the below text from MSDN to better understand about the delegate and why it's needed.

"Because query execution is asynchronous when you use the SharePoint Foundation Silverlight object model, you must pass delegates for callback methods as parameters in the ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) method, similarly to query execution in the ECMAScript object model. However, to run code that makes changes in the user interface (UI) through the Silverlight object model, you must delegate this work to the Dispatcher object of the thread that created the UI by calling BeginInvoke()". So, we should use the delegate to make changes on the UI.

Deploy and Test
We have two ways to deploy the XAP file in SharePoint environment.
One is, We can use SharePoint default location [\Templates\Layouts\ClientBin] and deploy the file there. Refer this location from the Silverlight web part.
Second is, We can use a SharePoint document library and deploy the file there. Refer this document library location file path while adding the silverlight web part. In this post, we will use the default location to deploy and test.
  1. To deploy and test the code in SharePoint 2010 we need to use the SharePoint Silverlight web part [New web part added in this version].
  2. The silverlight web part default location is "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin". So, all the XAP files should be deployed to this location to use them in the silverlight web part.
  3. To make this process easier, we need to do below.
  4. Right click on Silverlight project -> propertiese -> Build -> change the output path to "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin" as shown below.

  5. Build the solution and see the XAP generated in the ClientBin location. 
  6. Now, navigate to SharePoint site where you want to see the silverlight data, Edit page.
  7. Add silverlight web part to the page. 
  8. It will prompt you for the XAP file location: Type the url: "/_layouts/ClientBin/SP2010Silverlight_HelloWorld.xap".
  9. Now click on OK and you can see the silverlight web part on the page.

Ooh!!! This is it!!! We are done with development, deployment and testing. Hope this won't create any confusion. If you have any issues please let me know. I am always here to help you out...

SharePoint 2010 Client Object Model - Managed Client OM - How to use

After I have written nice posts on complete details on Client Object Model and ECMAScript introduction planned to write introduction posts on Managed Client OM and Silverlight Client OM. So, in this post, we will discuss on Managed Client Object Model.

So, Managed Client Object Model is simple .NET code written based on CLR. So, you can use C#.net or VB.net to write code and execute whatever you want against SharePoint server.So, you can use any c# project to write code and run it. But, are there any prerequisites to write code? Do we need to follow some rules or process?

First, to write client side code, we need SharePoint libraries which has all the methods and classes to communicate with the SharePoint server. For this reason Microsoft SharePoint 2010 provides us the client side dll's to write Managed code. Below are the details.
DLL's needed: Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll. Find these files in the 14/ISAPI folder. Usually, the location would be at "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI".

How to create a project to start working and communicate to SharePoint 2010 server? Follow the steps below.
  1. Open Visual Studio 2010.
  2. File -> New -> Project -> Visual C# -> Choose Console Application from the list of C# project types. [Make sure you have selected .NET Framework 3.5 on the top of window as shown in below.]

  3. Give some name to the project. I have given some valid name as "SP2010_HelloWorld".
  4. Now next step is getting the SharePoint Client Dll's reference to our project. So, for this get the SharePoint dll's to the client machine [Where we created project] and paste the DLL's in some safe location. I copied them to C:\SP2010_ClientDLL\.
  5. Now, go to Visual Studio 2010 project right click on project -> select References and browse to location where client dll's copied and select both dll's and hit ok.
  6. Finally, our references folder should looks like below.

  7. Now we are ready with all prerequisites and part left is writing code. I will show you simple code on how to write the code for getting web site title and description.
  8. Before start coding, we need add reference to the namespace in page by declaring using keyword as shown below.

    using Microsoft.SharePoint.Client;
  9. This is the code to get data from a SharePoint server, in this example we are retrieving Title and Description of a web site.

    static void Main(string[] args)
    {
    LoadSiteData();
    }

    private static void LoadSiteData()
    {
    string webUrl = "http://nb16";
    ClientContext context = new ClientContext(webUrl);
    Web web = context.Web;
    //Loads all web properties.
    context.Load(web);
    //Execute the query and load the object into the response given in load() method.
    context.ExecuteQuery();
    Console.WriteLine(web.Title);
    Console.ReadLine();
    }
    NOTE: Remember, you need to change the webUrl to actual SharePoint site url in the above example.
  10. Now run the code and see the output. You will see the title and description of the SharePoint web site.
That's it!!! Very simple and easier way. SharePoint 2010 client side development is very easy now. Lets rock it....