Example - Enumerate Files
//create an object to act as a namespace var sharePoint = {}; //Function that makes the client side call to the server sharePoint.getListItem = function (id, listName, onSuccess, onFailure) { sharePoint.clientContext = SP.ClientContext.get_current(); sharePoint.list = sharePoint.clientContext.get_web().get_lists().getByTitle(listName); sharePoint.listItem = sharePoint.list.getItemById(id); sharePoint.clientContext.load(sharePoint.listItem); sharePoint.clientContext.load(sharePoint.listItem.get_attachmentFiles()); sharePoint.clientContext.executeQueryAsync(Function.createDelegate(this, onSuccess), Function.createDelegate(this, onFailure)); }; //If successful, this method is called sharePoint.onSuccess = function (sender, args) { var item = sharePoint.listItem; var total = item.get_attachmentFiles().get_count(); if (total > 0) { console.log("I found " + total + " file attachments"); } for (var count = 0; count < total; count++) { var spfile = item.get_attachmentFiles().get_item(count); var fileName = spfile.get_fileName(); var serverRelativeUrl = spfile.get_serverRelativeUrl(); console.log("Filename: " + fileName + " File Path: " + serverRelativeUrl); } /* Example output: I found 1 file attachments Filename: Sample.jpg File Path: /subsite/Lists/MyList/Attachments/58/Sample.jpg */ }; //If an error occurs this method is called sharePoint.onFailure = function (sender, args) { if (args instanceof SP.ClientRequestFailedEventArgs) { var message = args.get_message(); var code = args.get_errorCode(); var details = args.get_errorDetails(); var value = args.get_errorValue(); var typeName = args.get_errorTypeName(); var stack = args.get_stackTrace(); var correlationId = args.get_errorTraceCorrelationId(); alert('Error: ' + message + "."); } else { alert('Unexpected Error'); } }; //This method initates the process function Test() { sharePoint.getListItem("58", "MyList", sharePoint.onSuccess, sharePoint.onFailure); } $(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(Test, "SP.js"); });
Available Methods
Method Name | Description |
---|---|
get_item | Get an item at index (zero based) |
itemAt | as above |
get_count | Returns total items. |
getByFileName | Returns item based on filename |
Additional Methods
Note: It is unclear if any of these methods are intended for use.
Method Name | Description |
---|---|
getEnumerator | |
get_childItemType | |
get_areItemsAvailable | |
retrieveItems | |
getItemAtIndex | |
addChild | |
removeChild | |
retrieve | |
refreshLoad | |
isPropertyAvailable |