现在的位置: 首页 > 综合 > 正文

Ajax post data to sharepoint web services

2013年09月01日 ⁄ 综合 ⁄ 共 14463字 ⁄ 字号 评论关闭

I've worked on a project which needed silverlight to run inside sharepoint these days. One of the requirements was that the silverlight app upload image to sharepoint. So i have to call webservice of sharepoint in silverlight to save the image into document library of MOSS. Follow codes my colleague sent to me was about how to call webservice by using javascript. But i haven't try it!

<div id="MeetingAgendaResult" style="width:100%;"></div>
<xml id="xmlMeetingWorkspaces"></xml>
<script type="text/javascript" language="javascript">
  var xmlhttp; 
  var tempCurrentUrl;
  var MeetingSeries;
  var innerHtml = "";
  var htmlString = new Array();
  var perDate = new Array();
  function GetMeetingWorkspaces(currentSiteUrl)
  {
   var xmlToSend = "<?xml version='1.0' encoding='utf-8'?>";
   xmlToSend += "<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/'>";
   xmlToSend += "  <soap:Body>";
   xmlToSend += "    <GetMeetingWorkspaces xmlns='http://schemas.microsoft.com/sharepoint/soap/meetings/'>";
   //xmlToSend += "      <recurring>true</recurring>";
   xmlToSend += "    </GetMeetingWorkspaces>";
   xmlToSend += "  </soap:Body>";
   xmlToSend += "</soap:Envelope>";

   var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
   xmldoc.loadXML(xmlToSend);
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   xmlhttp.onreadystatechange = state_Change_GetMeetingWorkspaces;
   xmlhttp.open("POST", currentSiteUrl +"/_vti_bin/Meetings.asmx", false);
   xmlhttp.setRequestHeader ("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/meetings/GetMeetingWorkspaces");
   xmlhttp.setRequestHeader ("Content-Type", "application/soap+xml; charset=utf-8");
   xmlhttp.send(xmldoc);
  }
  function state_Change_GetMeetingWorkspaces()
  {
      // if xmlhttp shows "loaded"
      if (xmlhttp.readyState==4)
      {
          // if "OK"
          if (xmlhttp.status==200)
          {
           var xmlDoc = document.getElementById("xmlMeetingWorkspaces");
           xmlDoc.loadXML(xmlhttp.responseXML.xml);
           var MeetingWorkspaces = xmlDoc.documentElement.childNodes(0).childNodes(0).childNodes(0).childNodes(0);
           var len = MeetingWorkspaces.childNodes.length;
           var meetingworkspaces = new Array();
           for(var i=0; i<len; i++)
           {
            meetingworkspaces[i] = MeetingWorkspaces.childNodes[i].getAttribute('Url');
            GetListItems_MeetingSeries(meetingworkspaces[i]);
           }
           //for(var i=0; i<htmlString.length; i++)
           //{
           // innerHtml += htmlString[i];
           //}

           var per = new Array();
           for(var i=0; i< perDate.length; i++)
           {
            per[i] = perDate[i];
           }
           BubbleSortForDate(per);
           var currentDate = "";
           for(var i=0; i<per.length; i++)
           {
            if(currentDate == "")
            {
             currentDate = per[per.length-1-i];
             for(var k=0; k< perDate.length; k++)
             {
              if(perDate[k] == currentDate)
              {
               innerHtml += htmlString[k];
              }
             }
            }
            else if(currentDate != "" && currentDate.split(" ")[0] == per[per.length-1-i].split(" ")[0])
            {
             for(var k=0; k< perDate.length; k++)
             {
              if(perDate[k] == per[per.length-1-i])
              {
               innerHtml += htmlString[k];
              }
             }

            }
           }
           document.getElementById("MeetingAgendaResult").innerHTML = innerHtml;
          }
          else
          {
              alert("Problem retrieving XML data");
              document.getElementById("MeetingAgendaResult").innerText = xmlhttp.responseXML.xml;
          }
      }
  }
  function BubbleSortForDate(arr)
  {
      for(var i=0;i<arr.length;i++)
      {
          for(var j=i;j<arr.length;j++)
          {
              if(arr[i].split(" ")[0]<arr[j].split(" ")[0])
              {
                  var temp=arr[i];
                  arr[i]=arr[j];
                  arr[j]=temp;
              }
          }
      }
  }
  function GetListItems_MeetingSeries(currentSiteUrl)
  {
   tempCurrentUrl = currentSiteUrl;
   var xmlToSend = "<?xml version='1.0' encoding='utf-8'?>";
   xmlToSend += "<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/'>";
   xmlToSend += "  <soap:Body>";
   xmlToSend += "    <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>";
   xmlToSend += "      <listName>Meeting Series</listName>";
   xmlToSend += "    </GetListItems>";
   xmlToSend += "  </soap:Body>";
   xmlToSend += "</soap:Envelope>";

   var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
   xmldoc.loadXML(xmlToSend);
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   xmlhttp.onreadystatechange = state_Change_GetListItems_MeetingSeries;
   xmlhttp.open("POST", currentSiteUrl + "/_vti_bin/Lists.asmx", false);
   xmlhttp.setRequestHeader ("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");
   xmlhttp.setRequestHeader ("Content-Type", "application/soap+xml; charset=utf-8");
   xmlhttp.send(xmldoc);
  }
  function state_Change_GetListItems_MeetingSeries()
  {
      // if xmlhttp shows "loaded"
      if (xmlhttp.readyState==4)
      {
          // if "OK"
          if (xmlhttp.status==200)
          {
           var xmlDoc = document.getElementById("xmlMeetingWorkspaces");
           xmlDoc.loadXML(xmlhttp.responseXML.xml);
           var MeetingWorkspaces = xmlDoc.documentElement.childNodes(0).childNodes(0).childNodes(0).childNodes(0).childNodes(0);
           var len = MeetingWorkspaces.childNodes.length;
           var currentDate = new Date();
           var currentMonth = currentDate.getMonth()+1;
           if(currentMonth < 10){currentMonth = "0" + currentMonth;}
           var currentDay = currentDate.getDate();
           if(currentDay < 10){currentDay = "0" + currentDay;}
           
           var currentDateString = currentDate.getYear() +"-"+ currentMonth +"-"+ currentDay + " 10:00:00";
           //var currentDateString = "2008-01-14 10:00:00";
     MeetingSeries = new Array();
           for(var i=0; i<len; i++)
           {
            var InstanceID = MeetingWorkspaces.childNodes[i].getAttribute('ows_InstanceID');
            var EventDate = MeetingWorkspaces.childNodes[i].getAttribute('ows_EventDate');
            var Title = MeetingWorkspaces.childNodes[i].getAttribute('ows_Title');
            if(EventDate > currentDateString)
            {
             MeetingSeries[MeetingSeries.length] = InstanceID +"|"+ EventDate +"|"+ Title;
            }
           }
           if(MeetingSeries.length > 0)
           {
            BubbleSort(MeetingSeries);
            SetInstanceID(tempCurrentUrl,MeetingSeries[MeetingSeries.length-1].split("|")[0]);
           }
          }
          else
          {
              alert("Problem retrieving XML data");
              document.getElementById("MeetingAgendaResult").innerText = xmlhttp.responseXML.xml;
          }
      }
  }
  
  function GetListItems_Agenda(currentSiteUrl)
  {
   var xmlToSend = "<?xml version='1.0' encoding='utf-8'?>";
   xmlToSend += "<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/'>";
   xmlToSend += "  <soap:Body>";
   xmlToSend += "    <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>";
   xmlToSend += "      <listName>Agenda</listName>";
   xmlToSend += "    </GetListItems>";
   xmlToSend += "  </soap:Body>";
   xmlToSend += "</soap:Envelope>";

   var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
   xmldoc.loadXML(xmlToSend);
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   xmlhttp.onreadystatechange = state_Change_GetListItems_Agenda;
   xmlhttp.open("POST", currentSiteUrl + "/_vti_bin/Lists.asmx", false);
   xmlhttp.setRequestHeader ("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");
   xmlhttp.setRequestHeader ("Content-Type", "application/soap+xml; charset=utf-8");
   xmlhttp.send(xmldoc);
  }

  function state_Change_GetListItems_Agenda()
  {
      // if xmlhttp shows "loaded"
      if (xmlhttp.readyState==4)
      {
          // if "OK"
          if (xmlhttp.status==200)
          {
              var datetimeString = MeetingSeries[MeetingSeries.length-1].split("|")[1];
              var dateString = datetimeString.split(" ");
              var dateArray = dateString[0].split("-");
              var date = new Date();
              date.setYear(dateArray[0]);
              date.setMonth(dateArray[1]-1);
              date.setDate(dateArray[2]);
             
              perDate[perDate.length] = datetimeString;
              var currentHtml = "";
             
              currentHtml += "<table width='100%' border='1' cellpadding='0' cellspacing='0' bordercolor='#FAFAFA'><tr>";
           currentHtml += "    <td height='32' bgcolor='#f2f2f2'>&nbsp;&nbsp;<strong>"+ MeetingSeries[MeetingSeries.length-1].split("|")[2] +"</strong><br><font size='1'>&nbsp;&nbsp;"+date.toDateString()+"</font>";
           currentHtml += "    </td>";
           currentHtml += "  </tr>";
           var xmlDoc = document.getElementById("xmlMeetingWorkspaces");
           xmlDoc.loadXML(xmlhttp.responseXML.xml);
           var agenda = xmlDoc.documentElement.childNodes(0).childNodes(0).childNodes(0).childNodes(0).childNodes(0);
           var len = agenda.childNodes.length;
           currentHtml += "<tr><td><table width='95%' border='1' align='center' cellpadding='3' cellspacing='0' bordercolor='#000000' bgcolor='#FFFFFF'>"
              currentHtml += "  <tr>"
              currentHtml += "    <td bgcolor='#CCCCCC' width='20%'><div align='center'><strong>Time</strong></div></td>"
              currentHtml += "    <td bgcolor='#CCCCCC' width='10%'><div align='center'><strong>Duration</strong></div></td>"
              currentHtml += "    <td bgcolor='#CCCCCC' width='40%'><div align='center'><strong>Topic</strong></div></td>"
              currentHtml += "    <td bgcolor='#CCCCCC' width='30%'><div align='center'><strong>Owner</strong></div></td>"
              currentHtml += "  </tr>";
           for(var i=0; i<len; i++)
           {
            var Time = agenda.childNodes[i].getAttribute('ows_Time');
            var Duration= GetDurationFromTime(Time);
            var Topic = agenda.childNodes[i].getAttribute('ows_Title');
            var Owner= agenda.childNodes[i].getAttribute('ows_Owner');
            currentHtml += "<tr>"
               currentHtml += "    <td><div align='center' width='20%'>"+Time+"</div></td>"
               currentHtml += "    <td><div align='center' width='10%'>"+Duration+"</div></td>"
               currentHtml += "    <td><div align='left' width='40%'><strong>"+Topic+"</strong></div></td>"
               currentHtml += "    <td><div align='center' width='30%'>"+Owner+"</div></td>"
               currentHtml += "</tr>";
           }
           currentHtml += "</table></td></tr>";
           currentHtml += "</table>";
           
           htmlString[htmlString.length] = currentHtml;
          }
          else
          {
              alert("Problem retrieving XML data");
              document.getElementById("MeetingAgendaResult").innerText = xmlhttp.responseXML.xml;
          }
      }
  }
  function SetInstanceID(currentSiteUrl, instanceId)
  {
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   xmlhttp.onreadystatechange = state_Change_SetInstanceID;
   xmlhttp.open("POST", currentSiteUrl + "/default.aspx?InstanceID=" + instanceId, false);
   xmlhttp.send("");
  }
  function state_Change_SetInstanceID()
  {
      // if xmlhttp shows "loaded"
      if (xmlhttp.readyState==4)
      {
          // if "OK"
          if (xmlhttp.status==200)
          {
              GetListItems_Agenda(tempCurrentUrl);
          }
          else
          {
              alert("Problem retrieving XML data");
              document.getElementById("MeetingAgendaResult").innerText = xmlhttp.responseXML.xml;
          }
      }
  }
  function GetDurationFromTime(time)
  {
   var timeSplitArray = time.split("-");
   if(timeSplitArray.length != 2){ return "time format error!";}
   
   var startTimeArray = timeSplitArray[0].split(":");
   if(startTimeArray.length != 2){ return "time format error!";}
   var startHour = startTimeArray[0];
   var startMinutes = startTimeArray[1];
   
   var endTimeArray = timeSplitArray[1].split(":");
   if(endTimeArray.length != 2){ return "time format error!";}
   var endtHour = endTimeArray[0];
   var endMinutes = endTimeArray[1];
   if(startHour == endtHour)
   {
    return (endMinutes - (-startMinutes)) + " min";
   }
   else if(parseInt(startHour) < parseInt(endtHour))
   {
    return ((endtHour-startHour)*60 - startMinutes - (-endMinutes)) + " min";
   }
   else
   {
    return "time logic error!";
   }
  }
  function GetServerUrl()
  {
   var currentUrl = window.location.href;
   var lastIndex0 = currentUrl.lastIndexOf("/");
   var frontString0 = currentUrl.substr(0,lastIndex0);
   var lastIndex1 = frontString0.lastIndexOf("/");
   var frontString1 = frontString0.substr(lastIndex1+1); 
   if(frontString1.toUpperCase() == "PAGES") 
   {
    return currentUrl.substr(0,lastIndex1);
   }
   else
   {
    return currentUrl.substr(0,lastIndex0);
   }
  }
  function BubbleSort(arr)
  {
      for(var i=0;i<arr.length;i++)
      {
          for(var j=i;j<arr.length;j++)
          {
              if(arr[i].split("|")[1]<arr[j].split("|")[1])
              {
                  var temp=arr[i];
                  arr[i]=arr[j];
                  arr[j]=temp;
              }
          }
      }
  }

  function a(currentSiteUrl)
  {
   var xmlToSend = "<?xml version='1.0' encoding='utf-8'?>";
   xmlToSend += "<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/'>";
   xmlToSend += "  <soap:Body>";
   xmlToSend += "    <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>";
   xmlToSend += "      <listName>MBD Calendar</listName>";
   xmlToSend += "    </GetListItems>";
   xmlToSend += "  </soap:Body>";
   xmlToSend += "</soap:Envelope>";
alert(xmlToSend);
   var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
   xmldoc.loadXML(xmlToSend);
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   xmlhttp.onreadystatechange = ab;
   xmlhttp.open("POST", currentSiteUrl + "/_vti_bin/Lists.asmx", false);
   xmlhttp.setRequestHeader ("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");
   xmlhttp.setRequestHeader ("Content-Type", "application/soap+xml; charset=utf-8");
   xmlhttp.send(xmldoc);
  }
  function ab()
  {
      // if xmlhttp shows "loaded"
      if (xmlhttp.readyState==4)
      {
          // if "OK"
          if (xmlhttp.status==200)
          {
           var xmlDoc = document.getElementById("xmlMeetingWorkspaces");
           xmlDoc.loadXML(xmlhttp.responseXML.xml);
           document.getElementById("MeetingAgendaResult").innerText = xmlhttp.responseXML.xml;
          }
          else
          {
              alert("Problem retrieving XML data");
              document.getElementById("MeetingAgendaResult").innerText = xmlhttp.responseXML.xml;
          }
      }
  }
  a('http://businsighttest/MBD');

  //GetMeetingWorkspaces(GetServerUrl());
 </script>

抱歉!评论已关闭.