Monday 20 August 2012

SharePoint 2013 - Retrieve List Items using REST


A simple example of how to Retrieve List Items using REST that will be useful while creating a SharePoint apps.
 




//This method retrieves all items from a specified list.
  private void RetrieveListItems(Guid listId)
  {
            TokenHelper.TrustAllCertificates();

            string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request);

                contextToken =
                    TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);

                sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);

                accessToken =
                    TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken;

              if (IsPostBack)
            {
                sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);
            }

              //Add pertinent namespaces to the namespace manager.
            xmlnspm.AddNamespace("atom", "http://www.w3.org/2005/Atom");
            xmlnspm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
            xmlnspm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");

            //Execute a REST request to get the list name.
            HttpWebRequest listRequest =
                (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "/_api/Web/lists(guid'" + listId + "')");
            listRequest.Method = "GET";
            listRequest.Accept = "application/atom+xml";
            listRequest.ContentType = "application/atom+xml;type=entry";
            listRequest.Headers.Add("Authorization", "Bearer " + accessToken);
            HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse();
            StreamReader listReader = new StreamReader(listResponse.GetResponseStream());
            var listXml = new XmlDocument();
            listXml.LoadXml(listReader.ReadToEnd());

            var listNameNode = listXml.SelectSingleNode("//atom:entry/atom:content/m:properties/d:Title", xmlnspm);
            string listName = listNameNode.InnerXml;

             //Execute a REST request to get all of the list's items.

            HttpWebRequest itemRequest =
                (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "/_api/Web/lists(guid'" + listId + "')/Items");
            itemRequest.Method = "GET";
            itemRequest.Accept = "application/atom+xml";
            itemRequest.ContentType = "application/atom+xml;type=entry";
            itemRequest.Headers.Add("Authorization", "Bearer " + accessToken);
            HttpWebResponse itemResponse = (HttpWebResponse)itemRequest.GetResponse();
            StreamReader itemReader = new StreamReader(itemResponse.GetResponseStream());
            var itemXml = new XmlDocument();
            itemXml.LoadXml(itemReader.ReadToEnd());

            var itemList = itemXml.SelectNodes("//atom:entry/atom:content/m:properties/d:Title", xmlnspm);

            TableRow tableRow = new TableRow();
            TableCell tableCell1 = new TableCell();
            tableCell1.Controls.Add(new LiteralControl(listName));
            TableCell tableCell2 = new TableCell();

            foreach (XmlNode itemTitle in itemList)
            {
               // itemTitle.InnerXml + "<br>";
            }
 
        }

Ads by Google

No comments:

Post a Comment