前段时间做一个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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
return this.core.executeRequest(this.serviceUrl, action, packet, params);
}
this.getListItems = function(listName, viewName, query, viewFields, rowLimit, queryOptions, webID)
{
if (queryOptions == null || queryOptions == '') queryOptions = '
var action = 'http://schemas.microsoft.com/sharepoint/soap/GetListItems';
var params = [listName, viewName, query, viewFields, rowLimit, queryOptions, webID];
var packet = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
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 = '
return this.core.executeRequest(this.serviceUrl, action, packet, params);
}
this.callUpdateListItems = function(listName, fields, command, rootFolder)
{
var batch;
var itemArray;
batch = "
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 += "
for (att in itemArray[i])
{
batch += "
}
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 = "
if (rootFolder != null)
{
batch += " RootFolder='" + rootFolder + "'";
}
batch += ">";
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 = '
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 = '
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 = '
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 '
'
+
id
+
'
'
,
//
query
13 '
'
,
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 }
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 '
13 '
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 }