0

i am getting this exception of "Thread was being aborted " on library.Update();

my code snippet :

                      var fileName = Path.GetFileName(file.FileName);
            var byteArray = ReadFully(file.InputStream);
            if (byteArray != null)
            {
                var site = SPContext.Current.Site;
                using (var web = site.RootWeb)
                {
                    var imagesFolderUrl = web.Url + "/" + library.RootFolder.Url + "/" + folderName;
                    if (!web.GetFolder(imagesFolderUrl).Exists)
                    {
                        web.AllowUnsafeUpdates = true;
                        var folderItem = library.AddItem(library.RootFolder.ServerRelativeUrl,
                            SPFileSystemObjectType.Folder, folderName);
                        folderItem.Update();
                        library.RootFolder.Update();
                        library.Update();
                        web.AllowUnsafeUpdates = false;
                    }
                    var subFolderUrl = web.Url + "/" + library.RootFolder.Url + "/" + folderName;
                    SPFile imagefile = web.GetFile(subFolderUrl + "/" + file.FileName);
                    if (imagefile.Exists && !isOverwrite)
                    {
                        filePath = "Overwrite: An image by the name " + fileName +
                                   " already exists, would you like to overwrite it?";
                    }
                    else
                    {
                        web.AllowUnsafeUpdates = true;
                        SPFolder folder = web.GetFolder(subFolderUrl);
                        var spFile = folder.Files.Add(fileName, byteArray, true);
                        filePath = web.Url + "/" + spFile.Url;
                        folder.Update();
                        library.Update();
                        web.AllowUnsafeUpdates = false;
                    }

                }
            }

Can any body get me out of this?

1 Answer 1

0

The folder is fetched from its URL so instead of updating the library which contains that folder directly update that folder only

web.AllowUnsafeUpdates = true;
SPFolder folder = web.GetFolder(subFolderUrl);
var spFile = folder.Files.Add(fileName, byteArray, true);
filePath = web.Url + "/" + spFile.Url;
folder.Update();
web.AllowUnsafeUpdates = false;

Updated:

You are trying to create one folder in the document library at root level. After creating a folder, adding a file into that folder.

Below code snippet creates a folder and then create a file into that folder.

        SPWeb web = SPContext.Current.Web;
        SPDocumentLibrary library = web.Lists.TryGetList("Documents") as SPDocumentLibrary;

        web.AllowUnsafeUpdates = true;

        if (!web.GetFolder(library.RootFolder.ServerRelativeUrl + "/Test").Exists)
        {
            SPFolder folder = library.RootFolder.SubFolders.Add("Test");

            byte[] bytes = System.Text.Encoding.Unicode.GetBytes("Hello World.");

            if (!web.GetFile(folder.ServerRelativeUrl + "/FileTest.txt").Exists)
            {
                var spFile = folder.Files.Add("FileTest.txt", bytes, true);                
            }
        }
        web.AllowUnsafeUpdates = false;
3
  • Could you explain your answer?
    – Waqas Sarwar MVP
    May 1, 2017 at 15:18
  • Thanks Divya, but i am still getting the same error i have updated my code above. May 2, 2017 at 4:48
  • 1
    @WaqasSarwarMVP, The folder is fetched from its URL so instead of updating the library which contains that folder directly update that folder only. May 2, 2017 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.