public static bool GetAccessControlPrincipal(OAuth2Token token, string principalId, string folderitemid) { String uri = string.Format("https://{0}/sf/v3/Items({1})/AccessControls", ShareFileV3Sample.GetHostname(token), folderitemid, principalId); Console.WriteLine(uri); HttpWebRequest request = WebRequest.CreateHttp(uri); ShareFileV3Sample.addAuthorizationHeader(request, token); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(response.StatusCode); using (var reader = new StreamReader(response.GetResponseStream())) { String body = reader.ReadToEnd(); if (body.Contains(principalId)) { return(true); } else { return(false); } } }
/// <summary> /// Downloads a single Item. /// </summary> /// <param name="token">the OAuth2Token returned from Authenticate</param> /// <param name="itemId">the id of the item to download</param> /// <param name="localPath">where to download the item to, like "c:\\path\\to\\the.file". If downloading a folder the localPath name should end in .zip.</param> public static void DownloadItem(OAuth2Token token, string itemId, string localPath) { String uri = string.Format("https://{0}/sf/v3/Items({1})/Download", ShareFileV3Sample.GetHostname(token), itemId); Console.WriteLine(uri); HttpWebRequest request = WebRequest.CreateHttp(uri); ShareFileV3Sample.addAuthorizationHeader(request, token); request.AllowAutoRedirect = true; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (var source = new BufferedStream(response.GetResponseStream())) { using (var target = new FileStream(localPath, FileMode.Create)) { int chunkSize = 1024 * 8; byte[] chunk = new byte[chunkSize]; int len = 0; while ((len = source.Read(chunk, 0, chunkSize)) > 0) { target.Write(chunk, 0, len); } Console.WriteLine("Download complete"); } } Console.WriteLine(response.StatusCode); }
/// <summary> /// Uploads a File using the Standard upload method with a multipart/form mime encoded POST. /// </summary> /// <param name="token">the OAuth2Token returned from Authenticate</param> /// <param name="parentId">where to upload the file</param> /// <param name="localPath">the full path of the file to upload, like "c:\\path\\to\\file.name"</param> public static void UploadFile(OAuth2Token token, string parentId, string localPath) { String uri = string.Format("https://{0}/sf/v3/Items({1})/Upload", ShareFileV3Sample.GetHostname(token), parentId); Console.WriteLine(uri); HttpWebRequest request = WebRequest.CreateHttp(uri); ShareFileV3Sample.addAuthorizationHeader(request, token); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (var reader = new StreamReader(response.GetResponseStream())) { String body = reader.ReadToEnd(); JObject uploadConfig = JObject.Parse(body); string chunkUri = (string)uploadConfig["ChunkUri"]; if (chunkUri != null) { Console.WriteLine("Starting Upload"); UploadMultiPartFile("File1", new FileInfo(localPath), chunkUri); } } }
/// <summary> /// Update the name and description of an Item. /// </summary> /// <param name="token">the OAuth2Token returned from Authenticate</param> /// <param name="itemId">the id of the item to update</param> /// <param name="name">the item name</param> /// <param name="description">the item description</param> public static void UpdateItem(OAuth2Token token, string itemId, string name, string description) { String uri = string.Format("https://{0}/sf/v3/Items({1})", ShareFileV3Sample.GetHostname(token), itemId); Console.WriteLine(uri); HttpWebRequest request = WebRequest.CreateHttp(uri); ShareFileV3Sample.addAuthorizationHeader(request, token); Dictionary <string, object> folder = new Dictionary <string, object>(); folder.Add("Name", name); folder.Add("Description", description); string json = JsonConvert.SerializeObject(folder); Console.WriteLine(json); request.Method = "PATCH"; request.ContentType = "application/json"; using (var writer = new StreamWriter(request.GetRequestStream())) { writer.Write(json); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(response.StatusCode); using (var reader = new StreamReader(response.GetResponseStream())) { String body = reader.ReadToEnd(); JObject newFolder = JObject.Parse(body); Console.WriteLine("Updated Folder: " + newFolder["Id"]); } }
/// <summary> /// Get a folder using some of the common query parameters that are available. This will /// add the expand, select parameters. The following are used: /// expand=Children to get any Children of the folder /// select=Id,Name,Children/Id,Children/Name,Children/CreationDate to get the Id, Name of the folder and the Id, Name, CreationDate of any Children /// </summary> /// <param name="token">the OAuth2Token returned from Authenticate</param> /// <param name="id">a folder id</param> public static void GetFolderWithQueryParameters(OAuth2Token token, string id) { String uri = string.Format("https://{0}/sf/v3/Items({1})?$expand=Children&$select=Id,Name,Children/Id,Children/Name,Children/CreationDate", ShareFileV3Sample.GetHostname(token), id); Console.WriteLine(uri); HttpWebRequest request = WebRequest.CreateHttp(uri); ShareFileV3Sample.addAuthorizationHeader(request, token); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(response.StatusCode); using (var reader = new StreamReader(response.GetResponseStream())) { String body = reader.ReadToEnd(); JObject folder = JObject.Parse(body); // only Id and Name are available because we specifically selected only those two Properties Console.WriteLine(folder["Id"] + " " + folder["Name"]); JArray children = (JArray)folder["Children"]; if (children != null) { foreach (JObject child in children) { // CreationDate is also available on Children because we specifically selected that property in addition to Id, Name Console.WriteLine(child["Id"] + " " + child["CreationDate"] + " " + child["Name"]); } } } }
/// <summary> /// Get the root level Item for the provided user. To retrieve Children the $expand=Children /// parameter can be added. /// </summary> /// <param name="token">the OAuth2Token returned from Authenticate</param> /// <param name="getChildren">retrieve Children Items if true, default is false</param> public static void GetRoot(OAuth2Token token, bool getChildren = false) { String uri = string.Format("https://{0}/sf/v3/Items", ShareFileV3Sample.GetHostname(token)); if (getChildren) { uri += "?$expand=Children"; } Console.WriteLine(uri); HttpWebRequest request = WebRequest.CreateHttp(uri); ShareFileV3Sample.addAuthorizationHeader(request, token); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(response.StatusCode); using (var reader = new StreamReader(response.GetResponseStream())) { String body = reader.ReadToEnd(); JObject root = JObject.Parse(body); // just print Id, CreationDate, Name of each element Console.WriteLine(root["Id"] + " " + root["CreationDate"] + " " + root["Name"]); JArray children = (JArray)root["Children"]; if (children != null) { foreach (JObject child in children) { Console.WriteLine(child["Id"] + " " + child["CreationDate"] + " " + child["Name"]); } } } }
/// <summary> /// Delete an Item by Id. /// </summary> /// <param name="token">the OAuth2Token returned from Authenticate</param> /// <param name="itemId">the id of the item to delete</param> public static void DeleteItem(OAuth2Token token, string itemId) { String uri = string.Format("https://{0}/sf/v3/Items({1})", ShareFileV3Sample.GetHostname(token), itemId); Console.WriteLine(uri); HttpWebRequest request = WebRequest.CreateHttp(uri); ShareFileV3Sample.addAuthorizationHeader(request, token); request.Method = "DELETE"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(response.StatusCode); }
/// <summary> /// Get a single Item by Id. /// </summary> /// <param name="token">the OAuth2Token returned from Authenticate</param> /// <param name="id">an item id</param> public static void GetItemById(OAuth2Token token, string id) { String uri = string.Format("https://{0}/sf/v3/Items({1})", ShareFileV3Sample.GetHostname(token), id); Console.WriteLine(uri); HttpWebRequest request = WebRequest.CreateHttp(uri); ShareFileV3Sample.addAuthorizationHeader(request, token); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(response.StatusCode); using (var reader = new StreamReader(response.GetResponseStream())) { String body = reader.ReadToEnd(); JObject item = JObject.Parse(body); Console.WriteLine(item["Id"] + " " + item["CreationDate"] + " " + item["Name"]); } }