Beruflich Dokumente
Kultur Dokumente
http://darrenjohnstone.net/2008/07/22/examples-for-the-sharepoint...
The following examples illustrate the use of the javascript API for SharePoint and Office Live. These examples are intended to demonstrate the core uses of the API for solution developers. Get the full definition of a list [1] Get the full definition of a list and associated view [2] Get the basic detail of all lists in a site [3] Get the GUID of a list from its name [4] Querying a list using the Lists web service [5] Querying a list using the List Data Retrieval Service [6] Create a new list from an existing list template [7] Delete a list [8] Create one or more new list items [9] Update one or more list items [10] Delete one or more list items [11] Create a new folder within a list [12] Performing a keyword search [13] Performing a full text search [14]
Code sample
var lists = new SPAPI_Lists('http://localhost') var listDef = lists.getList('Pages'); if (listDef.status == 200) { // The list definition is in listDef.resultNode and is a browser specific XML document } else { alert('There was an error: ' + listDef.statusText); }
References
Lists.asmx on MSDN [16] The GetList method definition on MSDN [15]
Code sample
var lists = new SPAPI_Lists('http://localhost') var listDef = lists.getListAndView('Tasks', '{4011C1CE-A840-4BEB-89E3-A42DF3E3711E}'); if (listDef.status == 200) { // The list definition is in listDef.resultNode and is a browser specific XML document } else { alert('There was an error: ' + listDef.statusText); }
References
Lists.asmx on MSDN [16] The GetListAndView method definition on MSDN [18]
1 von 9
21.09.2009 14:44
Darrens SharePoint and .Net blog Examples for the SharePoint a...
http://darrenjohnstone.net/2008/07/22/examples-for-the-sharepoint...
Result packet from GetListAndView showing the full list and view definition [19]
Code sample
var lists = new SPAPI_Lists('http://localhost') var listCollection = lists.getListCollection(); if (listCollection.status == 200) { var items = listCollection.responseXML.getElementsByTagName('List'); alert('There are ' + items.length + ' lists in the site.'); for (var i=0; i<items.length; i++) { // Do something alert(items[i].getAttribute('Title') + ' ' + items[i].getAttribute('ID')); } } else { alert('There was an error: ' + listCollection.statusText); }
References
Lists.asmx on MSDN [16] The GetListCollection method definition on MSDN [20]
Code sample
function getListGuid(baseUrl, listName) { var res; var lists = new SPAPI_Lists(''); res = lists.getList(listName); if (res.status == 200) { var listID = -1; if (res.resultNode != null) { listID = res.resultNode.childNodes[0].getAttribute('ID'); } return { listID : listID, fullResult : res }; } else { return { listID : null, fullResult : res }; } }
References
Lists.asmx on MSDN [16] The GetList method definition on MSDN [15]
2 von 9
21.09.2009 14:44
Darrens SharePoint and .Net blog Examples for the SharePoint a...
http://darrenjohnstone.net/2008/07/22/examples-for-the-sharepoint...
The GetListItems method of the lists web service can be used to query a list using CAML query notation and return list items that match the criteria. Of the different ways to return list data this is probably the easiest and most recommended way of doing so. By default, GetListItems returns the records in a list view- either the default view or a custom view definition. In this way the filtering and ordering may be specified by the view definition. However, the method also allows the query, row limit, and list of fields to be changed allowing custom queries to be executed. See the links given in the references section for more information on the parameters of this method. Services Lists.asmx SPAPI_Core.js SPAPI_Lists.js JS API Includes Required
Code sample
// Return all items in the default view of MyList var lists = new SPAPI_Lists('http://localhost') var items = lists.getListItems('MyList'); if (items.status == 200) { var rows = items.responseXML.getElementsByTagName('z:row'); document.getElementById('output').value = items.responseText; for (var i=0; i<rows.length; i++) { // Do something with the row } } else { alert('There was an error: ' + items.statusText); } // Return the first 5 items where ID < 10. Only return the Title and ID columns and do not include mandatory columns. var lists = new SPAPI_Lists('http://localhost') var items = lists.getListItems( 'MyList', // listName '', // viewName '<Query><Where><Lt><FieldRef Name="ID"/><Value Type="Counter">10</Value></Lt></Where></Query>', // query '<ViewFields><FieldRef Name="ID"/><FieldRef Name="Title"/></ViewFields>', // viewFields 5, // rowLimit '<QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions>' // queryOptions ); if (items.status == 200) { var rows = items.responseXML.getElementsByTagName('z:row'); document.getElementById('output').value = items.responseText; for (var i=0; i<rows.length; i++) { // Do something with the row } } else { alert('There was an error: ' + items.statusText); }
References
Lists.asmx on MSDN [16] GetListItems method on MSDN [22]
Code sample
var dspsts = new SPAPI_dspsts('http://localhost'); var res = dspsts.queryRequest( "{CFD2A33E-EE89-47F3-9E28-24442E598326}", // listGuid "<Field Name='ID'/><Field Name='Title'/>", // fields "<Geq><FieldRef Name='ID' /><Value Type='Counter'>5</Value></Geq>", // where "<OrderField Name='ID' Type='xsd:int' Direction='DESC'/>", // orderBy 10 // rowLimit ); if (res.status == 200) {
3 von 9
21.09.2009 14:44
Darrens SharePoint and .Net blog Examples for the SharePoint a...
http://darrenjohnstone.net/2008/07/22/examples-for-the-sharepoint...
var rows = res.responseXML.getElementsByTagName('Row'); alert('There were ' + rows.length + ' results.'); for (var i=0; i<rows.length; i++) { var item = rows[i]; // Do something with the row alert('The page with ID ' + item.getAttribute('ID') + ' has a title of ' + item.getAttribute('Title')); } } else { alert('There was an error: ' + res.statusText); }
References
List Data Retrieval service on MSDN [24]
Code sample
var lists = new SPAPI_Lists('http://localhost') var res = lists.addList('NewTasks', 'My new task list', lists.LIST_ID_TASKS) if (res.status == 200) { alert('The new list was created.'); } else { alert('There was an error: ' + res.statusText); }
Remarks
The list template IDs you will need to use this method are defined as constants in the SPAPI_Lists class. /* List template IDs */ this.LIST_ID_ADMIN_TASKS this.LIST_ID_ANNOUNCEMENTS this.LIST_ID_BLOG_CATEGORIES this.LIST_ID_BLOG_COMMENTS this.LIST_ID_BLOG_POSTS this.LIST_ID_CONTACTS this.LIST_ID_CUSTOM_GRID this.LIST_ID_CUSTOM_WORKFLOW this.LIST_ID_DATA_CONNECTIONS this.LIST_ID_SATA_SOURCES this.LIST_ID_DISCUSSION_BORAD this.LIST_ID_DOCUMENT_LIBRARY this.LIST_ID_EVENTS this.LIST_ID_GANTT_TASKS this.LIST_ID_GENERIC this.LIST_ID_ISSUE_TRACKING this.LIST_ID_LINKS this.LIST_ID_LIST_TEMPLATE this.LIST_ID_MASTER_PAGE this.LIST_ID_MEETING_AGENDA this.LIST_ID_MEETING_ATTENDEES this.LIST_ID_MEETING_DECISIONS this.LIST_ID_MEETING_OBJECTIVES this.LIST_ID_MEETING_SERIES this.LIST_ID_MEETING_TEXT_BOX this.LIST_ID_MEETING_TTB this.LIST_ID_MEETING_WS_PAGES this.LIST_ID_NO_CODE_WORKLOFWS this.LIST_ID_PERSONAL_DOCLIB this.LIST_ID_PICTURE_LIBRARY this.LIST_ID_PORTAL_SITE_LIST this.LIST_ID_PRIVATE_DOCLIB this.LIST_ID_SITE_TEMPLATES this.LIST_ID_SURVEY this.LIST_ID_TASKS this.LIST_ID_USER_INFO this.LIST_ID_WEB_PARTS this.LIST_ID_WIKI_PAGES this.LIST_ID_WORKFLOW_HISTORY this.LIST_ID_XML_FORMS /*-------------------*/ = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 1200 104 303 302 301 105 120 118 130 110 108 101 106 150 100 1100 103 114 116 201 202 204 207 200 210 211 212 117 2002 109 300 2003 111 102 107 112 113 119 140 115 // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // Administrator tasks list Announcements list Blog Categories list Blog Comments list Blog Posts list Contacts list Custom grid for a list Custom Workflow Process Data Connection library Data sources Discussion board Document library Events list Gantt Tasks list Generic list Issue tracking Links list List template gallery Master pages gallery Meeting Agenda list Meeting Attendees list Meeting Decisions list Meeting Objectives list Meeting Series list Meeting text box Meeting Things To Bring list Meeting Workspace Pages list No-Code Workflows Personal document library Picture library Portal Sites list Private document library Site template gallery Survey Tasks list User Information list Web Part gallery Wiki Page library Workflow History XML Form library
References
Lists.asmx on MSDN [16] The AddList method definition on MSDN [25]
8- Delete a list
The DeleteList method of the lists web service may be used to delete a list and all list items. The list is moved to the recycle bin prior to being
4 von 9
21.09.2009 14:44
Darrens SharePoint and .Net blog Examples for the SharePoint a...
http://darrenjohnstone.net/2008/07/22/examples-for-the-sharepoint...
completely removed from the system. Services Lists.asmx SPAPI_Core.js SPAPI_Lists.js JS API Includes Required
Code sample
var lists = new SPAPI_Lists('http://localhost') var res = lists.deleteList('MyList'); if (res.status == 200) { alert('The list was deleted.'); } else { alert('There was an error: ' + res.statusText); }
References
Lists.asmx on MSDN [16] The DeleteList method definition on MSDN [26]
Code sample
var lists = new SPAPI_Lists('http://localhost') var res = lists.quickAddListItem('MyList', { Title: 'My new item 2', Description: 'My Description' }); if (res.status == 200) { alert('The new list item was created.'); } else { alert('There was an error: ' + res.statusText); } // Or to create multiple items.... var lists = new SPAPI_Lists('http://localhost') var res = lists.quickAddListItem('MyList', [ { Title: 'My new item 3' }, { Title: 'My new item 4' } ]); if (res.status == 200) { alert('The new list items were created.'); } else { alert('There was an error: ' + res.statusText); } // Or to create items in a folder var lists = new SPAPI_Lists('http://localhost') var res = lists.quickAddListItem('MyList', [ { Title: 'My new item 3' }, { Title: 'My new item 4' } ], '/Lists/MyList/Folder1/Folder2'); if (res.status == 200) { alert('The new list items were created.'); } else { alert('There was an error: ' + res.statusText); }
References
Lists.asmx on MSDN [16] The UpdateItems method definition on MSDN [27]
5 von 9
21.09.2009 14:44
Darrens SharePoint and .Net blog Examples for the SharePoint a...
http://darrenjohnstone.net/2008/07/22/examples-for-the-sharepoint...
Code sample
var lists = new SPAPI_Lists('http://localhost') // Update the title of the item with ID 8 var res = lists.quickUpdateListItem('MyList', { ID: 8, Title: 'Updated title 1' } ); if (res.status == 200) { alert('The list item was updated.'); } else { alert('There was an error: ' + res.statusText); } // Or to update multiple items.... var lists = new SPAPI_Lists('http://localhost') // Update the titles of the items with ID 8 and 9 var res = lists.quickUpdateListItem('MyList', [ { ID: 8, Title: 'Updated title 1' }, { ID: 9, Title: 'Updated title 2' } ]); if (res.status == 200) { alert('The list items were updated.'); } else { alert('There was an error: ' + res.statusText); }
References
Lists.asmx on MSDN [16] The UpdateItems method definition on MSDN [27]
Code sample
var lists = new SPAPI_Lists('http://localhost') var res = lists.quickDeleteListItem('MyList', 4); if (res.status == 200) { alert('The list item was deleted.'); } else { alert('There was an error: ' + res.statusText); } // Or to delete multiple items.... var lists = new SPAPI_Lists('http://localhost') var res = lists.quickDeleteListItem('MyList', [ 1, 2, 3, 4]); if (res.status == 200)
6 von 9
21.09.2009 14:44
Darrens SharePoint and .Net blog Examples for the SharePoint a...
http://darrenjohnstone.net/2008/07/22/examples-for-the-sharepoint...
{ alert('The list items were deleted.'); } else { alert('There was an error: ' + res.statusText); }
References
Lists.asmx on MSDN [16] The UpdateItems method definition on MSDN [27]
Code sample
var lists = new SPAPI_Lists('http://localhost') var res = lists.createFolder('MyList', 'TestFolder2'); if (res.status == 200) { alert('The folder was created.'); } else { alert('There was an error: ' + res.statusText); } // Or to create a folder within a folder var lists = new SPAPI_Lists('http://localhost') var res = lists.createFolder('MyList', 'TestFolder2', '/Lists/MyList/TestFolder'); if (res.status == 200) { alert('The folder was created.'); } else { alert('There was an error: ' + res.statusText); }
References
Lists.asmx on MSDN [16] The UpdateListItems method definition on MSDN [27]
Search.asmx
Code sample
var lists = new SPAPI_Search(''); // Create the new query // Type = SQL_Query_Type_Keyword // Query = Sharepoint // Fields to return = Title,Path,Description,Write,Rank,Size var query = new SPAPI_QueryPacket(SPAPI_Query_Type_Keyword, 'Sharepoint', 'Title,Path,Description,Write,Rank,Size'); // Add a new sort property - sort by ID, descending, first sort property query.addSortProperty('ID', false, 0); // Add a new sort property - sort by Title, ascending, second sort property query.addSortProperty('ID', true, 1); var res = lists.query(query.getXML()); if (res.status == 200) { // Get the response document var resultDocument = query.getResultDocument(res.responseXML);
7 von 9
21.09.2009 14:44
Darrens SharePoint and .Net blog Examples for the SharePoint a...
http://darrenjohnstone.net/2008/07/22/examples-for-the-sharepoint...
var status = resultDocument.getElementsByTagName('Response')[0].getElementsByTagName('Status')[0].childNodes[0].nodeValue; alert('The status of the search was: ' + status); if (status == 'SUCCESS') { var count = resultDocument.getElementsByTagName('Range')[0].getElementsByTagName('Count')[0].childNodes[0].nodeValue; alert(count + ' results were returned'); var items = resultDocument.getElementsByTagName('Response')[0].getElementsByTagName('Document'); for (var i=0; i<items.length; i++) { // Process each document result } } } else { alert('There was an error processing the search'); }
References
The QueryService on MSDN [30] The QueryPacket schema on MSDN [29]
Search.asmx
Code sample
var lists = new SPAPI_Search(''); // Create the new query // Type = SPAPI_Query_Type_SQL // Query = Sharepoint // Fields to return = Title,Path,Description,Write,Rank,Size var query = new SPAPI_QueryPacket(SPAPI_Query_Type_SQL, "SELECT PreferredName, Department, Manager, AboutMe,"+ "Title, Path, Description, Write, Rank, Size FROM SCOPE() where \"scope\"='People' "+ "AND PreferredName LIKE 'A%'", ''); // Add a new sort property - sort by PreferredName, ascending, first sort property query.addSortProperty('PreferredName', true, 0); var res = lists.query(query.getXML()); if (res.status == 200) { // Get the response document var resultDocument = query.getResultDocument(res.responseXML); var status = resultDocument.getElementsByTagName('Response')[0].getElementsByTagName('Status')[0].childNodes[0].nodeValue; alert('The status of the search was: ' + status); if (status == 'SUCCESS') { var count = resultDocument.getElementsByTagName('Range')[0].getElementsByTagName('Count')[0].childNodes[0].nodeValue; alert(count + ' results were returned'); var items = resultDocument.getElementsByTagName('Response')[0].getElementsByTagName('Document'); for (var i=0; i<items.length; i++) { // Process each document result } } } else { alert('There was an error processing the search'); }
References
The QueryService on MSDN [30] The QueryPacket schema on MSDN [29]
8 von 9
21.09.2009 14:44
Darrens SharePoint and .Net blog Examples for the SharePoint a...
http://darrenjohnstone.net/2008/07/22/examples-for-the-sharepoint...
Article printed from Darrens SharePoint and .Net blog: http://darrenjohnstone.net URL to article: http://darrenjohnstone.net/2008/07/22/examples-for-the-sharepoint-and-office-live-javascript-api/ URLs in this post: [1] Get the full definition of a list: #topic-1 [2] Get the full definition of a list and associated view: #topic-2 [3] Get the basic detail of all lists in a site: #topic-3 [4] Get the GUID of a list from its name: #topic-4 [5] Querying a list using the Lists web service: #topic-5 [6] Querying a list using the List Data Retrieval Service: #topic-6 [7] Create a new list from an existing list template: #topic-7 [8] Delete a list: #topic-8 [9] Create one or more new list items: #topic-9 [10] Update one or more list items: #topic-10 [11] Delete one or more list items: #topic-11 [12] Create a new folder within a list: #topic-12 [13] Performing a keyword search: #topic-13 [14] Performing a full text search: #topic-14 [15] on MSDN: http://msdn.microsoft.com/en-us/library/lists.lists.getlist.aspx [16] Lists.asmx on MSDN: http://msdn.microsoft.com/en-us/library/lists.aspx [17] Result packet from GetList showing the full list definition: http://darrenjohnstone.net/wp-content/jsapi/packets/GetList.xml [18] on MSDN: http://msdn.microsoft.com/en-us/library/lists.lists.getlistandview.aspx [19] Result packet from GetListAndView showing the full list and view definition: http://darrenjohnstone.net/wp-content/jsapi/packets /GetListAndView.xml [20] on MSDN: http://msdn.microsoft.com/en-us/library/lists.lists.getlistcollection.aspx [21] Result packet from GetListCollection showing the full list and view definition: http://darrenjohnstone.net/wp-content/jsapi/packets /GetlistCollection.xml [22] GetListItems method on MSDN: http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx [23] Sample GetListItems result packet: http://darrenjohnstone.net/wp-content/jsapi/packets/GetListItems.xml [24] List Data Retrieval Service: http://msdn.microsoft.com/en-us/library/ms774413.aspx [25] The AddList method definition on MSDN: http://msdn.microsoft.com/en-us/library/lists.lists.addlist.aspx [26] The DeleteList method definition on MSDN: http://msdn.microsoft.com/en-us/library/lists.lists.deletelist.aspx [27] The UpdateItems method definition on MSDN: http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems.aspx [28] Query Service: http://msdn.microsoft.com/en-us/library/search.queryservice.aspx [29] QueryPacket: http://msdn.microsoft.com/en-us/library/ms473235.aspx [30] The QueryService on MSDN: http://msdn.microsoft.com/en-us/library/search.queryservice_members.aspx [31] Sample full response packet from the search method: http://darrenjohnstone.net/wp-content/jsapi/packets/QueryResult.xml [32] Sample extracted query result packet from the search method: http://darrenjohnstone.net/wp-content/jsapi/packets /ExtractedQueryResult.xml Copyright 2008 Darren Johnstone. All rights reserved.
9 von 9
21.09.2009 14:44