Question

I need to create a simple folder in a document library in SharePoint, but I can't seem to find a scrap of documentation on the subject.

The dws webservice seems to be used to create physical folders in a workspace, I need a way to create a folder in a document library.

Not sure what to do , please help

Was it helpful?

Solution

I found this method to work :

    HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create("http://mySite/MyList/MyfolderIwantedtocreate");
    request.Credentials = CredentialCache.DefaultCredentials;
    request.Method = "MKCOL";
    HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
    response.Close();

OTHER TIPS

This is the code for similar request in JAVA using apache HttpClient

import org.apache.http.*;

private static HttpResponse makeFolder(
            String url,
            DefaultHttpClient httpClient) throws Exception {
    BasicHttpRequest httpPost = new BasicHttpRequest("MKCOL", url);
    HttpUriRequest httpUriRequest = new RequestWrapper(httpPost);

    HttpResponse status = httpClient.execute(httpUriRequest);
    EntityUtils.consume(status.getEntity());
    return status;
}

Code to create the httpClient and call the makeFolder Function

DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getCredentialsProvider().setCredentials(
        AuthScope.ANY,
        new NTCredentials(config.getUserName(), config.getPasswords(),
                        "", config.getDomain()));

I know this is a pretty old question, but in case someone else finds it, this is how I've done it:

       String CAML =  "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
            "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
            "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
        "<soap:Body>" +
        "<CreateFolder " + "xmlns=\"http://schemas.microsoft.com/sharepoint/soap/dws/\">"+
            "<url>" + ParentFolder+'/'+NewFolderName+ "</url>"+
        "</CreateFolder>"+
        "</soap:Body>" +
        "</soap:Envelope>";

       String uri = "http://[your site]/_vti_bin/dws.asmx";

       WebClient client = new WebClient();
        client.Headers["SOAPAction"] = "http://schemas.microsoft.com/sharepoint/soap/dws/CreateFolder";
        client.Headers["content-type"] = "text/xml; charset=utf-8";
        client.Encoding = Encoding.UTF8;
        client.UploadStringCompleted += UploadStringCompleted;
        try
        {
            client.UploadStringAsync(new Uri(uri, UriKind.Absolute), "POST", CAML);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in upload string async: " + ex.Message);
        }

I was using silverlight, which is why I used upload string async, but this can be done other ways with the same http post method

I've done some work with the Web services but I can't find any code that creates a folder. However, I have code that copies files from a network share to an existing folder in a SharePoint document library using UNC paths. It uses System.IO.File - perhaps you could use that technique to create a folder?

Created folders in sharepoint by using Document Workspace Web Service (Dws). Works great.

public static bool CreateSPFolder(string FolderDir, string FolderName, yourCredentialsClass credentials)
{
    FolderName = ReplaceInvalidChars(FolderName);

    // create an instance of the sharepoint service reference
    Dws.Dws dwsWebService = new Dws.Dws();
    dwsWebService.Url = credentials.SharePointUrl + "/_vti_bin/Dws.asmx";
    dwsWebService.Credentials = new NetworkCredential(credentials.UserId, credentials.Password);

    string result = dwsWebService.CreateFolder(string.Format("{0}/{1}",FolderDir,FolderName));
    dwsWebService.Dispose();

    if (result == null)
    {
        throw new Exception("No response creating SharePoint folder");
    }

    if (result.Equals("<Result/>"))
    {
        return true;
    }

    return false;
}

public static string ReplaceInvalidChars(string strIn)
{
    return Regex.Replace(strIn.Replace('"', '-'), @"[.~#%&*{}:<>?|/]", "-");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top