0
votes

I have write a code that contains SPSite and I got this error CS0246: The type or namespace name 'SPSite' could not be found (are you missing a using directive or an assembly reference?)

I have tried downloading Microsoft.SharePoint from the nuget site here https://www.nuget.org/packages/Microsoft.SharePoint.dll/ and then I have put the package in the right folder and still I have the same error

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Security;
using System.Net;
using System.Configuration;
using System.Collections.Specialized;
using System.Runtime;
using Microsoft.SharePoint;
using System.Reflection;

using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
protected void getList()
   {
       string strUrl = "   SharePoint SITE     ";
       using (SPSite oSite = new SPSite(strUrl))
       {
           using (SPWeb oWeb = oSite.OpenWeb())
           {
               SPList list = oWeb.Lists["Workplan"];
               foreach (SPField field in list.Fields)
               {
                   Console.WriteLine(field.Title);
               }
           }
       }
   }

after adding the package that I download in the right place I expected for the error to fade away still it's there. Help please!

2
You shouldn't be manually putting packages in folders, installing a nuget package handles this for you. Did you try Install-Package ....?Crowcoder
You have using SP = Microsoft.SharePoint.Client;, do you need SP.SPSite?doctorlove
I don't know where I should be writting this : Install-Package Microsoft.SharePoint.dll -Version 15.0.4867.1000ChampagneM12
@ChampagneM12 SPSite is a server-side type but your code references the client libraries. You don't need SPSite to work with the client libraries at all. Did you copy some server-side code and tried to use it from a client perhaps?Panagiotis Kanavos
@ChampagneM12 you don't need and can't add that assembly as if it were a package. You can't write code by blindly copying snippets. The docs show how to use the client API. It's a lot easier than using the server APIPanagiotis Kanavos

2 Answers

0
votes

Install the nuget package, its the best way to use it. You can install with Package Manager UI (Visual Studio) or Package Manager Console (Visual Studio) see hear.

Note 1: SPSite, SPWeb, SPList and SPField are classes in namespace Microsoft.SharePoint.

Note 2: you have in your code: using Microsoft.SharePoint; and using Microsoft.SharePoint.Client;, Microsoft.SharePoint and Microsoft.SharePoint.Client are two different nuget packages.

Yue

-1
votes

For those who had the same problem as me. I found a solution that made the error disappear. Manage NuGet Packages

Click Manage NuGet Packages, and then in the browsing area tape 'Microsoft.SharePoint.dll' when you do that you would want to choose the right version of your NuGet Package in a way that it would be compatible with you .Net Framework Then Click install. Right after when you go back to the code, you will see the error went away. For more clearity hit me up in the comment.