/// <summary>
        /// Deletes the selected item or folder from the ListBox.
        /// </summary>
        /// <returns>A Boolean value that indicates whether the file or folder was successfully deleted.</returns>
        internal async Task <bool?> DeleteFileOrFolderAsync(FileSystemItemViewModel _selectedFileObject)
        {
            bool?isSuccess = false;

            try
            {
                // Gets the FileSystemItem that is selected in the bound ListBox control.
                IItem fileOrFolderToDelete = _selectedFileObject.FileSystemItem;

                // This results in a call to the service.
                await fileOrFolderToDelete.DeleteAsync();

                isSuccess = true;
            }
            catch (Microsoft.Data.OData.ODataErrorException)
            {
                isSuccess = null;
            }
            catch (NullReferenceException)
            {
                isSuccess = null;
            }

            return(isSuccess);
        }
Exemple #2
0
        //<summary>
        //Creates a new file named demo.txt in the default document library.
        //</summary>
        //<returns>A Boolean value that indicates whether the new text file was successfully created.</returns>
        internal async Task <String> CreateNewTextFileAsync()
        {
            //bool isSuccess = false;
            String newID            = string.Empty;
            var    sharePointClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles");

            try
            {
                // First check whether demo.txt already exists. If it exists, delete it.
                // If it doesn't exist, swallow the error.
                IItem item = await sharePointClient.Files.GetByPathAsync("demo.txt");

                await item.DeleteAsync();
            }
            catch (ODataErrorException)
            {
                // fail silently because demo.txt doesn't exist.
            }

            try
            {
                // In this example, we'll create a simple text file and write the current timestamp into it.
                string createdTime = "Created at " + DateTime.Now.ToLocalTime().ToString();
                byte[] bytes       = Encoding.UTF8.GetBytes(createdTime);

                using (MemoryStream stream = new MemoryStream(bytes))
                {
                    // File is called demo.txt. If it already exists, we'll get an exception.
                    Microsoft.Office365.SharePoint.FileServices.File newFile = new Microsoft.Office365.SharePoint.FileServices.File
                    {
                        Name = "demo.txt"
                    };

                    // Create the empty file.
                    await sharePointClient.Files.AddItemAsync(newFile);

                    newID = newFile.Id;

                    // Upload the file contents.
                    await sharePointClient.Files.GetById(newFile.Id).ToFile().UploadAsync(stream);
                }
            }

            // ODataErrorException can be thrown when you try to create a file that already exists.
            catch (Microsoft.Data.OData.ODataErrorException)
            {
                //isSuccess = false;
            }

            return(newID);
        }