Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharepoint 2013 GetListItems web service returning a list of folders in library, instead of list of files within a folder

I'm having a problem being able to get the list of items within a folder, it keeps giving me the list of folders in the library, and not the list of files within the specified folder.

I'm using a C# program, using the following method to call the web service GetListItems, and it all seems to work up to a point:

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/GetListItems", RequestNamespace="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace="http://schemas.microsoft.com/sharepoint/soap/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public System.Xml.XmlNode GetListItems(string listName, string viewName, System.Xml.XmlNode query, System.Xml.XmlNode viewFields, string rowLimit, System.Xml.XmlNode queryOptions, string webID) {
    object[] results = this.Invoke("GetListItems", new object[] {
                listName,
                viewName,
                query,
                viewFields,
                rowLimit,
                queryOptions,
                webID});
    return ((System.Xml.XmlNode)(results[0]));
    }

The values of the parameters are as follows:

listName = "Letters"
viewName = null
query = null
viewFields.OuterXml = "<ViewFields>
                            <FieldRef Name=\"ID\" /> 
                            <FieldRef Name=\"Title\" />
                            <FieldRef Name=\"Modified\" />
                            <FieldRef Name=\"Status\" />
                            <FieldRef Name=\"_UIVersion\" />
                            <FieldRef Name=\"_UIVersionString\" />
                            <FieldRef Name=\"EpisodeId\" />
                        </ViewFields>"
rowLimit = null
queryOptions.OuterXml = "<QueryOptions>
                            <IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>
                            <Folder>http://D3TVCAPP-APP02.test.local/Letters/ABBOTT, Nash  _489611</Folder>
                            <DateInUtc>TRUE</DateInUtc>
                         </QueryOptions>"
webID = null

And it actually returns a result, it is hitting the service ok, the result looks like this:

results.OuterXml =  
    "<listitems xmlns:s=\"uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882\" xmlns:dt=\"uuid:C2F41010-65B3-11d1-A29F-00AA00C14882\" xmlns:rs=\"urn:schemas-microsoft-com:rowset\" xmlns:z=\"#RowsetSchema\" xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">
        <rs:data ItemCount=\"2\">
            <z:row 
                ows_ID=\"84\" 
                ows_Modified=\"2021-06-09T09:50:11Z\" 
                ows__UIVersion=\"1\" 
                ows__UIVersionString=\"0.1\" 
                ows__ModerationStatus=\"2\" 
                ows__Level=\"2\" 
                ows_UniqueId=\"84;#{C8E8EC38-6DD5-4275-873A-B829BA850A54}\" 
                ows_owshiddenversion=\"1\" 
                ows_FSObjType=\"84;#1\" 
                ows_Created_x0020_Date=\"84;#2021-06-09T09:50:11Z\" 
                ows_ProgId=\"84;#\" 
                ows_FileLeafRef=\"84;#ABBOTT, Nash  _489611\" 
                ows_PermMask=\"0x7fffffffffffffff\" 
                ows_FileRef=\"84;#Letters/ABBOTT, Nash  _489611\" 
                ows_Editor=\"1;#John Smith\" 
                ows_MetaInfo=\"84;#\" 
                ows_Last_x0020_Modified=\"84;#2021-06-09T09:50:14Z\" 
            />
            <z:row 
                ows_ID=\"3\" 
                ows_Modified=\"2020-11-04T13:02:42Z\" 
                ows__UIVersion=\"1\" 
                ows__UIVersionString=\"0.1\" 
                ows__ModerationStatus=\"2\" 
                ows__Level=\"2\" 
                ows_UniqueId=\"3;#{1CA7B690-BF90-41EC-A5EA-E910C8D72376}\" 
                ows_owshiddenversion=\"1\" 
                ows_FSObjType=\"3;#1\" 
                ows_Created_x0020_Date=\"3;#2020-11-04T13:02:42Z\" 
                ows_ProgId=\"3;#\" 
                ows_FileLeafRef=\"3;#SMITH, JOHN  _483835\" 
                ows_PermMask=\"0x7fffffffffffffff\" 
                ows_FileRef=\"3;#Letters/SMITH, JOHN  _483835\" 
                ows_Editor=\"1;#John Smith\" 
                ows_MetaInfo=\"3;#\" 
                ows_Last_x0020_Modified=\"3;#2020-11-06T16:59:03Z\" 
            />
        </rs:data>
    </listitems>"

Apologies for the wall of code, I just hope that someone can help me and want to provide as much info as possible. I can't figure this one out, I feel like I'm doing everything right. Anyone got any idea what I 'm doing wrong?

like image 732
necrofish666 Avatar asked Nov 06 '22 01:11

necrofish666


1 Answers

C# Programming give for developers two options to consumes SharePoint data; seeing your code, I think you try to consume SharePoint data using REST API approach. The REST API approach is commonly used to make interfaces with SharePoint and external Systems written by other programming languages (different of C#) and not used a lot by C# developer community in frequency; but, it's need to be clear: it's not a rule and it's not impossible to consumes SharePoint data using REST API from C#, but this is more "difficultly" comparing with CSOM approach.

Now, I will show you an example using CSOM approach to get files from specific folder, and you get your conclusions:

  1. First of all, download and install SharePoint 2013 Client SDK;
  2. After installed SDK, create some standalone app, like a Console App using .NET Framework 4.5 or later;
  3. Add in References (search in Framework) two DLLs: Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll;
  4. Add this code-file bellow in your Console App Project:
using System;
using System.Net;
using System.Security;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;

namespace SharePoint.Csom.Approach
{
    public class SharePointDocumentModel
    {
        public string FileName { get; set; }

        public string Url { get; set; }

        public string Version { get; set; }
    }

    public class SharePointDocuments : IDisposable
    {
        private bool _disposed = false;

        protected readonly ClientContext _clientContext;

        public SharePointDocuments(string sharepointSiteUrl, string user, SecureString password)
        {
            if (string.IsNullOrWhiteSpace(sharepointSiteUrl) || !sharepointSiteUrl.StartsWith("http"))
            {
                throw new InvalidOperationException("Impossible to connect with invalid URL.");
            }
            this._clientContext = new ClientContext(sharepointSiteUrl);
            this._clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
            this._clientContext.Credentials = new NetworkCredential(user, password);
        }

        public IEnumerable<SharePointDocumentModel> GetFiles(string folderRelativeURL)
        {
            Web web = this._clientContext.Web;
            this._clientContext.Load(web);
            this._clientContext.ExecuteQuery();
            Folder folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + folderRelativeURL);
            this._clientContext.Load(folder);
            this._clientContext.ExecuteQuery();
            FileCollection fileCol = folder.Files;
            this._clientContext.Load(fileCol);
            this._clientContext.ExecuteQuery();
            foreach (File file in fileCol)
            {
                string fileVersion = null;
                FileVersionCollection oFileVersionCollection = file.Versions;
                this._clientContext.Load(oFileVersionCollection);
                this._clientContext.ExecuteQuery();
                foreach (FileVersion oFileVersion in oFileVersionCollection)
                {
                    fileVersion = oFileVersion.VersionLabel;
                }
                yield return new SharePointDocumentModel()
                {
                    FileName = file.Name,
                    Url = file.ServerRelativeUrl,
                    Version = fileVersion
                };
            }
        }

        public void Dispose()
        {
            this.Dispose(true);
        }

        private void Dispose(bool disposing)
        {
            if (this._disposed)
            {
                return;
            }
            if (disposing)
            {
                if (null != this._clientContext)
                {
                    this._clientContext.Dispose();
                }
            }
            this._disposed = true;
        }

        ~SharePointDocuments()
        {
            this.Dispose(false);
        }
    }
}
  1. At Program class and in Main method, add this code:
List<SharePoint.Csom.Approach.SharePointDocumentModel> files = new List<SharePoint.Csom.Approach.SharePointDocumentModel();
using (SharePoint.Csom.Approach.SharePointDocuments sharepoint = new SharePoint.Csom.Approach.SharePointDocuments("http://D3TVCAPP-APP02.test.local", "user", "password"))
{
    files = sharepoint.GetFiles("/Letters/ABBOTT, Nash  _489611").ToList();
}
  1. Voilá!

After made your tests, give us the feedback.

Thanks.

like image 156
Antonio Leonardo Avatar answered Nov 15 '22 08:11

Antonio Leonardo