none
SharePoint 2013 - Get All User Names and Emails Using REST API RRS feed

  • Question

  • Hello Community,

    I am working in SharePoint 2013 and I am trying to get all the site users by utilizing a REST API query.  Unfortunately while my query works in Postman it does not work in SP.  My code is below, can anyone see where the problem is?

    <script src="/prgm/provost/CampaignPlan/SiteAssets/JQuery/jquery-3.1.1.min.js" type="text/javascript"></script>
    <script src="/prgm/provost/CampaignPlan/SiteAssets/SPServices/jquery.SPServices-2014.02.js" type="text/javascript"></script>
    <script type="text/javascript">
    
    $(function() {
      $.ajax({
        url: "/prgm/provost/CampaignPlan/_api/web/SiteUsers",
        type: "GET",
        headers: { "accept": "application/json;odata=verbose" },
    	success:(function(data) { 
    	
    		alert(data.d.Title + " : " + data.d.Created); 
    		var listItemInfo = "";
    		$.each(data.d.results, function (key, value) { listItemInfo += "<strong>Title: </strong>" + value.Title + " <strong>Description: </strong>" + value.Email + "<br />";	});
    		
    		}),
    		
    		$("#listItems").html(listItemInfo);
      });
    });
    </script>

    Thanks!

    Tom


    Tom Molskow - Senior SharePoint Architect - Microsoft Community Contributor 2011 and 2012 Award - Linked-In - SharePoint Gypsy

    Wednesday, April 19, 2017 5:15 PM

Answers

  • I see only mistake in your code is the URL in the request should be an absolute not a relative. Example as follows

    <<site url>>/prgm/provost/CampaignPlan/_api/web/SiteUsers

    It will return all users in a site collection.

    If you are looking for only users in a web you should use following url

    <<site url>>/prgm/provost/CampaignPlan/_api/web/RoleAssignments

    You can update the error message if you are getting error after using absolute url.

    Thursday, April 20, 2017 3:37 AM

All replies

  • Hi,

    Could you add the error function so it would easy to debug, I test the code and seems fine.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script type="text/javascript">
            $(function() {
                $.ajax({
                    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/SiteUsers",
                    type: "GET",
                    headers: { "accept": "application/json;odata=verbose" },
                    success: (function (data, textStatus, jqXHR) {
                        alert(data.d.Title + " : " + data.d.Created);
                        var listItemInfo = "";
                        $.each(data.d.results, function (key, value) {
                            listItemInfo += "<strong>Title: </strong>" + value.Title + " <strong>Description: </strong>" + value.Email + "<br />";
                        });
                        $("#listItems").html(listItemInfo);
    
                    }),
                    error: (function (xhr, ajaxOptions, thrownError) {
                        alert('Request failed: ' + xhr.status + '\n' + thrownError + '\n' + xhr.responseText);
                    })
            });
            });
        </script>

    Best Regards,

    Lee


    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 20, 2017 3:28 AM
  • I see only mistake in your code is the URL in the request should be an absolute not a relative. Example as follows

    <<site url>>/prgm/provost/CampaignPlan/_api/web/SiteUsers

    It will return all users in a site collection.

    If you are looking for only users in a web you should use following url

    <<site url>>/prgm/provost/CampaignPlan/_api/web/RoleAssignments

    You can update the error message if you are getting error after using absolute url.

    Thursday, April 20, 2017 3:37 AM
  • Hi,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    You can mark the helpful post as answer to help other community members find the helpful information quickly.

    You can also share your own solution here and mark it as answer, and we can learn from each other.

    Best Regards,

    Lee


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

    Tuesday, April 25, 2017 5:21 AM