Wednesday, September 30, 2015

Upload file on sharepoint 2013 using SharePoint REST API

Code to upload file on SharePoint using SharePoint REST API

        public int FileUploadToSharePointREST(string filePath, string libraryName, string SharepointUrl)
        {
            int posted = -1;
            string errorMessage = string.Format("Sharepoint upload failed.. for File {0} on {1}", filePath, SharepointUrl);
            try
            {
               // string libraryName = "Launch Pad Files UAT";
                byte[] binary = System.IO.File.ReadAllBytes(filePath); ;
                string fname = System.IO.Path.GetFileName(filePath);
                string result = string.Empty;
                string resourceUrl = SharepointUrl + "/_api/web/lists/getbytitle('" + libraryName + "')/RootFolder/files/add(url='" + fname + "',overwrite=true)";
                HttpWebRequest wreq = HttpWebRequest.Create(resourceUrl) as HttpWebRequest;
                wreq.UseDefaultCredentials = true;
                string formDigest = GetFormDigest(SharepointUrl);
                wreq.Headers.Add("X-RequestDigest", formDigest);
                wreq.Method = "POST";
                wreq.Timeout = 1000000;
                wreq.Accept = "application/json; odata=verbose";
                wreq.ContentLength = binary.Length;
                using (System.IO.Stream requestStream = wreq.GetRequestStream())
                {
                    requestStream.Write(binary, 0, binary.Length);
                }
                HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
                if (wresp.StatusDescription == "OK")
                {
                    posted = 0;
                }
                else
                {
                    Console.Write(errorMessage);
                    Console.Write("Upload operation failed for " + filePath + ".\r\n" + "Error : " + wresp.StatusDescription);
                }              
                return posted;
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.ToString()+":"+ex.StackTrace);
                log.WriteLog(errorMessage);
                return posted;
            }
        }
        public string GetFormDigest(string SharepointUrl)
        {
            string formDigest = null;
            string resourceUrl = SharepointUrl + "/_api/contextinfo";
            HttpWebRequest wreq = HttpWebRequest.Create(resourceUrl) as HttpWebRequest;
            wreq.UseDefaultCredentials = true;
            wreq.Method = "POST";
            wreq.Accept = "application/json;odata=verbose";
            wreq.ContentLength = 0;
            wreq.ContentType = "application/json";
            string result;
            WebResponse wresp = wreq.GetResponse();
            using (System.IO.StreamReader sr = new System.IO.StreamReader(wresp.GetResponseStream()))
            {
                result = sr.ReadToEnd();
            }
            var jss = new JavaScriptSerializer();
            var d = jss.Deserialize<dynamic>(result);
            formDigest = d["d"]["GetContextWebInformation"]["FormDigestValue"];
            return formDigest;
        }

No comments: