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);
                }
            }
示例#2
0
            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);
                }
            }