Created
February 1, 2017 09:48
-
-
Save amit12joshi/f748ad4ebfdb379f6d54676d29177763 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Xml; | |
using Microsoft.SharePoint.Client; | |
using System.Security; | |
using System.IO; | |
using System.Web.Script.Serialization; | |
namespace RestCallConsoleApp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
SharePointOnlineCredentials cred = EnterCredentails(); | |
UpdateListItem(cred); | |
} | |
catch (Exception ex) | |
{ | |
} | |
} | |
public static string GetFormDigest(SharePointOnlineCredentials cred) | |
{ | |
string formDigest = null; | |
string resourceUrl = "https://personal344.sharepoint.com/_api/contextinfo"; | |
HttpWebRequest wreq = HttpWebRequest.Create(resourceUrl) as HttpWebRequest; | |
wreq.Credentials = cred; | |
wreq.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; | |
wreq.Method = "POST"; | |
wreq.Accept = "application/json;odata=verbose"; | |
wreq.ContentLength = 0; | |
wreq.ContentType = "application/json"; | |
wreq.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); | |
string result; | |
WebResponse wresp = wreq.GetResponse(); | |
using (StreamReader sr = new StreamReader(wresp.GetResponseStream())) | |
{ | |
result = sr.ReadToEnd(); | |
} | |
var jss = new JavaScriptSerializer(); | |
var val = jss.Deserialize<Dictionary<string, object>>(result); | |
var d = val["d"] as Dictionary<string, object>; | |
var wi = d["GetContextWebInformation"] as Dictionary<string, object>; | |
formDigest = wi["FormDigestValue"].ToString(); | |
return formDigest; | |
} | |
public static void UpdateListItem(SharePointOnlineCredentials cred) | |
{ | |
string result = string.Empty; | |
Uri uri = new Uri("https://personal344.sharepoint.com/_api/web/lists/getbytitle('Test')/items(1)"); | |
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(uri); | |
wreq.Credentials = cred; | |
string formDigest = GetFormDigest(cred); | |
wreq.Method = "POST"; | |
wreq.Accept = "application/json; odata=verbose"; | |
wreq.ContentType = "application/json; odata=verbose"; | |
wreq.Headers.Add("X-HTTP-Method", "MERGE"); | |
wreq.Headers.Add("IF-MATCH", "*"); | |
wreq.Headers.Add("X-RequestDigest", formDigest); | |
wreq.Headers.Add("Authorization", "BEARER" + formDigest); | |
string stringData = "{'__metadata': { 'type': 'SP.Data.TestListItem' }, 'Title': 'I am updated Title now'}"; | |
wreq.ContentLength = stringData.Length; | |
StreamWriter writer = new StreamWriter(wreq.GetRequestStream()); | |
writer.Write(stringData); | |
writer.Flush(); | |
WebResponse wresp = wreq.GetResponse(); | |
using (StreamReader sr = new StreamReader(wresp.GetResponseStream())) | |
{ | |
result = sr.ReadToEnd(); | |
} | |
} | |
public static SharePointOnlineCredentials EnterCredentails() | |
{ | |
ConsoleColor defaultForeground = Console.ForegroundColor; | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine("Enter your loginid:"); | |
Console.ForegroundColor = defaultForeground; | |
string userName = Console.ReadLine(); | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine("Enter your password."); | |
Console.ForegroundColor = defaultForeground; | |
SecureString password = GetPassword(); | |
SharePointOnlineCredentials cred = new SharePointOnlineCredentials(userName, password); | |
return cred; | |
} | |
private static SecureString GetPassword() | |
{ | |
ConsoleKeyInfo info; | |
//Get the user's password as a SecureString | |
SecureString securePassword = new SecureString(); | |
do | |
{ | |
info = Console.ReadKey(true); | |
if (info.Key != ConsoleKey.Enter) | |
{ | |
securePassword.AppendChar(info.KeyChar); | |
} | |
} | |
while (info.Key != ConsoleKey.Enter); | |
return securePassword; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment