ads

Thursday, June 27, 2013

Client side - retrieve SharePoint list items (2 ways)

1.  ajax request

<script type="text/javascript">
function LoadData(listName) {
            var soapEnv ="<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
<soapenv:Body> \
       <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
            <listName>"+listName+"</listName> \
               <viewFields> \
                     <FieldRef Name='Status' /><FieldRef Name='AssignedTo' />
              </viewFields> \
           <rowLimit>100</rowLimit> \
</GetListItems> \
</soapenv:Body> \
</soapenv:Envelope>";

            $.ajax({
                        url: "site url/_vti_bin/lists.asmx",
                        type: "POST",
                        dataType: "xml",
                        data: soapEnv,
                        complete: OnLinksFetched,
                        contentType: "text/xml; charset=\"utf-8\""
            });
}

function OnLinksFetched(result) {
                    //iterate though all of the items
            $(result.responseXML).find("z\\:row").each(function()
             {
             alert("Assigned To : " + $(this).attr("ows_AssignedTo") + "; Status : "+ 
                      $(this).attr("ows_Status");
       
            });
           
            }
</script>
2.  client object model java script


<script type="text/javascript">             
var clientContext;
var web;
                function getListItems(listName)
                {
                clientContext = new SP.ClientContext.get_current();
                web = clientContext.get_web();
                var list = web.get_lists().getByTitle(listName);
                var camlQuery = new SP.CamlQuery();
                var q = '<View><RowLimit>100</RowLimit></View>';
                camlQuery.set_viewXml(q);
                this.listItems = list.getItems(camlQuery);
                clientContext.load(listItems, 'Include(Status,AssignedTo)');
                clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess),
                Function.createDelegate(this, this.onQueryFailed));
                }
                function onListItemsLoadSuccess(sender, args) {
                                var listEnumerator = this.listItems.getEnumerator();
                //iterate though all of the items
                                while (listEnumerator.moveNext()) {
                                    var item = listEnumerator.get_current();               
                    var  AssignedTo=item.get_item('AssignedTo').get_lookupValue();
                                    var  Status=item.get_item('Status');                   
                        alert("Assigned To : " + AssignedTo + "; Status : "+ Status);

                        }

                }
               
                function onQueryFailed(sender, args) {
               alert('request failed ' + args.get_message() + '\n' +   args.get_stackTrace());
                }
</script>

No comments:

Post a Comment