Friday, June 15, 2018

Converting JSON string into valid Class objects in C#

Sources:   http://json2csharp.com/


 public void GetCustomers()
        {
            WebRequest request = WebRequest.Create("http://northwind.servicestack.net/customers?format=json");
            request.Method = "GET";
            using (var response = request.GetResponse())
            {
                Stream responseStream = response.GetResponseStream();
                using (var reader = new StreamReader(responseStream))
                {
                    // get the response as text
                    string responseText = reader.ReadToEnd();

                    // convert from text
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    RootObject cust = ser.Deserialize<RootObject>(responseText);
               
                }
            }
        }
     
     
    }

    public class Customer
    {
        public string Id { get; set; }
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string ContactTitle { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }
        public string Phone { get; set; }
        public string Fax { get; set; }
     
    }
    public class RootObject
    {
        public List<Customer> Customers { get; set; }
    }

Thursday, June 7, 2018

Download Documents from Doc library from SharePoint Online by CSOM

Download Documents from Doc library in SharePoint Online by CSOM

 public void DownloadFiles()
        {
            var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

            using (var ctx = spContext.CreateUserClientContextForSPHost())
            {
                Web web = ctx.Web;
                Microsoft.SharePoint.Client.List list = web.Lists.GetByTitle("TestDocLibrary");
                CamlQuery query = new CamlQuery();
                Microsoft.SharePoint.Client.ListItemCollection items = list.GetItems(query);
                ctx.Load(items);
                ctx.ExecuteQuery();
                foreach (Microsoft.SharePoint.Client.ListItem listItem in items)
                {
                    ctx.Load(listItem, i => i.File);
                    ctx.ExecuteQuery();
                    var fileRef = listItem.File.ServerRelativeUrl;
                    // var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, fileRef); // this function not working
                 
                    Microsoft.SharePoint.Client.File file = listItem.File;
                    ClientResult<Stream> data = file.OpenBinaryStream();
                    ctx.Load(file);
                    ctx.ExecuteQuery();

                    int position = 1;
                    int bufferSize = 200000;

                    Byte[] readBuffer = new Byte[bufferSize];
                    string localFilePath = "C:\\Temp\\"+ file.Name;


                      using (System.IO.Stream stream = System.IO.File.Create(localFilePath))
                       {
                           while (position > 0)
                           {
                               // data.Value holds the Stream
                               position = data.Value.Read(readBuffer, 0, bufferSize);
                               stream.Write(readBuffer, 0, position);
                               readBuffer = new Byte[bufferSize];
                           }
                           stream.Flush();
                       }
                  }
             
            }


        }