locked
Add a folder to a list using WebService RRS feed

  • Question

  • I am able to add a list using the web service, but want to add folders inside of the list and have been unsuccessful...any help?

     

    Here is what I used to create the list:

    ' instatiate list service

    Dim listService As New SharePointLists.Lists()

    ' set credentials

    Dim nc As New System.Net.NetworkCredential

    nc = System.Net.CredentialCache.DefaultCredentials

    listService.Credentials = nc

    ' Concatenate name with GUID for uniqueness

    Dim listName As String = String.Concat(Name, "-"c, id)

    ' Create document library templateid 101 is a document library

    Dim result As System.Xml.XmlNode = listService.AddList(listName, "Document Library for " + Name, 101)

     

     

    Thanks in advance,

    Chad

    Friday, September 28, 2007 11:42 PM

Answers

  • Hi,

    This can be done by either of two ways:

    1) make use of Lists web service's UpdateListsItems method to  create a folder within the list. you need to look at sdk for exact xml parameters needed to be passed for creating the folder.

    however there is another easy way, by making use of webdav protocol:
    this is how you can do this: (this is in C#, you can convert it for your use in VB.NET)
    2)
    HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create("http://server/site/listname/foldernamethatyouwanttocreate");
    request.Credentials = CredentialCache.DefaultCredentials;
    request.Method = "MKCOL";
    HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
    response.Close();

    Thats it. this will create a folder in the listname list.





    Saturday, September 29, 2007 6:38 AM

All replies

  • Hi,

    This can be done by either of two ways:

    1) make use of Lists web service's UpdateListsItems method to  create a folder within the list. you need to look at sdk for exact xml parameters needed to be passed for creating the folder.

    however there is another easy way, by making use of webdav protocol:
    this is how you can do this: (this is in C#, you can convert it for your use in VB.NET)
    2)
    HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create("http://server/site/listname/foldernamethatyouwanttocreate");
    request.Credentials = CredentialCache.DefaultCredentials;
    request.Method = "MKCOL";
    HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
    response.Close();

    Thats it. this will create a folder in the listname list.





    Saturday, September 29, 2007 6:38 AM
  • Hi,

    I tried the above code and it is running without any error but it could not create any folder inside the document Library. Is there any modifications that I have to do. Thanks

    Friday, October 26, 2007 3:50 PM
  • This example worked perfectly for me. I added a couple extra lines of code to allow the user to select the name of the folder. In my example, the folder's name is a numerical ID:

            void CreateFolder(int id)
            {
                SPDocumentLibrary doclib = SPContext.Current.Web.Lists["INSERT LIST NAME HERE"] as SPDocumentLibrary;

                HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create(
                    String.Format("{0}/{1}",
                        SPContext.Current.Site.MakeFullUrl(doclib.RootFolder.ServerRelativeUrl),
                        id));

                request.Credentials = CredentialCache.DefaultCredentials;
                request.Method = "MKCOL";
                HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                response.Close();
            }
    Saturday, November 3, 2007 1:07 AM
  • Not sure why, but my solutions stopped working for me. All of a sudden I started getting an Error 405, Method Now Allowed.

    After researching the WSS 3.0 API, I found a cleaner solution through the SDK:

    SPDocumentLibrary doclib = SPContext.Current.Web.Lists["INSERT LIST NAME HERE"] as SPDocumentLibrary;
    SPListItem item = doclib.Items.Add( SPContext.Current.Site.MakeFullUrl( doclib.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, "FOLDER NAME" );
    item.Update();
    doclib.Update();

    This should work and is a lot more stable than generating and firing an HTTP Request.
    Tuesday, November 13, 2007 1:21 AM