0

I've having difficulty following this guide

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

I've created the helper class as advised:

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);
    }
  }
}

However, I do not want to create another site, as I already have a site, so I wanted to test the next part by retrieving the existing sites title:

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

and added this to the form load event:

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);

What I am getting confused about is, why I am typing in the title of the site which I want to receive the title of??? So I think I am not using this properly?

The error I get is:

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

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

Any idea what I am doing wrong?

The username and password I have used has full SharePoint privileges.

I am using Visual Studio 2013, C#, .NET 4.0, and SharePoint 2010.

1

GetWebByTitle(string siteTitle) method is trying to find title in all sub sites. If you want to get the title of the site whose URL you passed, You can use

var sh = new SharepointHelper("https://sharepoint.oshirowanen.com/sites/oshirodev/", "sharepoint_admin_user", "sharepoint_admin_password");
Console.WriteLine(rootWeb.Title);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.