最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sharepoint - Why I can't get the ClientContext by OM in JavaScript? - Stack Overflow

programmeradmin3浏览0评论

Here is what I did in my project:

<asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript">
  alert("before");
  //It can not work cause the LIST is in the subsite called "Service"
  //var clientContext = SP.ClientContext.get_current();
  var context =new SP.ClientContext("http://sp2010dev1:88/Service/");
  alert(context );  
</script>
</asp:Content>

But, the alert(context) can not execute ,when I check the concole it showed me TypeError: SP.ClientContext is not a constructor,that is to say there was something wrong with the initialization of ClientContext. Why? How can I get ClientContext? Or was it caused by the lacking of SP.js?

My Final Solution is : Add this statement to the masterpage: ,and then everything works good! May this can help you.

Here is what I did in my project:

<asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript">
  alert("before");
  //It can not work cause the LIST is in the subsite called "Service"
  //var clientContext = SP.ClientContext.get_current();
  var context =new SP.ClientContext("http://sp2010dev1:88/Service/");
  alert(context );  
</script>
</asp:Content>

But, the alert(context) can not execute ,when I check the concole it showed me TypeError: SP.ClientContext is not a constructor,that is to say there was something wrong with the initialization of ClientContext. Why? How can I get ClientContext? Or was it caused by the lacking of SP.js?

My Final Solution is : Add this statement to the masterpage: ,and then everything works good! May this can help you.

Share Improve this question edited Dec 6, 2012 at 5:42 JacobChan asked Dec 5, 2012 at 1:35 JacobChanJacobChan 1831 gold badge3 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Instead of writing directly in the <script> blocks try to insert in the function and call that function on body load.

Another good way is call using the jquery

$(document).ready()(function(){
var context =new SP.ClientContext("http://sp2010dev1:88/Service/");
  alert(context );  

});

Try this once, even if it is not working then will try to find another solution.

Along with this as per steve suggestion, include first sp.js and perform remaining things.

Yes, it probably is because SP.js is not loaded. Try this:

<SharePoint:ScriptLink runat="server" Name="SP.js" Localizable="false" OnDemand="False" LoadAfterUI="True"></SharePoint:ScriptLink>
<script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(function () {
        /* your code here */;
    }, "sp.js");
</script>
发布评论

评论列表(0)

  1. 暂无评论