Вопрос

Я создал пользовательскую главную страницу. И я хочу получить доступ к данным списка SharePoint на этой главной странице, используя JavaScript.

Я пишу следующий скрипт для доступа к данным, но это дает ошибку, что "SP.CLIRITCONTEXT.GET_CURRENT () NULL или undefined"

<script type="text/javascript"> 
window.onload=function(){
var url = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl;
var context = new SP.ClientContext.get_current();  // error "sp.clientcontext.get_current() null or undefined"
var clientContext = new SP.ClientContext(url);
var oList = clientContext.get_web().get_lists().getByTitle('MasterPageLinks');

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
    '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
    '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' + 
    '<RowLimit>10</RowLimit></View>'
);
this.collListItem = oList.getItems();

clientContext.load(collListItem);
clientContext.executeQueryAsync(
    Function.createDelegate(this, this.onQuerySucceeded), 
    Function.createDelegate(this, this.onQueryFailed)
); 
};
function onQuerySucceeded(sender, args) {
debugger;
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();

while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    listItemInfo += '\nID: ' + oListItem.get_id() + 
        '\nTitle: ' + oListItem.get_item('Title') + 
        '\nBody: ' + oListItem.get_item('Body');
}

alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

alert('Request failed. ' + args.get_message() + 
    '\n' + args.get_stackTrace());
}
</script>
.

Как я могу сделать это?

Это было полезно?

Решение

To use Client OM, you need a reference to SP.js

For production:

<SharePoint:ScriptLink Name="sp.js" runat="server" OnDemand="true" Localizable="false" />

For development purpose:

<SharePoint:ScriptLink Name="sp.debug.js" runat="server" OnDemand="true" Localizable="false" />

Moreover, there is also another way to declare it using the ExecuteOrDelauUntilScriptLoaded method:

var clientContext;

ExecuteOrDelayUntilScriptLoaded(initialize,"SP.js");

function initialize() {

  this.clientContext = SP.ClientContext.get_current();

}

Happy SharePointing!

Другие советы

Try this...

$(document).ready(function(){


ExecuteOrDelayUntilScriptLoaded(GetDocuments(), "sp.js");

setTimeout(function() {


var listurl = yoursite +"/_vti_bin/lists.asmx";
var listname = "listname";
var query = "";
var soapEnv = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'><soapenv:Body><GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>"+
              "<listName>" + listname + "</listName> <query><Query>"+query+"</Query></query>"+
              "<viewFields><ViewFields><FieldRef Name='LinkFilename' /></ViewFields></viewFields>"+
              "</GetListItems> </soapenv:Body></soapenv:Envelope>";

$.ajax
({
   url: listurl,
   beforeSend: function (xhr) {
       xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");
   },
   type: "POST",
   dataType: "xml",
   data: soapEnv,
   async:false,
   complete: function (xData, status) {
  $(xData.responseXML).find("z\\:row").each(function () {
            var title = $(this).attr("ows_Title");
        });
   },
   contentType: "text/xml; charset=utf-8"
});


}, 5000);


});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top