1

I am trying to use SPSite, SPWeb in my C# project. I get this error saying: "The type or namespace SPSite can not be found (are you missing an assembly reference)"

I have add 'using Microsoft.SharePoint.Client' in my code. And I have add 'Microsoft.Sharepoint.Client' as my assembly.

Thank you for your help.

The URL I am trying to access is 'https://mycompany.sharepoint.com/sites/parts/'.

So when I tried this to read the title of the page, I got a 403 error.

            ClientContext context = new ClientContext("https://mycompany.sharepoint.com/sites/parts/");
            var credentials = new NetworkCredential(mywindowsusername, mywindowspassword);


            context.Credentials = credentials;
            // The SharePoint web at the URL.
            Web web = context.Web;

            // We want to retrieve the web's properties.
            context.Load(web);

            // Execute the query to the server.
            context.ExecuteQuery();

            // Now, the web's properties are available and we could display 
            // web properties, such as title. 
            System.Console.WriteLine("Web Title");
            System.Console.WriteLine(web.Title);
5

If you are using Client object model use like this:

ClientContext context = new ClientContext("http://SiteUrl"); 

// The SharePoint web at the URL.
Web web = context.Web; 

// We want to retrieve the web's properties.
context.Load(web); 

// Execute the query to the server.
context.ExecuteQuery(); 

// Now, the web's properties are available and we could display 
// web properties, such as title. 
label1.Text = web.Title;

To use Server side object model use Microsoft.Sharepoint dll.

  • Thanks for your answer. I tried that. I get an exception 'The remote server returned an error: (403) Forbidden.' in the line 'context.Load(web). I have already add var credentials = new NetworkCredential(myusername, mypassword, "domain") context.Credentials = credentials. – n179911 Sep 24 '14 at 17:54
  • What authentication are you using on sharepoint? – Aanchal Sep 24 '14 at 17:58
  • You might be getting error on context.ExecuteQuery(); – Aanchal Sep 24 '14 at 18:00
  • In my browser, I enter my username and password. How can I find out what authentication on sharepoint? I did not build the sharepoint site. I am working on client to get data from that site. – n179911 Sep 24 '14 at 18:14
  • Are you passing windows credentials? Do you have sufficient permissions on the site? – Aanchal Sep 24 '14 at 18:15
5

SPSite and SPWeb are part of the Server Object Model and are implemented in Microsoft.SharePoint.dll. This object model can only be used when the code is executing on a SharePoint server.

Microsoft.SharePoint.Client.dll is one of the assemblies that implement the Client Object Model. This is a remote object model that can be used in client applications (e.g. WPF) or in web sites that are external to SharePoint.

3

Sometimes there are situations when users do not get enough priviledges over SharePoint resources. So in the case there is need for impersonation for using the resources. Try using below code if it works for you:

ClientContext context = new ClientContext("https://mycompany.sharepoint.com/sites/parts/");
    WindowsImpersonationContext ctx = null;
    if (!WindowsIdentity.GetCurrent().IsSystem)
    {
        ctx = WindowsIdentity.Impersonate(System.IntPtr.Zero);
        // The SharePoint web at the URL.
        Web web = context.Web;

        // We want to retrieve the web's properties.
        context.Load(web);

        // Execute the query to the server.
        context.ExecuteQuery();

        // Now, the web's properties are available and we could display 
        // web properties, such as title. 
        System.Console.WriteLine("Web Title");
        System.Console.WriteLine(web.Title);
    }

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.