How to create a folder in the document library on creation of a new list item using JavaScript?

Answered How to create a folder in the document library on creation of a new list item using JavaScript?

  • Dienstag, 26. Juli 2011 18:32
     
     

    Hi,

    I want to create a new folder in a SharePoint document library , I want to create that on PreSaveAction event i.e every time when  I create a new list item a folder corresponding to that item should be created in the document  library(the name of the folder should be  some unique value in the list item).I want to do this using using JavaScript and Client Object model(Server side C# code is not acceptable due to client constraints).

     

    • Verschoben Mike Walsh FIN Mittwoch, 27. Juli 2011 09:57 All Wiki questions go to Social Computing (From:SharePoint - Design and Customization (pre-SharePoint 2010))
    •  

Alle Antworten

  • Mittwoch, 27. Juli 2011 02:12
     
     

    I'm curious to know why that's a requirement. What's the purpose or end goal of this document library?

     


    Pman
    http://www.pmansLab.com/
  • Mittwoch, 27. Juli 2011 09:40
     
     

    I want to use a wiki library(and use its pre save action event for creating a new folder in some document library),As  wiki does not have the attachment option so what I want to do is: I want to create a folder in a document library and add all the related atytachments there Every time I create a new wiki article.

     


    Amol Gade
  • Freitag, 29. Juli 2011 07:42
     
     
    Can some body reply to this???
    Amol Gade
  • Dienstag, 2. August 2011 10:18
    Moderator
     
     Beantwortet

    Hi Amol,

    You posted your question in pre-SharePoint 2010 forum, so I assume you are uing Microsoft Office SharePoint Server(MOSS) 2007. You mentioned "Client Object model" in your post too, however, "Client Object model" is only available from SharePoint 2010.

    In SharePoint 2007, in order to use JavaScript to create a folder in a document library, please use JavaScript to involve SharePoint web service to create the folder. The web service is lists web service, the method is UpdateListItems.
    Below is the code snippet for your reference:
    function CreateFolder(listname, viewId, foldername) {

        //The batch to create folder
        var batch = '<Batch OnError="Continue" PreCalc="TRUE" ListVersion="0" ViewName="' + viewId + '">' + '   <Method ID="1" Cmd="New">' + '      <Field Name="ID">New</Field>' + '      <Field Name="FSObjType">1</Field>' + '      <Field Name="BaseName">' + foldername + '</Field>' + '   </Method>' + '</Batch>';

        // Variables
        var xmlHttpReq = null;

        // Mozilla/Safari
        if (window.XMLHttpRequest) {
            xmlHttpReq = new XMLHttpRequest();
        }

        // IE
        else if (window.ActiveXObject) {
            xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }

        if (xmlHttpReq) {

            xmlHttpReq.open('POST', 'http://sps2010-01/_vti_bin/lists.asmx', true);

            //Set the Headers
            xmlHttpReq.setRequestHeader('Content-Type', 'text/xml');
            xmlHttpReq.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/sharepoint/soap/UpdateListItems');

            //get the XML Request string
            xmlHttpReq.send('<?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>' + ' <UpdateListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">' + '  <listName>' + listname + '</listName>' + '  <updates>' + batch + '</updates>' + ' </UpdateListItems>' + ' </soap:Body>' + '</soap:Envelope>');


            //When the response is available, update the response text
            xmlHttpReq.onreadystatechange = function () {
                if (xmlHttpReq.readyState == 4) {
                    if (xmlHttpReq.status == 200) {
                        alert(xmlHttpReq.responseText);
                    } else {
                        alert(xmlHttpReq);
                    }
                }
            }
        } else {

            alert('unable to create XMLHttpRequest object');

        }

    }

    For more information, please see:
    Lists.UpdateListItems Method: http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems(v=office.12).aspx

    If there is anything unclear, please feel free to ask.

    Thanks,
    Jinchun Chen


    Jinchun Chen
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff AT microsoft.com(Please replace AT with @)
  • Dienstag, 2. August 2011 10:19
    Moderator
     
     Beantwortet
    Also, if you would like to use Client Object model in SharePoint 2010 to create a folder, please see:
    http://ranaictiu-technicalblog.blogspot.com/2010/03/sharepoint-2010-manage.html
    Jinchun Chen
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff AT microsoft.com(Please replace AT with @)
  • Dienstag, 10. April 2012 07:50
     
     
    Hi, I want to check the folder already exists before creating a new folder if not then only I can able to create the folder? how can I achieve that through the javascript in sharepoint 2007?