获取 SharePoint 网站标题

Getting SharePoint Site Title

我在遵循本指南时遇到困难

http://dotnetbyexample.blogspot.co.uk/2011/03/sharepoint-client-object-model-sites.html

我已经按照建议创建了助手 class:

namespace TestSharepoint
{
  public class SharepointHelper
  {
    private ClientContext clientContext;
    private Web rootWeb;

    public SharepointHelper(string url, string username, string password)
    {
      clientContext = new ClientContext(url);
      var credentials = new NetworkCredential(username, password, "oshirowanen.com");
      clientContext.Credentials = credentials;
      rootWeb = clientContext.Web;
      clientContext.Load(rootWeb);
    }
  }
}

但是,我不想创建另一个站点,因为我已经有一个站点,所以我想通过检索现有站点标题来测试下一部分:

public Web GetWebByTitle(string siteTitle)
{
  var query = clientContext.LoadQuery(
    rootWeb.Webs.Where(p => p.Title == siteTitle));
  clientContext.ExecuteQuery();
  return query.FirstOrDefault();
}

并将其添加到表单加载事件中:

var sh = new SharepointHelper("https://sharepoint.oshirowanen.com/sites/oshirodev/", "sharepoint_admin_user", "sharepoint_admin_password");
var w = sh.GetWebByTitle("Oshirowanen SharePoint");
Console.WriteLine(w.Title);

让我感到困惑的是,为什么我要输入我想要接收标题的网站标题???所以我觉得我没有正确使用它?

我得到的错误是:

An unhandled exception of type 'System.NullReferenceException' occurred in SharePointProgramming.exe

Additional information: Object reference not set to an instance of an object.

知道我做错了什么吗?

我使用的用户名和密码具有完整的 SharePoint 权限。

我正在使用 Visual Studio 2013、C#、.NET 4.0 和 SharePoint 2010。

要获取站点的标题,您只需要变量 web.title 的值。

  namespace TestSharepoint
{
  public class SharepointHelper
  {
    private ClientContext clientContext;
    private Web rootWeb;

    public SharepointHelper(string url, string username, string password)
    {
      clientContext = new ClientContext(url);
      var credentials = new NetworkCredential(username, password, "oshirowanen.com");
      clientContext.Credentials = credentials;
      rootWeb = clientContext.Web;
      clientContext.Load(rootWeb,web=>web.title);
    clientContent.ExecuteQuery();
    string siteTitle=web.title;
            }
          }
        }