Retrieve Data from SharePoint Document Libraries (using SPServices)

Abstract

This article describes how to retrieve data from a SharePoint document library using SPServices.

Definition

"SPServices is a jQuery library which abstracts SharePoint's Web Services and makes them easier to use.[...]1"

Prerequisites

We will use a content Page in a SharePoint Online evironment, that contains a Inhalts-Editor/Content Editor Web-Part (CEWP). This CEWP needs to include the two JavaScript libraries jQuery and SPServices.

Process

After creating a page and adding a CEWP, we have to set the source (HTML+JavaScript) of the web part. We recommend to use a separate source file which makes editing a bit more comfortable, i.e. create a .HTML-file and set its URL as the source of the web part. Furthermore, using a separate file enables you to use one of your favorite source code editores, e.g. Notepad++.

The source file at least has to contain the reference to the JavaScript libraries, a "local" script and a some HTML in order to display the results.

In our case, we have added a button that is automatically shown and to which a click handler is assigned when the document completed loading. This button triggers the query (getData function). The function sends an asynchronous request to the server and retrieves an XML response. The response is then shown as text in the section below the button and furthermore the XML is processed with the SPServices lib, finding all content rows. For each item in the list, an HTML unordered list (UL) element (LI) is added and the complete result is shown at the bottom of the document.

<script src="https://consulity.sharepoint.com/consulity/projects/mik/content/jquery-1.9.0.min.js">

</script>

<script src="https://consulity.sharepoint.com/consulity/projects/mik/content/jquery.SPServices-2014.02.min.js">

</script>

 

<script>

 

    $(function () {

        $('#go').show().click(function () {

            getData();

            return false;

        })

    });

 

 

    function getData() {

        $().SPServices({

            operation: "GetListItems",

            async: true,

            listName: "MyListName",

            /* we can also use the list id "{c991267a-05dc-462c-909c-ef6942fea145}", */

            completefunc: function (xData, Status) {

                $('#result').text(xData.responseText);

                $(xData.responseXML).SPFilterNode("z:row").each(function () {

                    var liHtml = "<li>" + $(this).attr("ows_LinkFilename") +

                        ' // ' + $(this).attr("ows_Modified") + "</li>";

                    $("#tasksUL").append(liHtml);

                });

            }

        });

    }

</script>

 

<button id="go" style="display:none">Get Data</button>

<hr />

<label>Result XML:</label>

<div id="result" style="background:lightyellow; overflow-y:scroll;height:200px; width:800px;"></div>

 

<label>Result as list (UL):</label>

<div id="result-list" style="background:lightgray">

    <ul id="tasksUL" />

</div>


Caution:

We were experiencing problems using version 1.9.1 of jQuery. The response did not deliver an xml-document, rather a response-text was returned which could not be processed with the SPServices lib. The responseXML was undefined, whereas the responseText was available. Switching to jQuery 1.9.0 solved the problem and the resultXML was retrieved correctly.

Subfolders

It is possible to files located subfolders with the following option:

// option to include subfolders

var options = "<QueryOptions><ViewAttributes Scope='RecursiveAll'/></QueryOptions>";

 

$().SPServices({

    operation: "YourListName",

    // include option to get files located in sub-folders

    CAMLQueryOptions: options,

    async: true,

    listName: DocLibDesign.settings.splib,

    completefunc: function (xData, Status) {

        if (Status == "success") {

            spList2DocList(xData);

        }

    }

});

Link

Here is the Link to the working example. Pleae note, the SharePoint page is available to all authenticated users. At least a Microsoft account is needed to view the example.

Screenshot of the working example

working example preview

List of available attributes in z:row

ows_DocIcon='pdf’

ows_LinkFilename='Presentation 1 Report 2331 2013 Q1.pdf’

ows_Modified='2016-01-06 17:37:02’

ows_FileSizeDisplay='106662’

ows_ContentType='Dokument’

ows_Year='2013’

ows_Edit='0’

ows_LinkFilenameNoMenu='Presentation 1 Report 2331 2013 Q1.pdf’

ows__ModerationStatus='0’

ows__Level='1’

ows_ID='27’

ows_UniqueId='27;#{D6814BED-B9AA-4CE8-BD8B-52E9ED6CF8FD}’

ows_owshiddenversion='2’

ows_FSObjType='27;#0’

ows_Created_x0020_Date='27;#2016-01-06 17:19:14’

ows_ProgId='27;#’

ows_FileLeafRef='27;#Presentation 1 Report 2331 2013 Q1.pdf’

ows_PermMask='0x7fffffffffffffff’

ows_FileRef='27;#consulity/projects/mik/DocLibDesign/Presentation 1 Report 2331 2013 Q1.pdf’

ows_Editor='6;#consulity | Dieter Neumann’

ows_MetaInfo='27;#vti_parserversion:SR|16.0.0.4719&#13;&#10;vti_folderitemcount:IR|0&#13;&#10;vti_author:SR|i:0#.f|membership|dn@consulity.net&#13;&#10;Year:SW|2013&#13;&#10;vti_modifiedby:SR|i:0#.f|membership|dn@consulity.net&#13;&#10;vti_foldersubfolderitemcount:IR|0&#13;&#10;ContentTypeId:SW|0x010100B244593EE011DC49B374CE4845EEB2CD&#13;&#10;

ows_Last_x0020_Modified='27;#2016-01-06 17:37:01’

 


Dieter Neumann