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
- Get the full definition of a list and associated view
- Get the basic detail of all lists in a site
- Get the GUID of a list from it’s name
- Querying a list using the Lists web service
- Querying a list using the List Data Retrieval Service
- Create a new list from an existing list template
- Delete a list
- Create one or more new list items
- Update one or more list items
- Delete one or more list items
- Create a new folder within a list
- Performing a keyword search
- Performing a full text search
1- Get the full definition of a list
The lists web service contains a method GetList which can be used to obtain the XML definition of a list.
This definition contains a great deal of information about the list. The result is a fragment of CAML as described on MSDN.
Services | JS API Includes Required |
---|---|
|
|
Code sample
01.
var
lists =
new
SPAPI_Lists(
'https://localhost'
)
02.
var
listDef = lists.getList(
'Pages'
);
03.
if
(listDef.status == 200)
04.
{
05.
// The list definition is in listDef.resultNode and is a browser specific XML document
06.
}
07.
else
08.
{
09.
alert(
'There was an error: '
+ listDef.statusText);
10.
}
References
Related sample XML packets
2- Get the full definition of a list and associated view
The lists web service contains a method GetListAndView which can be used to obtain the XML definition of a list and an associated view. This definition contains a great deal of information about the list. The result is a fragment of CAML as described on MSDN.
Services | JS API Includes Required |
---|---|
|
|
Code sample
01.
var
lists =
new
SPAPI_Lists(
'https://localhost'
)
02.
var
listDef = lists.getListAndView(
'Tasks'
,
'{4011C1CE-A840-4BEB-89E3-A42DF3E3711E}'
);
03.
if
(listDef.status == 200)
04.
{
05.
// The list definition is in listDef.resultNode and is a browser specific XML document
06.
}
07.
else
08.
{
09.
alert(
'There was an error: '
+ listDef.statusText);
10.
}
References
Related sample XML packets
3- Get the basic detail of all lists in a site
The lists web service contains a method GetListCollection which can be used to obtain the basic XML details of all lists in a site. Once these have been retrieved they can be enumarated and further details loaded as required. The result is a fragment of CAML as described on MSDN.
Services | JS API Includes Required |
---|---|
|
|
Code sample
01.
var
lists =
new
SPAPI_Lists(
'https://localhost'
)
02.
var
listCollection = lists.getListCollection();
03.
if
(listCollection.status == 200)
04.
{
05.
var
items = listCollection.responseXML.getElementsByTagName(
'List'
);
06.
alert(
'There are '
+ items.length +
' lists in the site.'
);
07.
for
(
var
i=0; i<items.length; i++)
08.
{
09.
// Do something
10.
alert(items[i].getAttribute(
'Title'
) +
' '
+ items[i].getAttribute(
'ID'
));
11.
}
12.
}
13.
else
14.
{
15.
alert(
'There was an error: '
+ listCollection.statusText);
16.
}
References
Related sample XML packets
4- Get the GUID of a list from it’s name
Some web services require that the internal guid of a list be passed as a paramter. Since this guid can potentially change between site backups and certainly when the solution is shipped this can be a problem for solution developers. The GetListGuid function takes a list name and returns the guid of the list.
Services | JS API Includes Required |
---|---|
|
|
Code sample
01.
function
getListGuid(baseUrl, listName)
02.
{
03.
var
res;
04.
var
lists =
new
SPAPI_Lists(
''
);
05.
res = lists.getList(listName);
06.
if
(res.status == 200)
07.
{
08.
var
listID = -1;
09.
if
(res.resultNode !=
null
)
10.
{
11.
listID = res.resultNode.childNodes[0].getAttribute(
'ID'
);
12.
}
13.
return
{ listID : listID, fullResult : res };
14.
}
15.
else
16.
{
17.
return
{ listID :
null
, fullResult : res };
18.
}
19.
}
References
Related sample XML packets
5- Querying a list using the Lists web service
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 | JS API Includes Required |
---|---|
|
|
Code sample
01.
// Return all items in the default view of MyList
02.
var
lists =
new
SPAPI_Lists(
'https://localhost'
)
03.
var
items = lists.getListItems(
'MyList'
);
04.
if
(items.status == 200)
05.
{
06.
var
rows = items.responseXML.getElementsByTagName(
'z:row'
);
07.
document.getElementById(
'output'
).value = items.responseText;
08.
for
(
var
i=0; i<rows.length; i++)
09.
{
10.
// Do something with the row
11.
}
12.
}
13.
else
14.
{
15.
alert(
'There was an error: '
+ items.statusText);
16.
}
17.
// Return the first 5 items where ID < 10. Only return the Title and ID columns and do not include mandatory columns.
18.
var
lists =
new
SPAPI_Lists(
'https://localhost'
)
19.
var
items = lists.getListItems(
20.
'MyList'
,
// listName
21.
''
,
// viewName
22.
'<Query><Where><Lt><FieldRef Name="ID"/><Value Type="Counter">10</Value></Lt></Where></Query>'
,
// query
23.
'<ViewFields><FieldRef Name="ID"/><FieldRef Name="Title"/></ViewFields>'
,
// viewFields
24.
5,
// rowLimit
25.
'<QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions>'
// queryOptions
26.
);
27.
if
(items.status == 200)
28.
{
29.
var
rows = items.responseXML.getElementsByTagName(
'z:row'
);
30.
document.getElementById(
'output'
).value = items.responseText;
31.
for
(
var
i=0; i<rows.length; i++)
32.
{
33.
// Do something with the row
34.
}
35.
}
36.
else
37.
{
38.
alert(
'There was an error: '
+ items.statusText);
39.
}
References
Related sample XML packets
6- Querying a list using the List Data Retrieval Service
The List Data Retrieval Service allows lists to be queried using CAML notation. This service is one of several ways to search SharePoint and Office Live lists. The service allows for complex search criteria, sorting, result count limiting, and selection of fields. The javascript API provides a specific helper method to assist in calling this service.
The results are in the form of a CAML fragment containing an element for every row in the result set.
The example selects all pages from the site with an ID greater than or equal to 5. These are sorted in descending order of ID and limited to 10 results. The getElementsById method is then used to extract the rows and display their attributes one by one.
Services | JS API Includes Required |
---|---|
|
|
Code sample
01.
var
dspsts =
new
SPAPI_dspsts(
'https://localhost'
);
02.
var
res = dspsts.queryRequest(
03.
"{CFD2A33E-EE89-47F3-9E28-24442E598326}"
,
// listGuid
04.
"<Field Name='ID'/><Field Name='Title'/>"
,
// fields
05.
"<Geq><FieldRef Name='ID' /><Value Type='Counter'>5</Value></Geq>"
,
// where
06.
"<OrderField Name='ID' Type='xsd:int' Direction='DESC'/>"
,
// orderBy
07.
10
// rowLimit
08.
);
09.
if
(res.status == 200)
10.
{
11.
var
rows = res.responseXML.getElementsByTagName(
'Row'
);
12.
alert(
'There were '
+ rows.length +
' results.'
);
13.
for
(
var
i=0; i<rows.length; i++)
14.
{
15.
var
item = rows[i];
16.
// Do something with the row
17.
alert(
'The page with ID '
+ item.getAttribute(
'ID'
) +
' has a title of '
+ item.getAttribute(
'Title'
));
18.
}
19.
}
20.
else
21.
{
22.
alert(
'There was an error: '
+ res.statusText);
23.
}
References
7- Create a new list from an existing list template
The AddList method of the lists web service may be used to create a new list based on an existing list template.
The example shown creates a new tasks list called NewTasks.
Services | JS API Includes Required |
---|---|
|
|
Code sample
01.
var
lists =
new
SPAPI_Lists(
'https://localhost'
)
02.
var
res = lists.addList(
'NewTasks'
,
'My new task list'
, lists.LIST_ID_TASKS)
03.
if
(res.status == 200)
04.
{
05.
alert(
'The new list was created.'
);
06.
}
07.
else
08.
{
09.
alert(
'There was an error: '
+ res.statusText);
10.
}
Remarks
The list template IDs you will need to use this method are defined as constants in the SPAPI_Lists class.
01.
/* List template IDs */
02.
this
.LIST_ID_ADMIN_TASKS = 1200
// Administrator tasks list
03.
this
.LIST_ID_ANNOUNCEMENTS = 104
// Announcements list
04.
this
.LIST_ID_BLOG_CATEGORIES = 303
// Blog Categories list
05.
this
.LIST_ID_BLOG_COMMENTS = 302
// Blog Comments list
06.
this
.LIST_ID_BLOG_POSTS = 301
// Blog Posts list
07.
this
.LIST_ID_CONTACTS = 105
// Contacts list
08.
this
.LIST_ID_CUSTOM_GRID = 120
// Custom grid for a list
09.
this
.LIST_ID_CUSTOM_WORKFLOW = 118
// Custom Workflow Process
10.
this
.LIST_ID_DATA_CONNECTIONS = 130
// Data Connection library
11.
this
.LIST_ID_SATA_SOURCES = 110
// Data sources
12.
this
.LIST_ID_DISCUSSION_BORAD = 108
// Discussion board
13.
this
.LIST_ID_DOCUMENT_LIBRARY = 101
// Document library
14.
this
.LIST_ID_EVENTS = 106
// Events list
15.
this
.LIST_ID_GANTT_TASKS = 150
// Gantt Tasks list
16.
this
.LIST_ID_GENERIC = 100
// Generic list
17.
this
.LIST_ID_ISSUE_TRACKING = 1100
// Issue tracking
18.
this
.LIST_ID_LINKS = 103
// Links list
19.
this
.LIST_ID_LIST_TEMPLATE = 114
// List template gallery
20.
this
.LIST_ID_MASTER_PAGE = 116
// Master pages gallery
21.
this
.LIST_ID_MEETING_AGENDA = 201
// Meeting Agenda list
22.
this
.LIST_ID_MEETING_ATTENDEES = 202
// Meeting Attendees list
23.
this
.LIST_ID_MEETING_DECISIONS = 204
// Meeting Decisions list
24.
this
.LIST_ID_MEETING_OBJECTIVES = 207
// Meeting Objectives list
25.
this
.LIST_ID_MEETING_SERIES = 200
// Meeting Series list
26.
this
.LIST_ID_MEETING_TEXT_BOX = 210
// Meeting text box
27.
this
.LIST_ID_MEETING_TTB = 211
// Meeting Things To Bring list
28.
this
.LIST_ID_MEETING_WS_PAGES = 212
// Meeting Workspace Pages list
29.
this
.LIST_ID_NO_CODE_WORKLOFWS = 117
// No-Code Workflows
30.
this
.LIST_ID_PERSONAL_DOCLIB = 2002
// Personal document library
31.
this
.LIST_ID_PICTURE_LIBRARY = 109
// Picture library
32.
this
.LIST_ID_PORTAL_SITE_LIST = 300
// Portal Sites list
33.
this
.LIST_ID_PRIVATE_DOCLIB = 2003
// Private document library
34.
this
.LIST_ID_SITE_TEMPLATES = 111
// Site template gallery
35.
this
.LIST_ID_SURVEY