0

I've got multiple custom lists with a lot of fields and formulas. I know I can save the list as a template but I was wondering if there is anyway to use JavaScript to save the list definition as a string so I can re-create it using JavaScript again?

1

Export it to any schema you want, and you have to write the import based on it as well.

The simplest would be to take the JSON result from a REST call.

Only 'standardized' schema is PnP format (that is an XML schema)

But PnP is push, not client side pull with JavaScript, to re-create the lists

  • What method would I use to get the schema and then what method to create? Will I have to modify the returned schema or just lift and drop? – IMTheNachoMan Mar 22 '17 at 2:54
  • Do a REST call, you get a JSON response with which you can do whatever you want. And it is never lift and drop, you have to program the creation of a new List and Items yourself. PnP Core JS library can help there.. but it is still writing 20 or 30 lines of code – Danny '365CSI' Engelman Mar 22 '17 at 12:29
  • Okay. Let me see if I can find the REST call I need to use. Do you think I could do it using SOAP and GetList? – IMTheNachoMan Mar 22 '17 at 18:13
  • Sure, you will get an XML response if I remember m,y SOAP. Same concept.. create you own schema, write your own schema.. If you need more help I want you paycheck as well, as I would be doing all the work – Danny '365CSI' Engelman Mar 23 '17 at 8:58
  • @danny-365csi-engelman Haha. Nothing like that. I'm not even getting paid for this. I was asking because I tried GetList and AddList but it didn't work. I will keep trying and if needed I'll post a new question. Thanks! – IMTheNachoMan Mar 23 '17 at 17:09
0

There isn't an easy export then re-import , but my technique took me just a day. Export the list you want using any post tool.

I like to use _vti_bin.lists.asmx with GetListAndView You will need the view GUID or leave viewName blank for the All Items view.

Parse the output of that into the web service calls below: Lists.asmx AddList Lists.asmx UpdateList Views.asmx AddView

Computed colums need to be xml formatted like this (I didn't get this into my example, so including it here just ripped from a GetListAndView)

   <Field Type="Calculated" DisplayName="Calc"  ResultType="Text" ReadOnly="TRUE"  StaticName="Calc" Name="Calc">
    <Formula>=Title+Regression</Formula>
    <FormulaDisplayNames>=Title+Regression</FormulaDisplayNames>
     <FieldRefs>
      <FieldRef Name="Regression"/>
      <FieldRef Name="Title"/>
     </FieldRefs>
   </Field>

GUIDs are useless, strip them all.

SP reserves a bunch of stuff you don't need, but they are great for inspiration on what you can do with a list. So clean that up (It may whine at you if you have more to clean up.)

Helpful guideline: javascript helps you do all the things users would do if they knew how. So, with this exercise you can use javascript to do all of the things users can and would have to do if they built from scratch.

Here is how I recreate a list over and over again.

        function soap(ListName) {
        var xmlhttp = new XMLHttpRequest();
        alert ('Please wait while the list "' + ListName + '" is created')
        xmlhttp.open('POST', '../_vti_bin/Lists.asmx', true);

        // build SOAP request
        var sr =
            '<?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>' +
                    '<AddList xmlns="http://schemas.microsoft.com/sharepoint/soap/">' +
                        '<listName>' + ListName + '</listName>' +
                        '<description></description>' +
                        '<templateID>100</templateID>' +
                    '</AddList>' +
                '</soap:Body>' +
            '</soap:Envelope>';

        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    xmlDoc = xmlhttp.responseXML;
                    ListName = xmlDoc.getElementsByTagName("List");
                    //alert (ListName[0].getAttribute('Title')) 
                    //alert('done. use firebug/console to see network response');
                    AddFields(ListName[0].getAttribute('Title'))
                }
                else {
                    alert('Creating List error: ' + xmlhttp.statusText)
                }
            }
        }
        // Send the POST request
        xmlhttp.setRequestHeader('Content-Type', 'text/xml');
        xmlhttp.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/sharepoint/soap/AddList');
        xmlhttp.send(sr);
        // send request
        // ...
    }

    function AddFields(ListName) {
        var xmlhttp2 = new XMLHttpRequest();
        xmlhttp2.open('POST', '../_vti_bin/Lists.asmx', true);

        // build SOAP request
        var srF =
            '<?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>' +
            '    <UpdateList xmlns="http://schemas.microsoft.com/sharepoint/soap/">' +
            '           <listName>' + ListName + '</listName>' +
            '           <newFields>' +
            '               <Fields>' +
            '                   <Method ID="1" AddToView="">' +
            '                       <Field Type="Note" Name="Step" DisplayName="Testing Step/Process" FromBaseType="TRUE"  RichText="TRUE" Description=""/>' +
            '                   </Method>' +
            '                   <Method ID="2" AddToView="">' +
            '                       <Field Type="Note" Name="Expected_Outcome" DisplayName="Expected Outcome" FromBaseType="TRUE" RichText="TRUE"  Description=""/>' +
            '                   </Method>' +
            '                   <Method ID="3" AddToView="">' +
            '                       <Field Type="Note" Name="Actual_Outcome" DisplayName="Actual Outcome" FromBaseType="TRUE"  RichText="TRUE" Description=""/>' +
            '                   </Method>' +
            '                   <Method ID="4" AddToView="">' +
            '                       <Field Type="Boolean" Name="Pass" DisplayName="Pass" FromBaseType="TRUE" Description=""/>' +
            '                   </Method>' +
            '                   <Method ID="5" AddToView="">' +
            '                       <Field Type="Note" Name="Comments" DisplayName="Comments/Issues" FromBaseType="TRUE" RichText="TRUE" Description=""/>' +
            '                   </Method>' +
            '                   <Method ID="6" AddToView="">' +
            '                       <Field Type="DateTime" Name="Test_Date" DisplayName="Test Date" FromBaseType="TRUE" Description=""/>' +
            '                   </Method>' +
            '                   <Method ID="7" AddToView="">' +
            '                       <Field Type="User" Name="Assigned_To" DisplayName="Assigned To" FromBaseType="TRUE" Description=""/>' +
            '                   </Method>' +
            '                   <Method ID="8" AddToView="">' +
            '                       <Field Type="DateTime" Name="Assignment_Date" DisplayName="Assignment Date" FromBaseType="TRUE" Description=""/>' +
            '                   </Method>' +
            '                   <Method ID="9" AddToView="">' +
            '                       <Field Type="Boolean" Name="Regression" DisplayName="Regression" FromBaseType="TRUE" Description=""/>' +
            '                   </Method>' +
            '                   <Method ID="10" AddToView="">' +
            '                       <Field Type="Boolean" Name="Script" DisplayName="Script" FromBaseType="TRUE" Description=""/>' +
            '                   </Method>' +
            '                   <Method ID="11" AddToView="">' +
            '                       <Field Type="Text" Name="Test_Group" DisplayName="Test Group" FromBaseType="TRUE" Description=""/>' +
            '                   </Method>' +
            '                   <Method ID="12" AddToView="">' +
            '                       <Field Type="Choice" Name="Suite" DisplayName="Test Suite" FromBaseType="TRUE" Description="">' +
            '                           <CHOICES>' +
            '                               <CHOICE>Unit</CHOICE>' +
            '                               <CHOICE>System</CHOICE>' +
            '                               <CHOICE>SU/RA</CHOICE>' +
            '                               <CHOICE>Workflow</CHOICE>' +
            '                               <CHOICE>End User Validation</CHOICE>' +
            '                               <CHOICE>Integration</CHOICE>' +
            '                           </CHOICES>' +
            '                       </Field>' +
            '                   </Method>' +
            '               </Fields>' +
            '           </newFields>' +
            '     </UpdateList>' +
            '  </soap:Body>' +
            '</soap:Envelope>';

        xmlhttp2.onreadystatechange = function () {
           if (xmlhttp2.readyState == 4) {
                if (xmlhttp2.status == 200) {
                    xmlDoc = xmlhttp2.responseXML;
                    ListName = xmlDoc.getElementsByTagName("ListProperties");
                    //alert (ListName[0].getAttribute('Title')) 
                    //alert('done. use firebug/console to see network response');
                    AddViews(ListName[0].getAttribute('Title'))
                }
                else {
                    alert('Creating List error: ' + xmlhttp2.statusText)
                }
            }
        }
        // Send the POST request
        xmlhttp2.setRequestHeader('Content-Type', 'text/xml');
        xmlhttp2.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/sharepoint/soap/UpdateList');
        xmlhttp2.send(srF);
        // send request
        // ...
    }
    function AddViews(ListName) {
        var xmlhttp4 = new XMLHttpRequest();
        xmlhttp4.open('POST', '../_vti_bin/Views.asmx', true);
        //alert ("Creating Scripts View")
        // build SOAP request
        var srV1 =
            '<?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>' +
            '    <AddView xmlns="http://schemas.microsoft.com/sharepoint/soap/">' +
            '           <listName>' + ListName + '</listName>' +
            '           <viewName>Scripts</viewName>' +
            '            <query>' +
            '               <Query>' +
            '                    <OrderBy>' +
            '                        <FieldRef Name="ID" />' +
            '                    </OrderBy>' +
            '                </Query>' +
            '            </query>' +        
            '            <viewFields>' +
            '               <ViewFields>' +
            '                        <FieldRef Name="Testing_x0020_Step_x002f_Process" />' +
            '                        <FieldRef Name="Expected_x0020_Outcome" />' +
            '                        <FieldRef Name="Assigned_x0020_To" />' +
            '                        <FieldRef Name="Assignment_x0020_Date" />' +
            '                        <FieldRef Name="Regression" />' +
            '                        <FieldRef Name="Script" />' +
            '                        <FieldRef Name="Test_x0020_Group" />' +
            '                        <FieldRef Name="Test_x0020_Suite" />' +
            '                    </ViewFields>' +
            '            </viewFields>' +
            '            <rowLimit><RowLimit Paged="TRUE">40</RowLimit></rowLimit>' +
            '     </AddView>' +
            '  </soap:Body>' +
            '</soap:Envelope>';

        xmlhttp4.onreadystatechange = function () {
            if (xmlhttp4.readyState == 4) {
                if (xmlhttp4.status == 200) {
                    //alert("Script view created")

                }
                else {
                    alert(xmlhttp4.statusText)
                }
            }
        }
        // Send the POST request
        xmlhttp4.setRequestHeader('Content-Type', 'text/xml');
        xmlhttp4.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/sharepoint/soap/AddView');
        xmlhttp4.send(srV1);
        // send request
        // ...
        //alert ("Creating Tests View")

        var xmlhttp3 = new XMLHttpRequest();
        xmlhttp3.open('POST', '../_vti_bin/Views.asmx', true);

        // build SOAP request
        var srV1 =
            '<?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>' +
            '    <AddView xmlns="http://schemas.microsoft.com/sharepoint/soap/">' +
            '           <listName>' + ListName + '</listName>' +
            '           <viewName>Tests</viewName>' +
            '            <query>' +
            '               <Query>' +
            '                    <OrderBy>' +
            '                        <FieldRef Name="ID" />' +
            '                    </OrderBy>' +
            '                </Query>' +
            '            </query>' +       
            '            <viewFields>' +
            '                    <ViewFields>' +
            '                        <FieldRef Name="Attachments" />' +
            '                        <FieldRef Name="LinkTitle" />' +
            '                        <FieldRef Name="Testing_x0020_Step_x002f_Process" />' +
            '                        <FieldRef Name="Expected_x0020_Outcome" />' +
            '                        <FieldRef Name="Actual_x0020_Outcome" />' +
            '                        <FieldRef Name="Pass" />' +
            '                        <FieldRef Name="Comments_x002f_Issues" />' +
            '                        <FieldRef Name="Test_x0020_Date" />' +
            '                        <FieldRef Name="Assigned_x0020_To" />' +
            '                        <FieldRef Name="Assignment_x0020_Date" />' +
            '                        <FieldRef Name="Script" />' +
            '                        <FieldRef Name="Test_x0020_Group" />' +
            '                    </ViewFields>' +
            '            </viewFields>' +
            '            <rowLimit><RowLimit Paged="TRUE">40</RowLimit></rowLimit>' +
            '     </AddView>' +
            '  </soap:Body>' +
            '</soap:Envelope>';

        xmlhttp3.onreadystatechange = function () {
            if (xmlhttp3.readyState == 4) {
                if (xmlhttp3.status == 200) {
                    //alert('Test View Created');

                }
                else {
                    alert(xmlhttp3.statusText)
                }
            }
        }
        // Send the POST request
        xmlhttp3.setRequestHeader('Content-Type', 'text/xml');
        xmlhttp3.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/sharepoint/soap/AddView');
        xmlhttp3.send(srV1);
        // send request
        // ...
        alert ("Go forth and test!")
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.