Marvin Rowe
Marvin Rowe

Reputation: 11

Trying to access Sharepoint remotely

I've found multiple sites that tell me how to access a Sharepoint site remotely but none seem to explain let alone answer what I am experiencing.

  1. I created a simple C# console app

  2. Within the app I reference: Microsoft.Sharepoint, Microsoft.Sharepoint.Client, and Microsoft.Sharepoint.Client.Runtime

  3. I am able to create using statements for Microsoft.Sharepoint and Microsoft.Sharepoint.Client but I do not get intellisense for Microsoft.Sharepoint.Client.Runtime (I only get Applications, Utilities, WebParts and Workflow in the drop down window) -- If I type in using Microsoft.Sharepoint.Client.Runtime, I get the "red squiggles" under Runtime

  4. Commenting out the Microsoft.Sharepoint.Client.Runtime, I then enter the following code:

    const string FORMAT = "{0}: {1}";
    string strURL = "http://<the SPS site address>";
    List<string> listFields = new List<string>();
    
    using (SPSite oSite = new SPSite(strURL))
    {
        using (SPWeb oWeb = oSite.OpenWeb())
        {
            foreach (SPList list in oWeb.Lists)
            {
                foreach (SPField field in list.Fields)
                {
                    listFields.Add(string.Format(FORMAT, list.ID, field.Title));
                }
            }
        }
    }
    

    Note: At this point in time, there are no errors reported in the error list

  5. I then compile the code (Rebuild) -- At that point I get:

    1. Squiggles in both "Sharepoint" using statements
    2. Five (5) errors stating:
      • 'Sharepoint' does not exist in the namespace "Microsoft" (are you missing an assembly reference?) 2X
      • 'SPSite' could not be found (are you missing a using directive or an assembly reference?) 2X
      • 'SPWeb' could not be found (are you missing a using directive or an assembly reference) 1X

Upvotes: 0

Views: 527

Answers (1)

Jaime Macias
Jaime Macias

Reputation: 847

If you're not developing on a computer with SharePoint installed on it you can't use those SSOM objects.

You should use their CSOM equivalent.

SPSite - Site

SPWeb - Web

SPList - List

Check out MSDN for other objects.

You will only need the reference to Microsoft.SharePoint.Client

Here's an example from MSDN to retrieve items from a list:

            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");

            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +
                "<Value Type='Number'>10</Value></Geq></Where></Query><RowLimit>100</RowLimit></View>";
            ListItemCollection collListItem = oList.GetItems(camlQuery);

            clientContext.Load(collListItem);

            clientContext.ExecuteQuery();

            foreach (ListItem oListItem in collListItem)
            {
                Console.WriteLine("ID: {0} \nTitle: {1} \nBody: {2}", oListItem.Id, oListItem["Title"], oListItem["Body"]);
            }

Upvotes: 1

Related Questions