public static SpoAuthUtility Create(Uri spSiteUrl, string username, string password, bool useIntegratedWindowsAuth) { var utility = new SpoAuthUtility(spSiteUrl, username, password, useIntegratedWindowsAuth); CookieContainer cc = utility.GetCookieContainer(); var cookies = from Cookie c in cc.GetCookies(spSiteUrl) where c.Name == "FedAuth" select c; if (cookies.Count() > 0) { //current = utility; return(utility); } else { throw new Exception("Could not retrieve Auth cookies"); } }
internal static bool CreateSubsite(IOrganizationService service, string siteName, string urlString) { var siteUrl = urlString; //@"sharepointUrl"; var _username = @"crmuserName"; var _password = "******"; var spSite = new Uri(siteUrl); var restURL = siteUrl + "/_api/web/webs/add"; var stringData = string.Concat("{'parameters': { '__metadata': { 'type': 'SP.WebCreationInformation' },", "'Title': '" + siteName + "', 'Url': '" + siteName + "', 'WebTemplate': 'siteTemplate', 'UseSamePermissionsAsParentSite': true } }"); var endpointRequest = (HttpWebRequest)HttpWebRequest.Create(restURL); var spo = SpoAuthUtility.Create(spSite, _username, WebUtility.HtmlEncode(_password), false); var cookies = spo.GetCookieContainer(); var formDigest = spo.GetRequestDigest(); endpointRequest.CookieContainer = cookies; endpointRequest.Method = "POST"; endpointRequest.ContentLength = stringData.Length; endpointRequest.Accept = "application/json;odata=verbose"; endpointRequest.ContentType = "application/json;odata=verbose"; endpointRequest.Headers.Add("X-RequestDigest", formDigest); var writer = new StreamWriter(endpointRequest.GetRequestStream()); writer.Write(stringData); writer.Flush(); var endpointResponse = (HttpWebResponse)endpointRequest.GetResponse(); try { var webResponse = endpointRequest.GetResponse(); var webStream = webResponse.GetResponseStream(); var responseReader = new StreamReader(webStream); var response = responseReader.ReadToEnd(); var jobj = JObject.Parse(response); responseReader.Close(); return(true); } catch (Exception e) { throw new InvalidPluginExecutionException("Inside create SHarpoint Plugin " + e.Message); } }
/// <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(); } var 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)); }