示例#1
0
        private string SaveAttachment(Attachment attachment)
        {
            string oneDriveResourceId  = AppConstants.oneDriveResourceId;
            string oneDriveApiEndpoint = AppConstants.oneDriveApiEndpoint;

            string accessToken = OAuthController.GetAccessTokenFromRefreshToken(oneDriveResourceId);

            // Prepare the HTTP request using the new "File" APIs
            HttpWebRequest webRequest =
                WebRequest.CreateHttp(oneDriveApiEndpoint + "/files/Add(name='" + attachment.AttachmentName + "', overwrite=true)");

            webRequest.Accept = "application/json;odata=verbose";
            webRequest.Headers.Add("Authorization", string.Format("Bearer {0}", accessToken));
            webRequest.Method        = "POST";
            webRequest.ContentLength = attachment.AttachmentBytes.Length;
            webRequest.ContentType   = "application/octet-stream";

            Stream requestStream = webRequest.GetRequestStream();

            requestStream.Write(attachment.AttachmentBytes, 0, attachment.AttachmentBytes.Length);
            requestStream.Close();

            // Make the request to SharePoint and get the response.
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

            // If the response is okay, read it
            if (webResponse.StatusCode == HttpStatusCode.OK)
            {
                Stream       responseStream = webResponse.GetResponseStream();
                StreamReader reader         = new StreamReader(responseStream);
                return(reader.ReadToEnd());
            }

            return("StatusCode was not OK!");
        }
示例#2
0
        private string SaveAttachments(Attachment[] attachments, string userEmail)
        {
            // Get the user's OneDrive endpoint and access token
            OAuthController.OneDriveAccessDetails accessDetails =
                OAuthController.GetUsersOneDriveAccessDetails(userEmail);

            string createAttachmentUri = accessDetails.ApiEndpoint +
                                         "/Files/getByPath('{0}')/content?nameConflict=overwrite";

            string returnString = "";

            foreach (Attachment attachment in attachments)
            {
                // Prepare the HTTP request using the new "File" APIs
                HttpWebRequest webRequest =
                    WebRequest.CreateHttp(string.Format(createAttachmentUri, attachment.AttachmentName));
                webRequest.Accept = "application/json";
                webRequest.Headers.Add("Authorization", string.Format("Bearer {0}", accessDetails.AccessToken));
                webRequest.Method        = "PUT";
                webRequest.ContentLength = attachment.AttachmentBytes.Length;
                webRequest.ContentType   = "application/octet-stream";

                Stream requestStream = webRequest.GetRequestStream();
                requestStream.Write(attachment.AttachmentBytes, 0, attachment.AttachmentBytes.Length);
                requestStream.Close();

                // Make the request to SharePoint and get the response.
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

                if (!string.IsNullOrEmpty(returnString))
                {
                    returnString += "; ";
                }

                // If the response is okay, read it
                if (webResponse.StatusCode == HttpStatusCode.Created)
                {
                    Stream       responseStream = webResponse.GetResponseStream();
                    StreamReader reader         = new StreamReader(responseStream);

                    returnString += attachment.AttachmentName + ": Success, ID: " +
                                    GetAttachmentIdFromJson(reader.ReadToEnd());
                }
                else
                {
                    returnString += attachment.AttachmentName + ": Error: " + webResponse.StatusCode + Environment.NewLine;
                }
            }

            return(returnString);
        }