Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

SharePoint 2013 - Updating property info for a file

$
0
0

Hi All,

I am trying to update "Title" for a file in "Shared Documents" libray but am getting Forbidden access error(403). Below is the code

            string uri = "https://mnp.sharepoint.com/_api/web/getfilebyserverrelativeurl('/Shared Documents/test.txt')"
            string userName = "abc@mnp.onmicrosoft.com";
            string password = "abc123";

            string title = "{'Title': 'Testing'}";
            byte[] binary = Encoding.UTF8.GetBytes(title);

            Uri weburi = new Uri(uri);

            var securePassword = new SecureString();
            foreach (var c in password) { securePassword.AppendChar(c); }

            var credentials = new SharePointOnlineCredentials(userName, securePassword);
            var authCookie = credentials.GetAuthenticationCookie(weburi);
            var cookieContainer = new CookieContainer();
            cookieContainer.SetCookies(weburi, authCookie);

            string formdigest = this.GetFormDigest(uri, credentials);

            HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(weburi);
            getRequest.Accept = "application/json;odata=verbose";
            getRequest.Method = "POST";
            getRequest.ContentLength = binary.Length;
            getRequest.CookieContainer = cookieContainer;
            getRequest.Credentials = credentials;
            getRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

            getRequest.Headers.Add("X-RequestDigest", formdigest);

            Stream st = getRequest.GetRequestStream();
            st.Write(binary, 0, binary.Length);


            HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();

        private string GetFormDigest(string siteurl, SharePointOnlineCredentials credentials)
        {
            Uri weburi = new Uri("https://mnp.sharepoint.com/_api/contextinfo");
            string newFormDigest = string.Empty;

            var authCookie = credentials.GetAuthenticationCookie(weburi);
            var cookieContainer = new CookieContainer();
            cookieContainer.SetCookies(weburi, authCookie);

            HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(weburi);
            webrequest.Accept = "application/json;odata=verbose";
            webrequest.Method = "POST";
            webrequest.ContentLength = 0;
            webrequest.Credentials = credentials;
            webrequest.CookieContainer = cookieContainer;
            webrequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

            HttpWebResponse webrequestResponse = (HttpWebResponse)webrequest.GetResponse();

            WebResponse webResp = webrequest.GetResponse();
            Stream webStream = webResp.GetResponseStream();
            StreamReader responseReader = new StreamReader(webStream);
            string response = responseReader.ReadToEnd();
            var j = JObject.Parse(response);
            var jObj = (JObject)JsonConvert.DeserializeObject(response);

            foreach (var item in jObj["d"].Children())
            {
                newFormDigest = item.First["FormDigestValue"].ToString();
            }

            responseReader.Close();

            return newFormDigest;
        }


Ramakrishna




Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>