public void CreateFolder(string siteUrl, string relativePath) { if (siteUrl != _siteUrl) { _siteUrl = siteUrl; Uri spSite = new Uri(siteUrl); _spo = SpoAuthUtility.Create(spSite, _username, WebUtility.HtmlEncode(_password), false); } string odataQuery = "_api/web/folders"; byte[] content = ASCIIEncoding.ASCII.GetBytes(@"{ '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '" + relativePath + "'}"); string digest = _spo.GetRequestDigest(); Uri url = new Uri(String.Format("{0}/{1}", _spo.SiteUrl, odataQuery)); // Set X-RequestDigest var webRequest = (HttpWebRequest)HttpWebRequest.Create(url); webRequest.Headers.Add("X-RequestDigest", digest); // Send a json odata request to SPO rest services to fetch all list items for the list. byte[] result = HttpHelper.SendODataJsonRequest( url, "POST", // reading data from SP through the rest api usually uses the GET verb content, webRequest, _spo // pass in the helper object that allows us to make authenticated calls to SPO rest services ); string response = Encoding.UTF8.GetString(result, 0, result.Length); }
/// <summary> /// Sends a JSON OData request appending SPO auth cookies to the request header. /// </summary> /// <param name="uri">The request uri</param> /// <param name="method">The http method</param> /// <param name="requestContent">A stream containing the request content</param> /// <param name="clientHandler">The request client handler</param> /// <param name="authUtility">An instance of the auth helper to perform authenticated calls to SPO</param> /// <param name="headers">The http headers to append to the request</param> public static byte[] SendODataJsonRequest(Uri uri, String method, byte[] requestContent, HttpWebRequest clientHandler, SpoAuthUtility authUtility, Dictionary<string, string> headers = null) { if (clientHandler.CookieContainer == null) clientHandler.CookieContainer = new CookieContainer(); CookieContainer cookieContainer = authUtility.GetCookieContainer(); // get the auth cookies from SPO after authenticating with Microsoft Online Services STS foreach (Cookie c in cookieContainer.GetCookies(uri)) { clientHandler.CookieContainer.Add(uri, c); // apppend SPO auth cookies to the request } return SendHttpRequest( uri, method, requestContent, "application/json;odata=verbose;charset=utf-8", // the http content type for the JSON flavor of SP REST services clientHandler, headers); }
static void Main(string[] args) { try { //Initialize the SPoAuthUtility spSite = new Uri(sharepointURL); _spo = SpoAuthUtility.Create(spSite, sharepointUsername, WebUtility.HtmlEncode(sharepointPassword), false); if (_spo != null) { string foldername = ""; string digest = _spo.GetRequestDigest(); Console.WriteLine("Create Folder: 1."); Console.WriteLine("Rename Folder: 2."); Console.WriteLine("Find Folder: 3. "); Console.WriteLine("Exit: any key"); string choice = Console.ReadLine(); if (choice == "1") { Console.WriteLine("Please provide a name for the folder and press enter!"); foldername = Console.ReadLine(); CreateFolder(foldername); } else if (choice == "2") { Console.WriteLine("Please enter the old folder name and hit enter!"); string oldfolder = Console.ReadLine(); Console.WriteLine("Please enter the new folder name and hit enter!"); string newfolder = Console.ReadLine(); RenameFolder(oldfolder, newfolder); } else if (choice == "3") { Console.WriteLine("Please enter the folder name and hit enter!"); string foldertofind = Console.ReadLine(); Console.WriteLine(FolderExists(foldertofind).ToString()); } else { return; } } else { Console.WriteLine("Could not authenticate user! Press any key to exit the application"); } Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } // End of main function
/// <summary> /// Sends a JSON OData request appending SPO auth cookies to the request header. /// </summary> /// <param name="uri">The request uri</param> /// <param name="method">The http method</param> /// <param name="requestContent">A stream containing the request content</param> /// <param name="clientHandler">The request client handler</param> /// <param name="authUtility">An instance of the auth helper to perform authenticated calls to SPO</param> /// <param name="headers">The http headers to append to the request</param> public static byte[] SendODataJsonRequest(Uri uri, String method, byte[] requestContent, HttpWebRequest clientHandler, SpoAuthUtility authUtility, Dictionary <string, string> headers = null) { if (clientHandler.CookieContainer == null) { clientHandler.CookieContainer = new CookieContainer(); } CookieContainer cookieContainer = authUtility.GetCookieContainer(); // get the auth cookies from SPO after authenticating with Microsoft Online Services STS foreach (Cookie c in cookieContainer.GetCookies(uri)) { clientHandler.CookieContainer.Add(uri, c); // apppend SPO auth cookies to the request } return(SendHttpRequest( uri, method, requestContent, "application/json;odata=verbose;charset=utf-8", // the http content type for the JSON flavor of SP REST services clientHandler, headers)); }