简体   繁体   中英

Pulling data from a list in SharePoint 2013 via REST

I'm trying to build a mobile service in Azure (C#) that pushes data onto client apps. The data I need to pull is from a sharepoint site which I'm not very familiar with nor do I have access to.

I've read a couple of resources online but whenever I try to quickly visit the list via _api/web/lists/ or _api/web/lists/getbytitle('pages') , I get an authentication prompt.

Is this the default behavior or hsa the rest api been switched off? Do I need to build a sharepoint app to generate an oauth token and then access the list?

I'm basically clueless so any piece of information would be greatly appreciated.

use the following URLs:

  • Get all lists http://servername/_api/web1/web2/lists/
  • Get list by title http://servername/_api/web1/web2/lists/GetByTitle('myList')
  • Get all items in specific list http://servername/_api/web1/web2/lists/GetByTitle('myList')/items
  • list item by item ID http://servername/_api/web1/web2/lists/GetByTitle('myList')/items/GetItemById(4)
  • Return items with specific fields http://servername/_api/web1/web2/lists/GetByTitle('myList')/items?$select=Title,Age

For more detail, please visit the link http://wmostafaw.wordpress.com/2012/11/17/odata-in-sharepoint-2013-lists-and-items/

That is default behavior. You will need credentials to access the REST Services and you have create a Digest to do this programmatically:

String retVal = "";
    try
    {
         string url = "https://YourSite.com/";
         HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
          client.BaseAddress = new System.Uri(url);
          string cmd = "_api/contextinfo";
          client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
          client.DefaultRequestHeaders.Add("ContentType", "application/json");
          client.DefaultRequestHeaders.Add("ContentLength", "0");
          StringContent httpContent = new StringContent("");
          var response = client.PostAsync(cmd, httpContent).Result;
          if (response.IsSuccessStatusCode)
          {
              string content = response.Content.ReadAsStringAsync().Result;
               JsonObject val = JsonValue.Parse(content).GetObject();
               JsonObject d = val.GetNamedObject("d");
               JsonObject wi = d.GetNamedObject("GetContextWebInformation");
               retVal = wi.GetNamedString("FormDigestValue");
           }
     }
     catch
     { }
return retVal;

If you have an account that can access the list in SharePoint then you should be able to use that same account to authenticate against the REST Service and retrieve the list items once you pass it the Digest. If you get one I have a lot of code examples on my blog for this type of application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM