locked
adding item to a list using javascript RRS feed

  • Question

  • hey

    i need some help implment a javascript code on my moss 2007 site.

    i plan to  implment javascript code via content editor web part .

    my goal it that when someone enter this page it will add item to a list automatically.

    i found this article on the web: http://msdn.microsoft.com/en-us/library/hh185011(v=office.14).aspx

    but i don't know where i write this permetes : list name, site name and  what to write to which column.

    i need that someone can help me by re-write this code with this parameter

    list name:test
    site name: test1.test.com
    column name: Title
    parameter to column: yes

    hope you understand . ask me if it you don't understand something.


    • Edited by knowledge11 Saturday, April 13, 2013 12:34 PM
    Saturday, April 13, 2013 11:59 AM

Answers

  • That doesn't go on the beginning of the script.  You have to put it on the masterpage the way you were doing the body OnLoad.  I would normally add it to the masterpage by putting it in the PlaceholderAdditionalPageHead on the content page.  That will add it to the masterpage in the <Head> tag

    Paul Stork SharePoint Server MVP
    Principal Architect: Blue Chip Consulting Group
    Blog: http://dontpapanic.com/blog
    Twitter: Follow @pstork
    Please remember to mark your question as "answered" if this solves your problem.

    Sunday, April 21, 2013 4:53 PM
  • Add the JS function definition code in any js file or directyly add in master page and if added into JS file then provide the reference of JS file into Master page. After that, as per your requirement you need to add one item in the list, it should be happen on any event, means on click of any button or on load of particular page:

    1. If you want to add item on click of particular button from Content Pgae, then add function call on click of button:

    onclick="CreateListItem('Title1');"

    2. On load of any content page or every page, then add the code in content editior webpart or in JS file, where you defined the CreateListItem fucntion:

    function CallFunctionsOnLoad() {
    //Create item in the list
    CreateListItem('Title1');
    } 
    //Call function on window onload event
    window.onload = CallFunctionsOnLoad; 


    Thanks, Amit Kumar, LinkedIn Profile ** My Blog

    Tuesday, April 23, 2013 5:54 AM

All replies

  • First, that article is for SharePoint 2010 and won't work on a MOSS 2007 site.  For 2007 you will need to create SOAP request and send it to the lists.asmx web service to create a new record in a list.  The Client Side object model wasn't created until 2010.  Take a look at this example instead.

    http://www.codeproject.com/Tips/43928/Create-a-sharepoint-list-item-using-javascript-usi


    Paul Stork SharePoint Server MVP
    Principal Architect: Blue Chip Consulting Group
    Blog: http://dontpapanic.com/blog
    Twitter: Follow @pstork
    Please remember to mark your question as "answered" if this solves your problem.

    Saturday, April 13, 2013 6:39 PM
  • thanks for answer.
    i need more help.
    i place this code in content editor web part and change the parameter that i need to change that match my site.
    but it still don't work?what i do wrong?

    <script>
    function CreateListItem(Title)
     { 
                              
        var soap1 = createEnvelope(Title);
                   
        var ws = new ActiveXObject("Microsoft.XMLHTTP");   
          if(ws == null)
           return null;
               
        ws.open("POST",
             "http://test1.test.com/_vti_bin/lists.asmx",
             false);
        ws.setRequestHeader("Content-Type", "text/xml; charset=utf-8");           
        ws.setRequestHeader("SOAPAction",
            "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems">
            http://schemas.microsoft.com/sharepoint/soap/UpdateListItemsquot;);
        ws.send(soap1); 
        //   alert(soap1);
              
    }    
      function createEnvelope(Title)
    {              
            
        var soap = '<?xml version=\"1.0\" encoding=\"utf-8\"?>';
        soap += '<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 += '<soap:Body>';
        soap += '<UpdateListItems xmlns="%22">http://schemas.microsoft.com/sharepoint/soap/">';
        soap += '<listName>{1a5505ee-5739-4071-a1f5-66f3afaf3752}</listName>'     
        soap +=   '<updates><Batch>' 
        soap += '<Method ID="1" Cmd="New"><Field Name="Title">'+ Title +'</Field>;
        soap += ' </Method></Batch></updates>'   
        soap += '</UpdateListItems></soap:Body></soap:Envelope>';
     
        return soap; }

    </script>

    Sunday, April 14, 2013 1:04 PM
  • hey there.

    i hope that someone can help me.

    still waiting.any clue will help

    Thursday, April 18, 2013 7:18 AM
  • That code in a content editor web part should embed it on the page, but what invokes the function to make the soap call?

    Paul Stork SharePoint Server MVP
    Principal Architect: Blue Chip Consulting Group
    Blog: http://dontpapanic.com/blog
    Twitter: Follow @pstork
    Please remember to mark your question as "answered" if this solves your problem.

    Thursday, April 18, 2013 12:28 PM
  • <body onload='CreateListItem()'>

    </body>

    this is enought to invoke this function? no?

    Sunday, April 21, 2013 7:49 AM
  • The problem is that The <body> tag is on the master page and the CEWP with the javascript is on the content page.  Your onload event will fire when the masterpage loads, so it doesn't usually work for javascript embedded via a CEWP.  Take a look at the following blog for the right way to run javascript by default in SharePoint.

    http://sharepointsolutions.blogspot.com/2007/07/adding-javascript-to-content-editor-web.html


    Paul Stork SharePoint Server MVP
    Principal Architect: Blue Chip Consulting Group
    Blog: http://dontpapanic.com/blog
    Twitter: Follow @pstork
    Please remember to mark your question as "answered" if this solves your problem.

    Sunday, April 21, 2013 11:19 AM
  • first of thanks for your help.
    i add "_spBodyOnLoadFunctionNames.push("CreateListItem")" to the begin of the script. but still nothing.
    the guid list  is correct and also the site url.

    ן tried also to declare function first and then push in spbodyonloadfunctionsnames and it still don't solve the problem.

    what can be the problem?



    Sunday, April 21, 2013 12:07 PM
  • That doesn't go on the beginning of the script.  You have to put it on the masterpage the way you were doing the body OnLoad.  I would normally add it to the masterpage by putting it in the PlaceholderAdditionalPageHead on the content page.  That will add it to the masterpage in the <Head> tag

    Paul Stork SharePoint Server MVP
    Principal Architect: Blue Chip Consulting Group
    Blog: http://dontpapanic.com/blog
    Twitter: Follow @pstork
    Please remember to mark your question as "answered" if this solves your problem.

    Sunday, April 21, 2013 4:53 PM
  • Add the JS function definition code in any js file or directyly add in master page and if added into JS file then provide the reference of JS file into Master page. After that, as per your requirement you need to add one item in the list, it should be happen on any event, means on click of any button or on load of particular page:

    1. If you want to add item on click of particular button from Content Pgae, then add function call on click of button:

    onclick="CreateListItem('Title1');"

    2. On load of any content page or every page, then add the code in content editior webpart or in JS file, where you defined the CreateListItem fucntion:

    function CallFunctionsOnLoad() {
    //Create item in the list
    CreateListItem('Title1');
    } 
    //Call function on window onload event
    window.onload = CallFunctionsOnLoad; 


    Thanks, Amit Kumar, LinkedIn Profile ** My Blog

    Tuesday, April 23, 2013 5:54 AM