static void Main(string[] args) { //This example returns all items from the Announcements list //For your own lists, first add a Service Reference to the SharePoint site you want to query //Then change the item object below. //Formulate the URL to the List Data RESTful service string Url = "http://intranet.contoso.com/_vti_bin/listdata.svc"; //Create a dataContext object for the service TeamSiteDataContext dataContext = new TeamSiteDataContext(new Uri(Url)); //Authenticate as the currently logged on user dataContext.Credentials = CredentialCache.DefaultCredentials; //Tell the user what will be displayed Console.WriteLine("Items in the Announcements list:"); Console.WriteLine(); //Loop through the announcements foreach (AnnouncementsItem item in dataContext.Announcements) { //Display the item's properties Console.WriteLine("Title: " + item.Title); Console.WriteLine("ID: " + item.Id); Console.WriteLine("Body: " + item.Body); Console.WriteLine(); } //This line prevents the console disappearing before you can read the result //Alternatively, remove this line a run the project without debugging (CTRL-F5) Console.ReadKey(); }
static void Main(string[] args) { //This example returns an item called "Test Item" from the Announcements list //For your own lists, first add a Service Reference to the SharePoint site you want to query //Then change the item object below. //Formulate the URL to the List Data RESTful service string Url = "http://intranet.contoso.com/_vti_bin/listdata.svc"; //Create a dataContext object for the service TeamSiteDataContext dataContext = new TeamSiteDataContext(new Uri(Url)); //Authenticate as the currently logged on user dataContext.Credentials = CredentialCache.DefaultCredentials; //Query for a single item by using a lambda expression to select items with a specific title AnnouncementsItem item = dataContext.Announcements.Where(i => i.Title == "Test Item").FirstOrDefault(); if (item == null) { //There is no announcement with that title Console.WriteLine("An announcement with the title 'Test Item' was not found."); } else { //Return the item Console.WriteLine("Title: " + item.Title); Console.WriteLine("Body: " + item.Body); } //This line prevents the console disappearing before you can read the result //Alternatively, remove this line a run the project without debugging (CTRL-F5) Console.ReadKey(); }
static void Main(string[] args) { //This example returns items from the Announcements list //For your own lists, first add a Service Reference to the SharePoint site you want to query //Then change the item object below. //Formulate the URL to the List Data RESTful service string Url = "http://intranet.contoso.com/_vti_bin/listdata.svc"; //Create a dataContext object for the service TeamSiteDataContext dataContext = new TeamSiteDataContext(new Uri(Url)); //Authenticate as the currently logged on user dataContext.Credentials = CredentialCache.DefaultCredentials; //Select all items beginning with 'A' by using a Linq query var results = from items in dataContext.Announcements where items.Title.StartsWith("A") orderby items.Title select new { items.Title, items.Id, items.Body }; //Tell the user what will be displayed Console.WriteLine("Items in the Announcements list that begin with an A, ordered by Title:"); Console.WriteLine(); //Loop through the results foreach (var announcement in results) { //Display some properties of the item Console.WriteLine("Title: " + announcement.Title); Console.WriteLine("ID: " + announcement.Id); Console.WriteLine("Body: " + announcement.Body); Console.WriteLine(); } //This line prevents the console disappearing before you can read the result //Alternatively, remove this line a run the project without debugging (CTRL-F5) Console.ReadKey(); }
public bool DeleteItem(string Title) { //This example finds the first item in the Announcements list //with the specified title. //For your own lists, first add a Service Reference to the //SharePoint site you want to query, then change the item object below. //Tell the user what's going on Console.WriteLine("Received a DeleteItem call. About to query SharePoint..."); //Formulate the URL to the List Data RESTful service. //You must correct this path to point to your own SharePoint farm string Url = "http://intranet.contoso.com/_vti_bin/listdata.svc"; //Create a dataContext object for the service TeamSiteDataContext dataContext = new TeamSiteDataContext(new Uri(Url)); //Authenticate as administrator. NetworkCredential myCredential = new NetworkCredential("Administrator", "pass@word1"); dataContext.Credentials = myCredential; try { //Use a lambda expression to locate an item with a matching title AnnouncementsItem announcement = dataContext.Announcements.Where(i => i.Title == Title).FirstOrDefault(); //Delete the announcement dataContext.DeleteObject(announcement); dataContext.SaveChanges(); return(true); } catch (Exception e) { Console.WriteLine("An error occurred: {0}", e.Message); return(false); } }
private Dictionary <string, string> queryListServer(string listName) { //This example returns all items from the Announcements list //For your own lists, first add a Service Reference to the //SharePoint site you want to query, then change the item object below. Dictionary <string, string> listEntries = new Dictionary <string, string>(); //Formulate the URL to the List Data RESTful service string Url = "http://intranet.contoso.com/_vti_bin/listdata.svc"; //Create a dataContext object for the service TeamSiteDataContext dataContext = new TeamSiteDataContext(new Uri(Url)); //Authenticate as administrator. NetworkCredential myCredential = new NetworkCredential("Administrator", "pass@word1"); dataContext.Credentials = myCredential; //As this is running in the console, we can display the results Console.WriteLine("Items in the Announcements list:"); Console.WriteLine(); //Loop through the announcements foreach (AnnouncementsItem item in dataContext.Announcements) { //Display the item's properties Console.WriteLine("Title: " + item.Title); listEntries.Add(item.Title, item.Body); } //Return the dictionary return(listEntries); }
public bool InsertItem(string Title, string Body) { //This example inserts a new item into the Announcements list //For your own lists, first add a Service Reference to the //SharePoint site you want to query, then change the item object below. //Tell the user what's going on Console.WriteLine("Received a InsertItem call. About to query SharePoint..."); //Formulate the URL to the List Data RESTful service. //You must correct this path to point to your own SharePoint farm string Url = "http://intranet.contoso.com/_vti_bin/listdata.svc"; //Create a dataContext object for the service TeamSiteDataContext dataContext = new TeamSiteDataContext(new Uri(Url)); //Authenticate as administrator. NetworkCredential myCredential = new NetworkCredential("Administrator", "pass@word1"); dataContext.Credentials = myCredential; //Create a new item AnnouncementsItem newAnnouncement = new AnnouncementsItem(); newAnnouncement.Title = Title; newAnnouncement.Body = Body; try { //Add it to the Announcements list dataContext.AddToAnnouncements(newAnnouncement); dataContext.SaveChanges(); Console.WriteLine("Added this new item: {0}", Title); //Return the dictionary return(true); } catch (Exception e) { Console.WriteLine("An error occurred: {0}", e.Message); return(false); } }