JavaScript 调用WSS

前段时间做一个MOSS的项目,boss要求在一个List Item新建的页面上增加用户身份验证,符合条件的用户可以进入,不符合条件的用户转到Accessdenied页面。因为整个项目建立在MOSS平台上,并且客户不允许在服务器上部署dll,所以不能写C#代码,所有的前台展示都需要在脚本中来完成(真是变态啊)。

    好在WSS给我们提供了内容丰富的WebService,我们只需要通过js去调用就行了,这跟使用Ajax基本没什么区别。虽然需求很简单,通过js调用Lists.GetListItems()方法,查找User Information List中的用户,然后再加一些判断就可以了,但是我想到在js中调用WSS提供的方法也是一个会经常使用到的东西,所以在这里整理了一下代码,也作为一个js调用WSS的通用代码,以后直接拿过来用就可以了。

    首先是核心部分,这跟Ajax的核心代码基本相同,都是先构造一个XMLHttpRequest或者ActiveXObject对象,然后就是XML的发送和接收。


function SPAPI_Core()
{
   
this.createXMLHttpRequest = function()
    {
       
if (typeof XMLHttpRequest != "undefined"
        {
           
return new XMLHttpRequest();
        } 
       
else if (typeof ActiveXObject != "undefined"
        {
           
return new ActiveXObject("Microsoft.XMLHTTP");
        } 
       
else 
        {
           
throw new Error("XMLHttpRequest not supported");
        }
    }
    
   
this.executeRequest = function(serviceUrl, action, packet, params)
    {
       
var oXMLHttpRequest = this.createXMLHttpRequest();
       
var result = null;
       
var resultName;
        
        oXMLHttpRequest.open(
"POST", serviceUrl, false);
        oXMLHttpRequest.setRequestHeader(
"Content-Type", "text/xml; charset=utf-8"); 
        oXMLHttpRequest.setRequestHeader(
"SOAPAction", action);
        
       
if (params != null)
        {
           
for (var i=0; i < params.length; i++)
            {
                packet
= packet.replace('{' + i.toString() + '}', (params[i] == null ? '' : params[i]));
            }
        }
        
        oXMLHttpRequest.send(packet);
        
        resultName
= action.substring(action.lastIndexOf('/') + 1) + 'Result';
       
var resBatch;
       
var status;
       
var statusText;
        
        status
= oXMLHttpRequest.status;
        statusText
= oXMLHttpRequest.statusText;
        
       
if (status == 200)
        {
           
// Check for SharePoint error code
            resBatch = oXMLHttpRequest.responseXML.getElementsByTagName(resultName);
            
           
var codeEl = oXMLHttpRequest.responseXML.getElementsByTagName('ErrorCode');
            
           
if (codeEl != null && codeEl.length > 0)
            {
               
var spStatus = parseInt(codeEl[0].childNodes[0].nodeValue);
                
               
if (spStatus != 0)
                {
                    status
= 0-spStatus; // Note we make this -ve to prevent confusion with the HTTP code
                    
                   
var messageEl = oXMLHttpRequest.responseXML.getElementsByTagName('ErrorText');
                   
if (messageEl != null && messageEl.length >= 0)
                    {
                        statusText
= messageEl[0].childNodes[0].nodeValue;
                    }
                }
            }
        }
            
        result
= {
        status : status,
        statusText : statusText,
        responseXML : oXMLHttpRequest.responseXML,
        responseText : oXMLHttpRequest.responseText,
        resultNode : (resBatch
== null || resBatch.length == 0 ? null : resBatch[0])
        };
        
       
return result;
    }
}

    然后就是写WSS中各个方法的调用接口。WSS中提供的方法特别多,这里只列出了常用的一部分,以后遇到其它的可以类似再添加。

Lists类下的所有方法


function SPAPI_Lists(baseUrl)
{
   
this.core = new SPAPI_Core();
   
this.serviceUrl = baseUrl + '/_vti_bin/lists.asmx';
    
   
/* List template IDs */
   
this.LIST_ID_ADMIN_TASKS        = 1200    // Administrator tasks list
    this.LIST_ID_ANNOUNCEMENTS      = 104     // Announcements list
    this.LIST_ID_BLOG_CATEGORIES    = 303     // Blog Categories list
    this.LIST_ID_BLOG_COMMENTS      = 302     // Blog Comments list
    this.LIST_ID_BLOG_POSTS         = 301     // Blog Posts list
    this.LIST_ID_CONTACTS           = 105     // Contacts list
    this.LIST_ID_CUSTOM_GRID        = 120     // Custom grid for a list
    this.LIST_ID_CUSTOM_WORKFLOW    = 118     // Custom Workflow Process
    this.LIST_ID_DATA_CONNECTIONS   = 130     // Data Connection library
    this.LIST_ID_SATA_SOURCES       = 110     // Data sources
    this.LIST_ID_DISCUSSION_BORAD   = 108     // Discussion board
    this.LIST_ID_DOCUMENT_LIBRARY   = 101     // Document library
    this.LIST_ID_EVENTS             = 106     // Events list
    this.LIST_ID_GANTT_TASKS        = 150     // Gantt Tasks list
    this.LIST_ID_GENERIC            = 100     // Generic list
    this.LIST_ID_ISSUE_TRACKING     = 1100    // Issue tracking
    this.LIST_ID_LINKS              = 103     // Links list
    this.LIST_ID_LIST_TEMPLATE      = 114     // List template gallery
    this.LIST_ID_MASTER_PAGE        = 116     // Master pages gallery
    this.LIST_ID_MEETING_AGENDA     = 201     // Meeting Agenda list
    this.LIST_ID_MEETING_ATTENDEES  = 202     // Meeting Attendees list
    this.LIST_ID_MEETING_DECISIONS  = 204     // Meeting Decisions list
    this.LIST_ID_MEETING_OBJECTIVES = 207     // Meeting Objectives list
    this.LIST_ID_MEETING_SERIES     = 200     // Meeting Series list
    this.LIST_ID_MEETING_TEXT_BOX   = 210     // Meeting text box
    this.LIST_ID_MEETING_TTB        = 211     // Meeting Things To Bring list
    this.LIST_ID_MEETING_WS_PAGES   = 212     // Meeting Workspace Pages list
    this.LIST_ID_NO_CODE_WORKLOFWS  = 117     // No-Code Workflows
    this.LIST_ID_PERSONAL_DOCLIB    = 2002    // Personal document library
    this.LIST_ID_PICTURE_LIBRARY    = 109     // Picture library
    this.LIST_ID_PORTAL_SITE_LIST   = 300     // Portal Sites list
    this.LIST_ID_PRIVATE_DOCLIB     = 2003    // Private document library
    this.LIST_ID_SITE_TEMPLATES     = 111     // Site template gallery
    this.LIST_ID_SURVEY             = 102     // Survey
    this.LIST_ID_TASKS              = 107     // Tasks list
    this.LIST_ID_USER_INFO          = 112     // User Information list
    this.LIST_ID_WEB_PARTS          = 113     // Web Part gallery
    this.LIST_ID_WIKI_PAGES         = 119     // Wiki Page library
    this.LIST_ID_WORKFLOW_HISTORY   = 140     // Workflow History
    this.LIST_ID_XML_FORMS          = 115     // XML Form library
    /*-------------------*/
    
   
this.addAttachment = function(listName, listItemID, fileName, attachment)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/AddAttachment';
           
var params = [listName, listItemID, fileName, attachment];
           
var packet = '<?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><AddAttachment xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><listItemID>{1}</listItemID><fileName>{2}</fileName><attachment>{3}</attachment></AddAttachment></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.addDiscussionBoardItem = function(listName, message)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/AddDiscussionBoardItem';
           
var params = [listName, message];
           
var packet = '<?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><AddDiscussionBoardItem xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><message>{1}</message></AddDiscussionBoardItem></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.addList = function(listName, description, templateID)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/AddList';
           
var params = [listName, description, templateID];
           
var packet = '<?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>{0}</listName><description>{1}</description><templateID>{2}</templateID></AddList></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.addListFromFeature = function(listName, description, featureID, templateID)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/AddListFromFeature';
           
var params = [listName, description, featureID, templateID];
           
var packet = '<?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><AddListFromFeature xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><description>{1}</description><featureID>{2}</featureID><templateID>{3}</templateID></AddListFromFeature></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.applyContentTypeToList = function(webUrl, contentTypeId, listName)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/ApplyContentTypeToList';
           
var params = [webUrl, contentTypeId, listName];
           
var packet = '<?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><ApplyContentTypeToList xmlns="http://schemas.microsoft.com/sharepoint/soap/"><webUrl>{0}</webUrl><contentTypeId>{1}</contentTypeId><listName>{2}</listName></ApplyContentTypeToList></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.checkInFile = function(pageUrl, comment, checkinType)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/CheckInFile';
           
var params = [pageUrl, comment, checkinType];
           
var packet = '<?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><CheckInFile xmlns="http://schemas.microsoft.com/sharepoint/soap/"><pageUrl>{0}</pageUrl><comment>{1}</comment><CheckinType>{2}</CheckinType></CheckInFile></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.checkOutFile = function(pageUrl, checkoutToLocal, lastmodified)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/CheckOutFile';
           
var params = [pageUrl, checkoutToLocal, lastmodified];
           
var packet = '<?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><CheckOutFile xmlns="http://schemas.microsoft.com/sharepoint/soap/"><pageUrl>{0}</pageUrl><checkoutToLocal>{1}</checkoutToLocal><lastmodified>{2}</lastmodified></CheckOutFile></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.createContentType = function(listName, displayName, parentType, fields, contentTypeProperties, addToView)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/CreateContentType';
           
var params = [listName, displayName, parentType, fields, contentTypeProperties, addToView];
           
var packet = '<?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><CreateContentType xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><displayName>{1}</displayName><parentType>{2}</parentType><fields>{3}</fields><contentTypeProperties>{4}</contentTypeProperties><addToView>{5}</addToView></CreateContentType></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.deleteAttachment = function(listName, listItemID, url)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/DeleteAttachment';
           
var params = [listName, listItemID, url];
           
var packet = '<?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><DeleteAttachment xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><listItemID>{1}</listItemID><url>{2}</url></DeleteAttachment></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.deleteContentType = function(listName, contentTypeId)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/DeleteContentType';
           
var params = [listName, contentTypeId];
           
var packet = '<?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><DeleteContentType xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><contentTypeId>{1}</contentTypeId></DeleteContentType></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.deleteContentTypeXmlDocument = function(listName, contentTypeId, documentUri)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/DeleteContentTypeXmlDocument';
           
var params = [listName, contentTypeId, documentUri];
           
var packet = '<?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><DeleteContentTypeXmlDocument xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><contentTypeId>{1}</contentTypeId><documentUri>{2}</documentUri></DeleteContentTypeXmlDocument></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.deleteList = function(listName)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/DeleteList';
           
var params = [listName];
           
var packet = '<?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><DeleteList xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName></DeleteList></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.getAttachmentCollection = function(listName, listItemID)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/GetAttachmentCollection';
           
var params = [listName, listItemID];
           
var packet = '<?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><GetAttachmentCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><listItemID>{1}</listItemID></GetAttachmentCollection></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.getList = function(listName)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/GetList';
           
var params = [listName];
           
var packet = '<?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><GetList xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName></GetList></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.getListAndView = function(listName, viewName)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/GetListAndView';
           
var params = [listName, viewName];
           
var packet = '<?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><GetListAndView xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><viewName>{1}</viewName></GetListAndView></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.getListCollection = function()
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/GetListCollection';
           
var params = [];
           
var packet = '<?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><GetListCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/" /></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.getListContentType = function(listName, contentTypeId)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/GetListContentType';
           
var params = [listName, contentTypeId];
           
var packet = '<?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><GetListContentType xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><contentTypeId>{1}</contentTypeId></GetListContentType></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.getListContentTypes = function(listName, contentTypeId)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/GetListContentTypes';
           
var params = [listName, contentTypeId];
           
var packet = '<?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><GetListContentTypes xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><contentTypeId>{1}</contentTypeId></GetListContentTypes></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }

     
this.getListItemChanges = function(listName, viewFields, since, contains)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/GetListItemChanges';
           
var params = [listName, viewFields, since, contains];
           
var packet = '<?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><GetListItemChanges xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><viewFields>{1}</viewFields><since>{2}</since><contains>{3}</contains></GetListItemChanges></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.getListItemChangesSinceToken = function(listName, viewName, query, viewFields, rowLimit, queryOptions, changeToken, contains)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/GetListItemChangesSinceToken';
           
var params = [listName, viewName, query, viewFields, rowLimit, queryOptions, changeToken, contains];
           
var packet = '<?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><GetListItemChangesSinceToken xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><viewName>{1}</viewName><query>{2}</query><viewFields>{3}</viewFields><rowLimit>{4}</rowLimit><queryOptions>{5}</queryOptions><changeToken>{6}</changeToken><contains>{7}</contains></GetListItemChangesSinceToken></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.getListItems = function(listName, viewName, query, viewFields, rowLimit, queryOptions, webID)
      {
         
if (queryOptions == null || queryOptions == '') queryOptions = '<QueryOptions/>';
          
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/GetListItems';
           
var params = [listName, viewName, query, viewFields, rowLimit, queryOptions, webID];
           
var packet = '<?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><GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><viewName>{1}</viewName><query>{2}</query><viewFields>{3}</viewFields><rowLimit>{4}</rowLimit><queryOptions>{5}</queryOptions><webID>{6}</webID></GetListItems></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.getVersionCollection = function(strlistID, strlistItemID, strFieldName)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/GetVersionCollection';
           
var params = [strlistID, strlistItemID, strFieldName];
           
var packet = '<?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><GetVersionCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/"><strlistID>{0}</strlistID><strlistItemID>{1}</strlistItemID><strFieldName>{2}</strFieldName></GetVersionCollection></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.undoCheckOut = function(pageUrl)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/UndoCheckOut';
           
var params = [pageUrl];
           
var packet = '<?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><UndoCheckOut xmlns="http://schemas.microsoft.com/sharepoint/soap/"><pageUrl>{0}</pageUrl></UndoCheckOut></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.updateContentType = function(listName, contentTypeId, contentTypeProperties, newFields, updateFields, deleteFields, addToView)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/UpdateContentType';
           
var params = [listName, contentTypeId, contentTypeProperties, newFields, updateFields, deleteFields, addToView];
           
var packet = '<?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><UpdateContentType xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><contentTypeId>{1}</contentTypeId><contentTypeProperties>{2}</contentTypeProperties><newFields>{3}</newFields><updateFields>{4}</updateFields><deleteFields>{5}</deleteFields><addToView>{6}</addToView></UpdateContentType></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.updateContentTypeXmlDocument = function(listName, contentTypeId, newDocument)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/UpdateContentTypeXmlDocument';
           
var params = [listName, contentTypeId, newDocument];
           
var packet = '<?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><UpdateContentTypeXmlDocument xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><contentTypeId>{1}</contentTypeId><newDocument>{2}</newDocument></UpdateContentTypeXmlDocument></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.updateContentTypeXmlDocument = function(listName, contentTypeId, newDocument)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/UpdateContentTypeXmlDocument';
           
var params = [listName, contentTypeId, newDocument];
           
var packet = '<?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><UpdateContentTypeXmlDocument xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName><contentTypeId>{1}</contentTypeId><newDocument>{2}</newDocument></UpdateContentTypeXmlDocument></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.updateList = function(listName, listProperties, newFields, updateFields, deleteFields, listVersion)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/UpdateList';
           
var params = [listName, listProperties, newFields, updateFields, deleteFields, listVersion];
           
var packet = '<?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>{0}</listName><listProperties>{1}</listProperties><newFields>{2}</newFields><updateFields>{3}</updateFields><deleteFields>{4}</deleteFields><listVersion>{5}</listVersion></UpdateList></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
     
this.updateListItems = function(listName, updates)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/UpdateListItems';
           
var params = [listName, updates];
           
var packet = '<?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>{0}</listName><updates>{1}</updates></UpdateListItems></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
   
this.callUpdateListItems = function(listName, fields, command, rootFolder)
      {
         
var batch;
         
var itemArray;
          
          batch
= "<Batch OnError='Continue'"
          
         
if (rootFolder != null)
          {
              batch
+= " RootFolder='" + rootFolder + "'";
          }
          
          batch
+= ">";
          
         
if (fields.constructor != Array)
          {
              itemArray
= [ fields ];
          }
         
else
          {
              itemArray
= fields;
          }
          
         
for (var i=0; i<itemArray.length; i++)
          {
              batch
+= "<Method ID='1' Cmd='" + command + "'>";
          
             
for (att in itemArray[i])
              {
                  batch
+= "<Field Name='" + att + "'><![CDATA[" + itemArray[i][att] + "]]></Field>";
              }
          
              batch
+= "</Method>";
          }
          
          batch
+= "</Batch>";
          
         
return this.updateListItems(listName, batch);
      }
      
     
this.quickAddListItem = function(listName, fields, rootFolder)
      {
         
return this.callUpdateListItems(listName, fields, "New", rootFolder);
      }
      
   
this.quickUpdateListItem = function(listName, fields)
      {
         
return this.callUpdateListItems(listName, fields, "Update");
      }
      
     
this.quickDeleteListItem = function(listName, itemIds)
      {
         
var idFields = [ ];
          
         
if (itemIds.constructor == Array)
          {
             
for (var i=0; i<itemIds.length; i++)
              {
                  idFields.push( { ID: itemIds[i] } );
              }
          }
         
else
          {
              idFields
= [ { ID: itemIds } ];
          }
          
         
return this.callUpdateListItems(listName, idFields, "Delete");
      }
      
     
this.callFolderUpdate = function(listName, folderName, command, rootFolder)
      {
         
var batch;
          
          batch
= "<Batch OnError='Continue'"
          
         
if (rootFolder != null)
          {
              batch
+= " RootFolder='" + rootFolder + "'";
          }
          
          batch
+= ">";
          
        batch
+= "<Method ID='1' Cmd='" + command + "'>"
       
+"<Field Name='FSObjType'>1</Field>"
       
+"<Field Name='BaseName'>" + folderName + "</Field>"
       
+"</Method>"
       
+"</Batch>";
        
         
return this.updateListItems(listName, batch);
      }
      
     
this.createFolder = function(listName, folderName, rootFolder)
      {
         
return this.callFolderUpdate(listName, folderName, "New", rootFolder);
      }
}

UserGroup类下的部分常用方法


function SPAPI_UserGroup(baseUrl)
{
   
this.core = new SPAPI_Core();
   
this.serviceUrl = baseUrl + '/_vti_bin/UserGroup.asmx';   
      
     
this.getGroupCollectionFromUser = function(userLoginName)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/directory/GetGroupCollectionFromUser';
           
var params = [userLoginName];
           
var packet = '<?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><GetGroupCollectionFromUser xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/"><userLoginName>{0}</userLoginName></GetGroupCollectionFromUser></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }     
      
            
     
this.getUserCollectionFromGroup = function(groupName)
      {
           
var action = 'http://schemas.microsoft.com/sharepoint/soap/directory/GetUserCollectionFromGroup';
           
var params = [groupName];
           
var packet = '<?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><GetUserCollectionFromGroup xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/"><groupName>{0}</groupName></GetUserCollectionFromGroup></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
      
     
this.getUserInfo = function(userLoginName)
      {
          
var action = 'http://schemas.microsoft.com/sharepoint/soap/directory/GetUserInfo';
           
var params = [userLoginName];
           
var packet = '<?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><GetUserInfo xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/"><userLoginName>{0}</userLoginName></GetUserInfo></soap:Body></soap:Envelope>';

           
return this.core.executeRequest(this.serviceUrl, action, packet, params);
      }
      
}
    顺便给出一个调用的例子:
 1  var  rootUrl  =  window.location.protocol  +   " // "   +  window.location.host;
 2  var  contractId  =  getUrlParam( " ID " );
 3  var  contractName  =  getContractNameById(rootUrl, contractId);
 4  alert(contractName);
 5 
 6  function  getContractNameById(baseUrl, id)
 7  {
 8       var  lists  =   new  SPAPI_Lists(baseUrl);
 9       var  items  =  lists.getListItems(
10       ' Contract ' ,
11       '' ,
12       ' <Query><Where><Eq><FieldRef Name="ID"/><Value Type="Counter"> '   +  id  +   ' </Value></Eq></Where></Query> ' ,   //  query
13       ' <ViewFields><FieldRef Name="Title"/></ViewFields> ' ,
14       1 ,   //  rowLimit
15       ''    //  queryOptions
16      );
17 
18       if  (items.status  ==   200 )
19      {
20           var  rows  =  items.responseXML.getElementsByTagName( ' z:row ' );
21 
22           if  (rows.length  ==   1 )
23          {
24               return  rows[ 0 ].getAttribute( ' ows_Title ' );
25          }
26           else
27          {
28               return   null ;
29          }
30      }
31       else
32      {
33           return   null ;
34      }
35  }
36 
37  function  getUrlParam(name) 
38  {
39       var  query  =  window.location.search.substring( 1 );
40       var  pairs  =  query.split( " & " );
41       var  argname  =   "" ;
42       var  val  =   "" ;
43       for  ( var  i  =   0 ; i  <  pairs.length; i ++
44      {
45           var  pos  =  pairs[i].indexOf( ' = ' );
46           if  (pos  ==   - 1
47          {
48               continue ;
49          }
50          argname  =  pairs[i].substring( 0 , pos);
51          val  =  pairs[i].substring(pos  +   1 );
52          val  =  decodeURIComponent(val);
53           if  (argname  ==  name) 
54          {
55               return  val;
56          }
57      }
58       return   null ;
59  }

你可能感兴趣的:(JavaScript)