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:
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:
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.
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.
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:
The SharepointItemFactory is a simple factory class which creates a concrete instance of SharepointItemBase based on supplied XML row.
Complete source code can be obtained here.
LukeN 090818
This post has 9 feedbacks awaiting moderation...