Why does the following script fail to create a folder just once randomly?

Puneeth, C 81 Reputation points
2022-08-23T13:19:47.733+00:00

I have a function that creates an empty folder when called. It usually works fine. But it only fails once throwing this error -

 Microsoft.SharePoint.SPException ---> System.Runtime.InteropServices.COMExceptio  
    n: <nativehr>0x80131904</nativehr><nativestack></nativestack>  
       at Microsoft.SharePoint.Library.SPRequestInternalClass.AddOrUpdateItem(String  
     bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPre  
    serveItemVersion, Boolean bPreserveItemUIVersion, Boolean bUpdateNoVersion, Int3  
    2& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bst  
    rVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& p  
    varProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bUnRestrictedUpdateI  
    nProgress, Boolean bMigration, Boolean bPublish, String bstrFileName, ISP2DSafeA  
    rrayWriter pListDataValidationCallback, ISP2DSafeArrayWriter pRestrictInsertCall  
    back, ISP2DSafeArrayWriter pUniqueFieldCallback)  
       at Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, Str  
    ing bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVers  
    ion, Boolean bPreserveItemUIVersion, Boolean bUpdateNoVersion, Int32& plID, Stri  
    ng& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Obj  
    ect& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties  
    , Boolean bCheckOut, Boolean bCheckin, Boolean bUnRestrictedUpdateInProgress, Bo  
    olean bMigration, Boolean bPublish, String bstrFileName, ISP2DSafeArrayWriter pL  
    istDataValidationCallback, ISP2DSafeArrayWriter pRestrictInsertCallback, ISP2DSa  
    feArrayWriter pUniqueFieldCallback)  
       --- End of inner exception stack trace ---  
       at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)  
       at Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, Str  
    ing bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVers  
    ion, Boolean bPreserveItemUIVersion, Boolean bUpdateNoVersion, Int32& plID, Stri  
    ng& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Obj  
    ect& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties  
    , Boolean bCheckOut, Boolean bCheckin, Boolean bUnRestrictedUpdateInProgress, Bo  
    olean bMigration, Boolean bPublish, String bstrFileName, ISP2DSafeArrayWriter pL  
    istDataValidationCallback, ISP2DSafeArrayWriter pRestrictInsertCallback, ISP2DSa  
    feArrayWriter pUniqueFieldCallback)  
       at Microsoft.SharePoint.SPListItem.AddOrUpdateItem(Boolean bAdd, Boolean bSys  
    tem, Boolean bPreserveItemVersion, Boolean bNoVersion, Boolean bMigration, Boole  
    an bPublish, Boolean bCheckOut, Boolean bCheckin, Guid newGuidOnAdd, Int32& ulID  
    , Object& objAttachmentNames, Object& objAttachmentContents, Boolean suppressAft  
    erEvents, String filename, Boolean bPreserveItemUIVersion)  
       at Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bP  
    reserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boo  
    lean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvent  
    s, String filename, Boolean bPreserveItemUIVersion)  
       at Microsoft.SharePoint.SPListItem.Update()  
       at MigrateSite.Program.AddDummyFolderInRoot(String siteUrl, String libraryNam  
    e, String folderName) in c:\Users\svcmmo13farm_dev\Source\Repos\MigrateSite\Migr  
    ateSite\Program.cs:line 784  

My code is as follows:

public static void AddDummyFolderInRoot(string siteUrl, string libraryName, string folderName)  
        {  
            using(SPSite site = new SPSite(siteUrl))  
            {  
                using(SPWeb web = site.OpenWeb())  
                {  
                    try  
                    {  
                        web.AllowUnsafeUpdates = true;  
                        web.Update();  
                        SPList lib = web.Lists.TryGetList(libraryName);  
                        SPListItem dummyFolder = lib.AddItem(lib.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderName);  
                        dummyFolder.Update();  
                        lib.Update();  
                        web.Update();  
                    }  
                    catch (Exception e)  
                    {  
                        Console.WriteLine("error:\n"+e);  
                        Console.ReadKey(true);  
                    }  
                }  
            }  
        }  

Could you help me out? Thanks

PS: I am working with Sharepoint 2013.

Microsoft 365 and Office | SharePoint | Development
{count} votes

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-08-24T03:50:40.577+00:00

    Hi @Puneeth, C
    Please check the ULS logs for more info(use the CorrelationID). Make sure that your Content Database in SQL is not full. Sharepoint was installed on MSSQL Express, which is limited to 4G.
    The account that you were using had access to the DB whilst another did not so sharepoint couldn't find the web application. Adding db_owner would give full access to the content database for anyone who is part of that group (like site collection admins).
    Here is the similar issue with yours for reference
    https://sharepoint.stackexchange.com/questions/110652/why-do-i-get-nativehr0x80131904-nativehr-when-connecting-to-a-site-url

    Since you are using SharePoint 2013. I will recommend you to use CSOM to create folder

    public SPFolder GetOrCreateFolder(ref SPWeb web, SPList sourceList, string listName, string folderName)  
    {  
        SPFolder folder = null;  
        if (sourceList == null)  
        {  
            folder = web.Folders[listName];  
            if (folder != null)  
            {  
                if(!folder.SubFolders[folderName].Exists)  
                    folder.SubFolders.Add(folderName);  
                else  
                    folder = folder.SubFolders[folderName];  
            }  
        }  
        return folder;  
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-09-05T08:57:46.197+00:00

    Hi @Puneeth, C
    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [Why does the following script fail to create a folder just once randomly?]

    Issue Symptom:
    It only fails once throwing this error

     Microsoft.SharePoint.SPException ---> System.Runtime.InteropServices.COMExceptio  
         n: <nativehr>0x80131904</nativehr><nativestack></nativestack>  
    

    Current status:
    Write a catch block to change the name of the folder if it fails to create any folder.
    It's looking fine now.

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.