Friday, November 15, 2013

Reading File contents in SharePoint programmatically

Line of code to perform reading the XML contents from the SharePoint file and converting the XML String to DataSet.
SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                   SPSite site = null;
                   SPWeb web = null;
                    site = new SPSite((SPContext.Current.Site.Url));
                   web = site.OpenWeb();
                   var file = web.GetFile("Shared%20Documents/temp.xml");
                  string fileUrl = SPContext.Current.Web.Url + "/Shared%20Documents/temp.xml";
                  byte[] binFile = file.OpenBinary();
                  Stream stream = new MemoryStream(binFile);
                  DataSet ds = new DataSet();
                  ds.ReadXml(stream);
                   spItems.DataSource = ds.Tables[0];
                   spItems.DataBind();
                });