none
Creating a new folder in a SharePoint library using C# Windows Application.

    Question

  • I have tried to create a folder within a SharePoint document library. The coding is as follows.

    Text Box Name: txtNewFolderName

    Button Name : btnCreateNewFolder

    private void btnCreateNewFolder_Click(object sender, EventArgs e)
            {
                //String parameters for site URL and site name
                const string siteUrl = "http://thekingsbury/";
                const string siteName = "/SiteDirectory/thekingsbury/";
    
                //Freeze UI
                Cursor = Cursors.WaitCursor;
                btnCreateNewFolder.Enabled = false;
                
                //Use SPSite constructor to assign site collection based on top-level URL
                SPSite siteCollection = new SPSite(siteUrl);
    
                //Assign target site (SPWeb instance) based on site name
                SPWeb site = siteCollection.AllWebs[siteName];
    
                //Create an instance of SPDocumentLibrary based on the named doc library list
                SPDocumentLibrary docLibrary = (SPDocumentLibrary)site.Lists["Test Library"];
    
                //Create instance of SPFolderCollection and add a named folder based on forms input,
                //then update library to reflect added folder
                SPFolderCollection myFolders = site.Folders;
                myFolders.Add("http://thekingsbury/Test%20Library/" + txtNewFolderName.Text + "/");
                docLibrary.Update();
    
                //UI clean-up
                Cursor = Cursors.Default;
                btnCreateNewFolder.Enabled = true;
            }

    Site URL :

    http://thekingsbury/

    Site Name: thekingsbury

    Document Library  : Test Library

    ----------------------------------------------

    My problem is when I click the button it gives an error as the following.

    "

    The Web application at http://thekingsbury/ could not be found.
    Verify that you have typed the URL correctly.
    If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

    "

    The error shows at the following statement.

    "SPSite siteCollection = new SPSite(siteUrl);"

    I created both Windows & Web solutions and the result was the same.

    Note: I have add the reference to the SharePoint using the Microsoft.SharePoint.dll file. And used the "using Microsoft.SharePoint;" statement.

    Please can someone help me on this matter.It's a real paint to me.


    Tuesday, October 08, 2013 9:12 AM

Answers

  • You need to add below dll's in your solution located in (%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\ISAPI):

    Microsoft.SharePoint.Client.Runtime.dll
    Microsoft.SharePoint.Client.dll
    http://msdn.microsoft.com/en-us/library/ee857094%28v=office.14%29.aspx

    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 4:59 AM
    Wednesday, October 09, 2013 4:54 AM
  • Hello,

    >I think that code is not compatible with InfoPath VSTA

    Add linq ref in your solution the try.

    BTW use below code in your infopath VSTA to create doc lib:

    using(SPSite mySite = new SPSite("Http://URL to your site"))
    {
    using(SPWeb myWeb = mySite.AllWebs["Name of your Web"])
    {
     myWeb.AllowUnSafeUpdates = true;
     myWeb.Lists.Add("New Document Library", "This was created Programatically", SPListTemplateType.DocumentLibrary);
    myWeb.Update();
    myWeb.AllowUnsafeUpdates = false;
    }}
    Make sure that you have admin rights to create library in site and your infopath form is browser based form (OR if it client form then run in sharepoint server itself)

    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 5:00 AM
    Thursday, October 10, 2013 4:31 AM
  • Try this:

    using(SPSite mySite = new SPSite("Http://URL to your site"))
    {
    using(SPWeb myWeb = mySite.OpenWeb())
    {
    Web name is required when you have subsite so use OpenWeb().


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 5:00 AM
    Thursday, October 10, 2013 7:03 AM
  • You already have that code in your first post: (copy and paste)

    SPFolderCollection myFolders = site.Folders;myFolders.Add("http://thekingsbury/Test%20Library/" + newfoldername + "/");
    docLibrary.Update();

    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/c10e46b0-68a3-4125-9913-670975733504/how-to-create-folder-in-document-library-programatically-?forum=sharepointdevelopmentprevious


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 5:03 AM
    Thursday, October 10, 2013 11:47 AM
  • Try this. I have tested:

    SPDocumentLibrary _DocLibrary = (SPDocumentLibrary) _Web.Lists["My Documents"];
    SPFolderCollection _Folders = _Web.Folders;
    _Folders.Add("http://siteurl/My%20Documents/" + foldername + "/");
    _DocLibrary.Update();
    http://sharepoint-tutorial.net/post/2011/03/21/sharepoint-2010-programmatically-create-a-folder-in-document-libraries-and-lists.aspx


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 5:01 AM
    Friday, October 11, 2013 10:17 AM
  • I solved the problem by my self. The modified statement for creating a new folder within the newly created document library simultaneously is at the below.

    newFolders.Add("http://thekingsbury/"+newLibraryName+"/"+newFolderName+"/");

    The whole problem regarding this question was solved. Almost everything was solved by Hemendra Agrawal.

    Thanks for your commitment.

    Regards,

    Chiranthaka

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 5:01 AM
    Monday, October 14, 2013 4:57 AM
  • The complete solution using an InfoPath 2010 form is as the below.

    using Microsoft.Office.InfoPath;
    using System;
    using System.Xml;
    using System.Xml.XPath;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint.Linq;
    namespace Create_Folder_In_SharePoint_Library
    {
        public partial class FormCode
        {
            public void InternalStartup()
            {
                ((ButtonEvent)EventManager.ControlEvents["btnCreateFolder"]).Clicked += new ClickedEventHandler(btnCreateFolder_Clicked);
            }
            public void btnCreateFolder_Clicked(object sender, ClickedEventArgs e)
            {
                   SPSite mySite = new SPSite("Http://thekingsbury/");
                    {
                        SPWeb myWeb = mySite.OpenWeb();
                        {
                            //Code to retreive the new library.
                            XPathNavigator xLibraryName = MainDataSource.CreateNavigator();
                            String NewLibraryName = xLibraryName.SelectSingleNode("/my:myFields/my:LibraryName", NamespaceManager).Value;
                            //Code to retreive the library description.
                            XPathNavigator xLibraryDesc = MainDataSource.CreateNavigator();
                            String NewLibDesc = xLibraryDesc.SelectSingleNode("/my:myFields/my:LibraryDescription", NamespaceManager).Value;
                            //Code to retreive the new folder name.
                            XPathNavigator xFolderName = MainDataSource.CreateNavigator();
                            String NewFolderName = xFolderName.SelectSingleNode("/my:myFields/my:FolderName", NamespaceManager).Value;
                            //Creating the new library.
                            myWeb.Lists.Add(NewLibraryName, NewLibDesc, SPListTemplateType.DocumentLibrary);
                            myWeb.Update();
                            //Creating the new folder within the new library.
                            SPDocumentLibrary newDocLibrary = (SPDocumentLibrary)myWeb.Lists[NewLibraryName];
                            SPFolderCollection newFolders = myWeb.Folders;
                            newFolders.Add("http://thekingsbury/" + NewLibraryName + "/" + NewFolderName + "/");
                            newDocLibrary.Update();
                        }
                    }
              }
        }
    }

    I think everyone can use this.
    Friday, December 20, 2013 10:17 AM

All replies

  • Hello,

    Since you are using object model code so you can't run your code remotely or outside the sharepoint environment that's why you are getting error. (no matter you use windows or web application). You have to run above code in sharepoint server itself.

    For client side, you have to use client object model or web service to connect with your site and make changes remotely.

    Client Object model Ref:

    http://www.makdeniz.com/create-folder-in-document-library-using-sharepoint-2010-client-object-model/

    http://msdn.microsoft.com/en-us/library/ee537247%28v=office.14%29.aspx

    Let us know if you have any doubt


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 08, 2013 10:36 AM
  • To use the Client object model what are the .dll files must be used. and how to import them into the solution?
    Tuesday, October 08, 2013 3:58 PM
  • You need to add below dll's in your solution located in (%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\ISAPI):

    Microsoft.SharePoint.Client.Runtime.dll
    Microsoft.SharePoint.Client.dll
    http://msdn.microsoft.com/en-us/library/ee857094%28v=office.14%29.aspx

    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 4:59 AM
    Wednesday, October 09, 2013 4:54 AM
  • Ok pal I have followed your guide and got the result I want. But there is a small problem and that is how am I supposed to embed that console app into SharePoint server and how to execute that?
    Wednesday, October 09, 2013 9:20 AM
  • If you use client object model then you don't need to execute your console application in sharepoint. You can execute thsi from any client machine.

    Sharepoint involvement is required if you use server side code as you were doing before.


    Hemendra:Yesterday is just a memory,Tomorrow we may never see

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    ">


    Wednesday, October 09, 2013 9:24 AM
  • My full requirement is as the following. 1. I want to upload scanned documents directly from the scanner to the Document Library previously created. I have done that successfully using KODAK Scan & View software application.So no worries on that matter. Because I already solved that. 2. Now I want to upload the scanned documents to the document libraries previously created. Also if the user want to create a new library then that facility must be provided not using the SharePoint UI but using an InfoPath form or other matter. 3. Also I need the facility to create folders and sub folders using an InfoPath form or other matter same as creating a document library, and that structure must be selected within the proposed InfoPath as an example using drop-down list and suchlike. As mentioned previously this is what I want to do. If some know how to solve this matter please kind to help me. Thank You, Chiranthaka
    Wednesday, October 09, 2013 11:11 AM
  • Where did you mention the infopath in your earlier posts?

    If you are using infopath browser based form then you can use server code (as you were doing in your first post). Infopath browser form can be published in sharepoint environment only so no need to use client object model.

    Client object model is helpful if you have no access to sharepoint server or want to access site without SP server.

    Your 2 and 3 points can be easily achieved by infopath and use server code.

    Use your first post code in infopath form to upload document in library and here is blog to create library via code:

    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/e01e2f60-9c2b-4298-b9c0-c3ea6d46a264/how-to-programmatically-create-a-document-library?forum=sharepointdevelopmentlegacy

    Let me know in case any issue or doubt


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Wednesday, October 09, 2013 11:59 AM
  • I tried the solution you provided in InfoPath but it gave me some errors in the

    var query = clientContext.LoadQuery(rootWeb.Lists.Where(p => p.Title == "SharedFolder"));

    statement @ var & p => p.Title sections.

    Stating that they were not declared. That solution is also from console app and I think that code is not compatible with InfoPath VSTA. I think you may have some sort of solution for these.

    Wednesday, October 09, 2013 3:54 PM
  • Hello,

    >I think that code is not compatible with InfoPath VSTA

    Add linq ref in your solution the try.

    BTW use below code in your infopath VSTA to create doc lib:

    using(SPSite mySite = new SPSite("Http://URL to your site"))
    {
    using(SPWeb myWeb = mySite.AllWebs["Name of your Web"])
    {
     myWeb.AllowUnSafeUpdates = true;
     myWeb.Lists.Add("New Document Library", "This was created Programatically", SPListTemplateType.DocumentLibrary);
    myWeb.Update();
    myWeb.AllowUnsafeUpdates = false;
    }}
    Make sure that you have admin rights to create library in site and your infopath form is browser based form (OR if it client form then run in sharepoint server itself)

    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 5:00 AM
    Thursday, October 10, 2013 4:31 AM
  • I have tried that code snippet you have stated above> However it given me an error at the following statement.

    myWeb.AllowUnSafeUpdates = true;

    Error : 'Microsoft.SharePoint.SPWeb' does not contain a definition for 'AllowUnSafeUpdates'    C:\Users\Administrator\Documents\InfoPath Projects\Create New Folder Form1\FormCode.cs    43    27    Create New Folder Form.

    I also added the SharePoint.Linq reference. But the error is still exists.

    Please try to solve this matter also.

    Thank You,

    Chiranthaka


    Thursday, October 10, 2013 6:18 AM
  • Do you have site collection admin rights? How and where you are executing your code?

    Try to remove below lines from code:

    myWeb.AllowUnSafeUpdates = true;
    myWeb.AllowUnSafeUpdates = false;


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Thursday, October 10, 2013 6:29 AM
  • I tried removing those statements and it given me an error at the following statement.

    myWeb.Lists.Add("New Document Library", "This was created Programatically", SPListTemplateType.DocumentLibrary);

    Error: <nativehr>0x80070002</nativehr><nativestack></nativestack>There is no Web named "/thekingsbury".

    I execute this code snippet using an InfoPath form and an event of a button. Also I have the administrator rights for the SharePoint site.

    Site URL : http://thekingsbury/

    ** This site is the root site.

    Site Name: thekingsbury

    When I click the button on my InfoPath form this code snippet will execute.

    I tried this InfoPath form in the 'Preview State".

    I think this will help you to solve this matter.

    Thursday, October 10, 2013 6:46 AM
  • Try this:

    using(SPSite mySite = new SPSite("Http://URL to your site"))
    {
    using(SPWeb myWeb = mySite.OpenWeb())
    {
    Web name is required when you have subsite so use OpenWeb().


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 5:00 AM
    Thursday, October 10, 2013 7:03 AM
  • HI,

    Change your project framework to .net 3.5, and also change project in x64 platform instead of x 86.

    And rebuild the solution.

    it will working fine.

    Thursday, October 10, 2013 8:13 AM
  • OK pal I got your advice and solve my problem. Many Thanks.
    Thursday, October 10, 2013 11:04 AM
  • Ok now I have created the library using InfoPath & managed code. Now I want to create a new folder within a document library using InfoPath and managed code in C# within VSTA environment. How am I supposed to do that? Please help me on this thing also!

    Thanks,

    Chiranthaka

    Thursday, October 10, 2013 11:16 AM
  • You already have that code in your first post: (copy and paste)

    SPFolderCollection myFolders = site.Folders;myFolders.Add("http://thekingsbury/Test%20Library/" + newfoldername + "/");
    docLibrary.Update();

    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/c10e46b0-68a3-4125-9913-670975733504/how-to-create-folder-in-document-library-programatically-?forum=sharepointdevelopmentprevious


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 5:03 AM
    Thursday, October 10, 2013 11:47 AM
  • I got some errors as the following.

    Statement:

    SPFolderCollection myFolders = mySite.Folders;
      Error: Microsoft.SharePoint.SPSite does not contain a definition for the 'Folders'.
    mySite.update();
     Error: Microsoft.SharePoint.SPSite does not contain a definition for the 'Folders'.

    I don't know how to fix these errors. Please try to solve this again.

    Many many thanks,

    Chiranthaka

    Friday, October 11, 2013 9:41 AM
  • Try this. I have tested:

    SPDocumentLibrary _DocLibrary = (SPDocumentLibrary) _Web.Lists["My Documents"];
    SPFolderCollection _Folders = _Web.Folders;
    _Folders.Add("http://siteurl/My%20Documents/" + foldername + "/");
    _DocLibrary.Update();
    http://sharepoint-tutorial.net/post/2011/03/21/sharepoint-2010-programmatically-create-a-folder-in-document-libraries-and-lists.aspx


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 5:01 AM
    Friday, October 11, 2013 10:17 AM
  • _Folders.Add("http://siteurl/My%20Documents/" + foldername + "/");

    At the above statement the library name is defined as a static one. But my requirement is the newly created library must have the ability to create a new folder & the name must be provided using a text box (xpath will be used).

    Please try to solve this,

    Thanks

    Chiranthaka

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 5:18 AM
    • Unmarked as answer by Chiranthaka J Monday, October 14, 2013 5:18 AM
    Saturday, October 12, 2013 1:34 AM
  • Use SPWeb.Url to get current site URL then pass lib name and folder name as string. Here is sample code:

    using(SPSite mySite = new SPSite("Http://URL to your site"))
    {
    using(SPWeb myWeb = mySite.OpenWeb())
    {
     myWeb.AllowUnSafeUpdates = true;
     myWeb.Lists.Add("New Document Library", "This was created Programatically", SPListTemplateType.DocumentLibrary);
     myWeb.Update();
     
     //write your code to add folder and pass dynamic name in below line:  
     string libURL = myWeb.Url + "/" + "New Document Library" + "/" + passnewfoldernamehere;
     _Folders.Add(liburl + "/");
    
     myWeb.AllowUnsafeUpdates = false;
    }}


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Monday, October 14, 2013 4:35 AM
  • I solved the problem by my self. The modified statement for creating a new folder within the newly created document library simultaneously is at the below.

    newFolders.Add("http://thekingsbury/"+newLibraryName+"/"+newFolderName+"/");

    The whole problem regarding this question was solved. Almost everything was solved by Hemendra Agrawal.

    Thanks for your commitment.

    Regards,

    Chiranthaka

    • Marked as answer by Chiranthaka J Monday, October 14, 2013 5:01 AM
    Monday, October 14, 2013 4:57 AM
  • The complete solution using an InfoPath 2010 form is as the below.

    using Microsoft.Office.InfoPath;
    using System;
    using System.Xml;
    using System.Xml.XPath;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint.Linq;
    namespace Create_Folder_In_SharePoint_Library
    {
        public partial class FormCode
        {
            public void InternalStartup()
            {
                ((ButtonEvent)EventManager.ControlEvents["btnCreateFolder"]).Clicked += new ClickedEventHandler(btnCreateFolder_Clicked);
            }
            public void btnCreateFolder_Clicked(object sender, ClickedEventArgs e)
            {
                   SPSite mySite = new SPSite("Http://thekingsbury/");
                    {
                        SPWeb myWeb = mySite.OpenWeb();
                        {
                            //Code to retreive the new library.
                            XPathNavigator xLibraryName = MainDataSource.CreateNavigator();
                            String NewLibraryName = xLibraryName.SelectSingleNode("/my:myFields/my:LibraryName", NamespaceManager).Value;
                            //Code to retreive the library description.
                            XPathNavigator xLibraryDesc = MainDataSource.CreateNavigator();
                            String NewLibDesc = xLibraryDesc.SelectSingleNode("/my:myFields/my:LibraryDescription", NamespaceManager).Value;
                            //Code to retreive the new folder name.
                            XPathNavigator xFolderName = MainDataSource.CreateNavigator();
                            String NewFolderName = xFolderName.SelectSingleNode("/my:myFields/my:FolderName", NamespaceManager).Value;
                            //Creating the new library.
                            myWeb.Lists.Add(NewLibraryName, NewLibDesc, SPListTemplateType.DocumentLibrary);
                            myWeb.Update();
                            //Creating the new folder within the new library.
                            SPDocumentLibrary newDocLibrary = (SPDocumentLibrary)myWeb.Lists[NewLibraryName];
                            SPFolderCollection newFolders = myWeb.Folders;
                            newFolders.Add("http://thekingsbury/" + NewLibraryName + "/" + NewFolderName + "/");
                            newDocLibrary.Update();
                        }
                    }
              }
        }
    }

    I think everyone can use this.
    Friday, December 20, 2013 10:17 AM
  • The complete solution using an InfoPath 2010 form is as the below.

    using Microsoft.Office.InfoPath;
    using System;
    using System.Xml;
    using System.Xml.XPath;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint.Linq;
    namespace Create_Folder_In_SharePoint_Library
    {
        public partial class FormCode
        {
            public void InternalStartup()
            {
                ((ButtonEvent)EventManager.ControlEvents["btnCreateFolder"]).Clicked += new ClickedEventHandler(btnCreateFolder_Clicked);
            }
            public void btnCreateFolder_Clicked(object sender, ClickedEventArgs e)
            {
                   SPSite mySite = new SPSite("Http://thekingsbury/");
                    {
                        SPWeb myWeb = mySite.OpenWeb();
                        {
                            //Code to retreive the new library.
                            XPathNavigator xLibraryName = MainDataSource.CreateNavigator();
                            String NewLibraryName = xLibraryName.SelectSingleNode("/my:myFields/my:LibraryName", NamespaceManager).Value;
                            //Code to retreive the library description.
                            XPathNavigator xLibraryDesc = MainDataSource.CreateNavigator();
                            String NewLibDesc = xLibraryDesc.SelectSingleNode("/my:myFields/my:LibraryDescription", NamespaceManager).Value;
                            //Code to retreive the new folder name.
                            XPathNavigator xFolderName = MainDataSource.CreateNavigator();
                            String NewFolderName = xFolderName.SelectSingleNode("/my:myFields/my:FolderName", NamespaceManager).Value;
                            //Creating the new library.
                            myWeb.Lists.Add(NewLibraryName, NewLibDesc, SPListTemplateType.DocumentLibrary);
                            myWeb.Update();
                            //Creating the new folder within the new library.
                            SPDocumentLibrary newDocLibrary = (SPDocumentLibrary)myWeb.Lists[NewLibraryName];
                            SPFolderCollection newFolders = myWeb.Folders;
                            newFolders.Add("http://thekingsbury/" + NewLibraryName + "/" + NewFolderName + "/");
                            newDocLibrary.Update();
                        }
                    }
              }
        }
    }

    I think everyone can use this.
     When you are generating a folder always try to avoid reserved characters  : ; # $ % ^ a and suchlike.
    Thursday, February 20, 2014 8:17 AM