1

I am looking for the SharePoint OnLine RESTFul API try to get all lists of specified Site.

Let's say the default Site of Site Collection in my Office 365 SharePoint OnLine trial. The Site Collection is https://beyondhard.sharepoint.com/.

I just found a sample code for the managed Server Class Libary for SharePoint 2013.

 using (SPSite site = new SPSite("http://localhost/sites/sitecollection"))
 {
    using (SPWeb web = site.OpenWeb("sitecollection/subsite"))
    {
       string listUrl = "/sites/sitecollection/subsite/Lists/Announcements";
       SPList list = web.GetList(listUrl);
       Console.WriteLine("List URL: {0}", list.RootFolder.ServerRelativeUrl);
    }
 }

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getlist.aspx

Is there any RESTFul for it? If could give me some code sample. It will be nice. Thanks.

3 Answers 3

2

Since you are looking for a C# example, the below example demonstrates how to retrieve all lists via WebClient (you could also utilize HttpClient or HttpWebRequest for that purpose)

Endpoint: /_api/web/lists

How to retrieve lists using SharePoint REST

public static JToken GetLists(Uri webUri,ICredentials credentials)
{
    using (var client = new WebClient())
    {
        client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
        client.Credentials = credentials;
        client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
        client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose");
        var endpointUri = new Uri(webUri, "/_api/web/lists/");
        var result = client.DownloadString(endpointUri);
        var t = JToken.Parse(result);
        return t["d"]["results"];
    }
}

Dependency: Json.NET library

Usage

The example demonstrates how to print lists titles in SPO site:

static void Main(string[] args)
{
      var webUri = new Uri("https://contoso.sharepoint.com");
      const string userName = "[email protected]";
      const string password = "password";
      var securePassword = new SecureString();
      foreach (var c in password)
      {
          securePassword.AppendChar(c);
      }
      var credentials = new SharePointOnlineCredentials(userName, securePassword);

      var lists = GetLists(webUri,credentials);
      //print List title
      foreach (var list in lists)
      {
            Console.WriteLine(list["Title"]);
      }
}
4

Yep you can get this from /_api/web/lists. You can try it out in a browser using https://beyondhard.sharepoint.com/_api/web/lists. There are loads of code samples on the internet but best place to start is MSDN

2
  • +1 Right , I can see the XML content in my browser. But Why all these MSDN samples are Javascript ? Is there any C# version for them ? Thanks.
    – Joe.wang
    Sep 3, 2014 at 8:56
  • MS is moving towards the App model. I think if you are not using JavaScript in SharePoint now you will be fairly soon. There are still plenty of REST samples for C# about but you might want to decide why you would use REST over CSOM.
    – Bunzab
    Sep 3, 2014 at 9:27
1

I upvoted Bunzab's answer, but here is an MSDN example of exactly what you're asking about (in C#):

HttpWebRequest endpointRequest =
  (HttpWebRequest)HttpWebRequest.Create("http://<site url>/_api/web/lists");
endpointRequest.Method = "GET";
endpointRequest.Accept = "application/json;odata=verbose";
// You don't need the following line if you're doing this from within an App Web
endpointRequest.Headers.Add("Authorization", "Bearer " + accessToken);
HttpWebResponse endpointResponse =
  (HttpWebResponse)endpointRequest.GetResponse();

Get your google going! =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.