How to Build a SharePoint Document Viewer Using GroupDocs.Viewer for .NET

When we released GroupDocs.Viewer for .NET, we got lots of questions from SharePoint users about whether it could be used to build a SharePoint document viewer. Finally, the answer is yes: our online document viewer allows integration with SharePoint web applications. Moreover, SharePoint websites using GroupDocs.Viewer for .NET can also be used for cross-domain document viewing. How to Build a SharePoint Document Viewer Using GroupDocs.Viewer for .NET Just follow these simple step-by-step instructions to embed our online document viewer in your SharePoint websites as a Web Part: Requirements
  • Server running the GroupDocs document viewer service
  • Microsoft SharePoint 2010
  • Microsoft Visual Studio 2010
The following steps illustrate a typical process for creating of a Web Part which can be used to run the GroupDocs document viewer service: 1. Open Visual Studio 2010 as administrator and create a new Empty SharePoint Project. Note: If you have not run as an administrator, then Visual Studio will prompt you to restart using different credentials as shown below:   2. Specify the local SharePoint server and then click Finish. Note: You cannot use a SharePoint instance outside your workstation. All the necessary files are created as shown below:   3. To create a Web Part in the project, click Web Part and then click Add. The necessary files needed for the Web Part are created as shown.   4. We are using WebPartConsumer.cs to write functions to access the GroupDocs document viewer service. Add the following lines to the WebPartConsumer.cs file:
protected override void RenderContents(HtmlTextWriter writer)
        {
// Fetch JavaScript Libraries
  string scriptLibraries = GetHtmlPageContents("http://localhost:2020/Home/GetJavaScriptLibraries");
// Fetches the  inline Scripts
  string inlineDocPreviewScript = GetHtmlPageContents("http://localhost:2020/Home/GetInlineDocumentScript");

            writer.Write(scriptLibraries);
            writer.Write("
");
            writer.Write(inlineDocPreviewScript);
          // Renders the contents to the page.
            base.RenderContents(writer);
        }
//Function to get the contents from a given URL
    private static string GetHtmlPageContents(string strURL)
        {
            String strResult;
            WebResponse objResponse;
            WebRequest objRequest = HttpWebRequest.Create(strURL);
            objResponse = objRequest.GetResponse();
            using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
           	 {
            	    strResult = sr.ReadToEnd();
           	 }
            return strResult;
        }
  5. Now, deploy the project. The Output screen is displayed: On the Output screen, you can see that the solution ViewConsumer.wsp is successfully deployed to the SharePoint server.   6. To view the control in a local SharePoint instance, you have to add it to your page as a web part.   7. To add the control, go to a page intended for editing.   8. Add the Web Part by using the option Add a Web Part as shown below.   9. The Web Part (WebPartConsumer) is listed as shown below.   10. Click Add to add the Web Part to the page. 11. Click Stop Editing to save the page. After saving, you can see the WebPart, WebPartConsumer, in action as shown below. Building a SharePoint Document Viewer Using The GroupDocs.Viewer for .NET Thus, the GroupDocs document viewer service, integrated in a web application, is consumed in a SharePoint website.