As a part of a client portal project we had a requirement to load documents displayed in the portal from a SharePoint document library. This seems like quite an interesting idea, because you don't need to build any fancy Content Management System (CMS) into your site for people to upload content and documents; they will just use standard SharePoint user interface to save documents into document library and your portal will take the content from there.

That's why I decided to create a generic component which will take documents from a document library on SharePoint and publish them as a part of any website.

The component loads data from a SharePoint Document Library:

And displays them inside a standard ASP.NET web application:

This article describes how you can build such solution.

Follow up:

SharePoint Web Service

As a first step, we need to obtain list of documents which are present in the document library. Microsoft SharePoint provides several web service interfaces, but we are going to use just one of them called Lists Web Service. This service provides methods to work with lists and their data, which is all we need for our purpose. We will use the GetListItems method to retrieve documents (and subfolders) in the source document library.

The GetListItems methods has the following signature:

  • listName – name of the list to get the data from; this is fixed value set through web.config in our application
  • viewName – name of the view used to get the date; we are not using this parameter
  • query – XML which defines query is used to restrict items which are returned by the service; we will use this parameter to retrieve data about specific item
  • viewFields – XML which defines which columns should be returned in the resulting data set; we are using GetList method to get list of columns to build this value
  • rowLimit – expression to limit number of records returned; not used by our application
  • queryOptions – XML which defines additional query options, such as parent folder identifier or whether the query should be recursive; we are using this parameter to browse through subfolders
  • webId – web site identifier; not user by our application

So to obtain list of top level items, we simply call:

XmlNode items = listsService.GetListItems(listName, null, null, fields, null, null, null);

This results in the following XML:

Each row in the XML structure represents a document or a subfolder. This is determined by the ows_ContentType attribute. To display items in the grid, we will also need name of the item (ows_LinkFilename attribute) and its unique identifier (ows_GUID).

In order to be able to browse through subfolder structure, we simply need to get the ows_ServerUrl attribute and use it in queryOptions parameter. For instance, the code to display items in subfolder called “Folder” would look like this:

string queryOptions = "<Folder>/Sandbox/My Documents/Folder</Folder>";

XmlNode items = listsService.GetListItems(listName, null, null, fields, null, queryOptions, null);

The resulting XML would then look exactly the same like in previous case.

Downloading documents

We know how we can browse the document library, now let's how simple it is to actually download a document from SharePoint and let clients download it as if the document was placed directly on the web site. All we need is the value of ows_EncodedAbsUrl attribute of a document. Let's say we want to download the bugfixcost.png file from the previous example. The could would look something like this:

string serverUrl= "http://memos-rs/Sandbox/My%20Documents/bugfixcost.png";
string documentName= "bugfixcost.png";

//Establish the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(document.EncodedUrl);
request.Credentials = CreateCredentials();
request.Timeout = QUERY_TIMEOUT;
request.UserAgent = USER_AGENT;

//Retrieve request info headers
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
BinaryReader responseStream = new BinaryReader(response.GetResponseStream());

//Download data 
byte[] data = responseStream.ReadBytes((int)response.ContentLength);
responseStream.Close();

//Send the document content as response
Response.Clear();
Response.ContentType = response.ContentType;
Response.AddHeader("Content-Disposition", "attachment;filename=" + documentName);
Response.BinaryWrite(data);
Response.End();

Once the code is placed in Form_Load procedure of a page, user will be able to download or open (based on the file type) the document.

Wrapping it all up

In the previous sections I briefly explained the principle behind loading documents from SharePoint. Now it is time to wrap this up into a reusable component so that it can be used in any project. I have designed the following architecture:

The component contains two classes which correspond item types on SharePoint – Document and Folder. Both classes inherit from common parent called SharepointItemBase, which provides basic functionality common for both item types (such as identifier, name, etc.).

The main module is the SharepointClient class, which provides these 4 methods:

  • GetItems – returns all items in specified folder (or root folder if not specified); returned collection can contain both Folder and Document instances
  • GetDocument – returns a Document item based on supplied identifier; the document content is not downloaded when calling this method
  • GetFolder – returns a Folder item based on supplied identifier
  • DowloadDocument – downloads document content for supplied document item

The SharepointItemFactory is a simple factory class which creates a concrete instance of SharepointItemBase based on supplied XML row.

Source Code

Complete source code can be obtained here.

LukeN 090818

Technorati:

Feedback awaiting moderation

This post has 9 feedbacks awaiting moderation...

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)

Contact Us


Are you interested in professional services of the developers who publish on this blog? Contact us on our web site now.

XML Feeds

Add to Technorati Favorites
September 2010
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30