我正在尝试编写一个控制台应用程序,它只是列出了共享点根目录的数量。
我尝试使用以下代码来执行此操作,但对象 SPContext.Current 为空。关于如何获取网络对象的任何想法?
SPWeb web = SPContext.Current.Site.OpenWeb("http://localhost") ;
我正在尝试编写一个控制台应用程序,它只是列出了共享点根目录的数量。
我尝试使用以下代码来执行此操作,但对象 SPContext.Current 为空。关于如何获取网络对象的任何想法?
SPWeb web = SPContext.Current.Site.OpenWeb("http://localhost") ;
只需在 Nat 的帖子中添加一点内容:
即使它不像在 SharePoint WebApp 中那样重要,仍然建议处理所有 SPWeb 和 SPSite 对象。
所以一定要养成好习惯:
using (SPSite site = new SPSite(weburl))
{
using (SPWeb web = site.OpenWeb())
{
// bla bla
}
}
注意:您可以直接将 weburl 传递给 SPSite 构造函数,因此 OpenWeb 将打开给定的 web。
SPSite spSite = new SPSite("http://myurl");
SPWeb spMySite = spSite.Allwebs["mysite"];
SPWeb spRootsite = spsite.RootWeb;
控制台应用程序只会像往常一样在服务器上运行。此外,使用的 url http://myurl可以是页面的 url,并且将创建一个 SPSite 对象。例如http://myurl/mysite/pages/default.aspx将获得一个有效的 SPSite 对象。
您还可以通过其他几种方式使用 SPSite.OpenWeb() ......
如果您跟踪 SPWeb 对象的 GUID:
site.OpenWeb(webUid);
使用 Web 的服务器或站点的相对 URL 或标题,请参阅MSDN SPSite.OpenWeb(string)了解更多详细信息:
site.OpenWeb(relativeUrl);
site.OpenWeb(title);
使用精确的相对 URL 并避免 SPSite.OpenWeb(string) 使用的任何巧妙的东西:
site.OpenWeb(relativeUrl, true);