none
How to get Name, Url, Type of items in Documents? RRS feed

  • Question

  • Hi,

    I need to get the Name of the item, Its type (either as folder/file), absolute url of items inside Documents library of OneDrive on mysite to be listed in a web page using REST and AJAX calls. I used _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Documents')/items?$select=FileLeafRef,File/Name,File/Title,File/Type&$expand=File

    $.each(data.d.results, function(i,result) {

    alert(result.FileLeafRef  + result.Name + result.Type + result.Title);

    }

    But it comes as undefined.

    Most importantly I am trying to get the items and items of all the folders and items in one call.

    Any advise? Thanks.



    • Edited by MC ROY Monday, April 24, 2017 6:44 AM
    Monday, April 24, 2017 6:34 AM

All replies

  • Hi,

    Please use the following code:

    <script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(function () {
    	// begin work to call across network
    	var requestUri = _spPageContextInfo.webAbsoluteUrl +
    				  "/_api/web/lists/getByTitle('Documents')/items?$select=FileLeafRef,File_x0020_Type,File/Name,File/Title&$expand=File";
    
    	// execute AJAX request
    	$.ajax({
    		url: requestUri,
    		type: "GET",
    		headers: { "ACCEPT": "application/json;odata=verbose" },
    		success: function (data) {
    			$.each(data.d.results, function(i,result) {
    				alert(result.FileLeafRef  +" | "+ result.File.Name + " | "+result.File_x0020_Type + " | "+ result.File.Title);
    			});
    		},
    		error: function () {
    			alert("Failed to get details");
    		}
    	});
    });
    </script>

    If the item is a folder, the result.File.Name and result.File.Title will return undefined, and result.File_x0020_Type return null.

    If the item is a file, we can see this:

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, April 27, 2017 3:33 AM
    Moderator
  • Hi,

    Any update?

    If my reply helps you, please mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, May 4, 2017 7:49 AM
    Moderator
  • Dennis, Thanks for your help. Is there a way to build the same structure of folders/files of One Drive along with its url. So that I can use this in home page of web app to provide direct access to their one drive files?
    Saturday, May 13, 2017 4:38 AM
  • Hi,

    For OneDrive, we can also use the REST URL like this:

    http://sp2013/my/personal/administrator/_api/web/lists/getByTitle('Documents')/items?$select=FileLeafRef,File_x0020_Type,File/Name,File/Title&$expand=File

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, May 15, 2017 1:16 AM
    Moderator
  • Hi Dennis,  I am still trying to build the TREE structure of Folders and Documents of Document Library of One Drive and Unable to achieve it due to the complexity of coding. I still need to build the exact folder and files structure of One Drive documents library using CSOM/JS.
    Monday, May 22, 2017 4:42 AM