Programmatically Retrieve Documents from SharePoint

Roshan Mehta, 16 December 2012

We recently built a mechanism inside a custom web portal where a client’s customers could log in, select an item from a grid and then click a button to download a matching document stored within SharePoint Online. There are many approaches you could use to build this functionality, but I suggest using the lists.asmx web service to avoid headaches.  This web service exposes multiple web methods that allow you to manipulate SharePoint lists but the one we are interested in is the GetListItems method.

There are a few things you will need before we start – the correct URL to the lists.asmx endpoint, and credentials to access the SharePoint site (with sufficient permissions). You will also need a basic understanding of writing CAML (Collaborative Application Markup Language).

Firstly, create a new project inside Visual Studio and add a new service reference. Enter the endpoint address, username, and password for the lists.asmx web service.

Programmatically Retrieve Documents from SharePoint

Click on Advanced and then select Add Web Reference… Select the endpoint URL from the drop-down list and specify a web reference name.

Programmatically Retrieve Documents from SharePoint

Now that we’ve added our references, we can begin consuming the GetListItems method. This method takes in the following parameters:

• ListName
• ViewName
• Query
• ViewFields
• RowLimit
• QueryOptions
• WebID

In our example, we only care about the ListName, Query, ViewFields, and QueryOptions parameters. All other parameters can be passed in as null.

Procedure – Specifying Credentials

Lists list = new Lists();
list.Credentials = new NetworkCredential("me@mysharepoint.spsite.com", "password");

Procedure – Building the CAML Query

The following CAML query will retrieve all documents inside the “Magnetism Folder”. It will return the EncodedAbsUrl, LinkFileName, and Created attributes.

<mylistitemrequest> 
  <Query> 
    <Where> 
      <And> 
        <Eq> 
          <FieldRef Name="FSObjType=" />
          <Value Type="Lookup">0</Value>
        </Eq> 
        <Contains> 
          <FieldRef Name="FileDirRef"/>
          <Value Type="Text">Magnetism</Value>
        </Contains> 
      </And> 
    </Where> 
  </Query> 
  <ViewFields> 
    <FieldRef Name="EncodedAbsUrl="/>
    <FieldRef Name="LinkFilename"/>
    <FieldRef Name="Created"/>
  </ViewFields> 
  <ViewAttributes Scope="Recursive"/>
  <QueryOptions> 
    <Folder>Magnetism</Folder>
  </QueryOptions>
</mylistitemrequest> 

Procedure – Calling the GetListItems Method

The following code will make a call to the GetListItems method to retrieve the documents from the “Customers List”. The CAML query above filters specifically on the “Magnetism” folder in SharePoint.

Programmatically Retrieve Documents from SharePoint

Note: The code to load the above XML into an XmlDocument has been ignored to save space.

XmlNode query = doc.SelectSingleNode("//Query");
XmlNode viewFields = doc.SelectSingleNode("//ViewFields");
XmlNode queryOptions = doc.SelectSingleNode("//QueryOptions");

XmlNode results = list.GetListItems("Customers", null, query, viewFields, null, queryOptions, null);

Procedure – Processing the Results

We now need to iterate through the results to find all documents returned by the CAML query.

XmlNodeList childNodes = results.ChildNodes;

foreach
(XmlNode node in childNodes)
{
    XmlNodeReader reader = new XmlNodeReader(node);
    while (reader.Read())
    {
        if (reader["ows_EncodedAbsUrl"] != null && reader["ows_LinkFilename"] != null && reader["ows_Created"] != null)
        {

            string url = reader["ows_EncodedAbsUrl"].ToString();
            string fileName = reader["ows_LinkFilename"].ToString();
            DateTime createdOn = Convert.ToDateTime(reader["ows_Created"]);

            Console.WriteLine("{0}, {1}, {2}", url, fileName, createdOn);
        }
    }
}

Enjoy!