Friday, May 15, 2009

View PDF file stored in SharePoint using WEBDAV

WEBDAV is a great way to retrieve documents from SharePoint if all you want to do is display them. I was preparing a demo using CRM 4.0 and wanted to display a PDF file stored in a WSS document library within an I-Frame of the letter entity. I have the full url to the file stored in a custom attibute of CRM (uklg_linkeddocument) so I used an ASPX page to render the PDF file.

I was only interested in displaying PDF files but it is easy enough to extend to other file formats.

CRM 4.0:
I edited the Letter Form to include an I-Frame that points to the web page the I create below. Check the option to "Pass record object type code and unique identifier as parameters". I added a custom string attribute called uklg_linkeddocument. {How I poulate this field is covered in another post].
You need to publish the cusomtizations AND refresh the CRM web service to get the new attributes to appear in Intellisense in VS. To do this got to Settings -> Customizations -> Download Web Service Description (WSDL) files. Click on the CRMService.ASMX and copy the url from the browser window that opens.

VS 2005 Web Project: Include a Web Reference to the CRMService using the url copied in the CRM step. Call the Web Reference: CrmService.
Edit the ASPX of the web page and remove everything except the <@Page> declaration at the top.

In the .ASPX.CS file add the following namespaces:

using System.IO;
using System.Net;
using System.Text;
using CrmService;

You'll need to change the Organization name for CRM 4.0, the service.url and the credentials you need for accessing CRM 4.0 and SharePoint.

The Page_Load event code is below:

protected void Page_Load(object sender, EventArgs e)
string doc = "";
string strSourceURL = "";
if (Request.QueryString["id"] != null)
// Set up the CRM Service.

CrmAuthenticationToken token = new CrmAuthenticationToken();
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0;
token.OrganizationName = "MyDemo";

CrmService.CrmService service = new CrmService.CrmService();
service.Url = "http://localhost:5555/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = new System.Net.NetworkCredential("Administrator", "Password1", "Integration"); //CredentialCache.DefaultCredentials;

// letter activity = new letter();
Guid createdLetterId = new Guid(Request.QueryString["id"].ToString());
letter activity = (letter)service.Retrieve(EntityName.letter.ToString(), createdLetterId, new AllColumns());
// path to the linked document
strSourceURL = activity.uklg_linkeddocument;


if (strSourceURL != "")

doc = strSourceURL;

WebRequest req = WebRequest.Create(strSourceURL);
req.Method = "GET";

// Network Credentials.
NetworkCredential cred = new NetworkCredential(

req.Credentials = cred;

WebResponse resp = req.GetResponse();
Stream str = resp.GetResponseStream();

int len = (int)resp.ContentLength;

byte[] buffer = new byte[len];
str.Read(buffer, 0, len);

Response.Charset = "windows-1252";
if (doc.EndsWith("pdf"))
Response.ContentType = "application/pdf";

if (doc.EndsWith("txt"))
Response.ContentType = "text/html";
ASCIIEncoding enc = new ASCIIEncoding();
Response.Write(enc.GetString(buffer, 0, len));

catch (Exception ex)



Unknown said...
This comment has been removed by a blog administrator.
Steve said...
This comment has been removed by a blog administrator.