2013-08-20 38 views

回答

2

您可以使用下面的使用客户端对象模型

var clientContext = new SP.ClientContext(); 
var owebsite = clientContext.get_site.get_rootWeb(); 

没有客户端对象模型,你可以使用下面的

var siteCollectionPath= _spPageContextInfo.siteServerRelativeUrl; 
+0

我试过这个var siteCollectionPath = _spPageContextInfo.siteServerRelativeUrl; alert(siteCollectionPath);它给错误_spPageContextInfo没有定义。我在团队网站中使用自定义母版页。并试过这个它给JS错误...我错过了什么 – user388969

1
var clientContext = new SP.ClientContext.get_current(); 
this.web = clientContext.get_site().get_rootWeb(); 

它为我工作。

0

通过使用可用信息和字符串从那里解析它,您可以获得没有客户端对象模型的url。不如客户端对象模型那么健壮,但对于简单任务来说要复杂得多。

window.location.href 

...给你完整的窗口网址(如 “http://sitecollection.com/sites/mysite/Lists/myList/NewForm.aspx?ContentTypeId=0x01006AC2C39AA621424EBAD9C2AC8A54F8B9007B626ABEEB66E34196C46E13E0CA41A2&ContentTypeName=xxxx”)

L_Menu_BaseUrl 

或者

_spPageContextInfo.siteServerRelativeUrl 

...(如不幸了所指出的)会给你的相对URL(例如“/ sites/mysite”)

0

下面的示例脚本显示了如何检索根网站。 onQuerySucceedSample函数警告根站点标题。

getRootWeb = function() { 
    //Get and load a reference to the root web of the current site collection. 
    var ctx = new SP.ClientContext.get_current(); 
    var site = ctx.get_site(); 
    this.rootWeb = site.get_rootWeb(); 

    ctx.load(this.rootWeb); 
    //Ask SharePoint to pull data for us 
    ctx.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceed),Function.createDelegate(this, this.onQueryFailed)); 
}; 

//Function executed on success 
onQuerySucceed = function() { 
    alert(this.rootWeb.get_title()); 
}; 

//Function executed on failure 
onQueryFailed = function (sender, args) { 
    alert('Unable to retrieve data from the SharePoint. Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 
};