コード例 #1
0
        /*
         * The DeleteEvent method uses the Model classes to interact with data in the database.
         * It also uses the SharePoint client-side object model to delete the corresponding calendar item
         */
        public ActionResult DeleteEvent(string EventID)
        {
            Models.EventData eData  = new Models.EventData();
            bool             result = eData.DeleteEvent(EventID);
            var spContext           = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    hostWeb = clientContext.Web;
                    clientContext.Load(hostWeb);
                    ListCollection hostLists = hostWeb.Lists;
                    clientContext.Load(hostLists);
                    clientContext.ExecuteQuery();
                    bool calendarFound = false;
                    foreach (List lst in hostLists)
                    {
                        if (lst.BaseTemplate == (int)ListTemplateType.Events)
                        {
                            if (lst.Title == "Contoso Events Calendar")
                            {
                                calendarFound = true;
                                break;
                            }
                        }
                    }
                    eVentCalendarExists = calendarFound;
                    if (calendarFound)
                    {
                        List calendar = hostWeb.Lists.GetByTitle("Contoso Events Calendar");

                        clientContext.Load(calendar);
                        clientContext.ExecuteQuery();
                        CamlQuery camlQuery = new CamlQuery();
                        camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Category' /><Value Type='Text'>" + EventID + "</Value></Eq></Where></Query></View>";
                        ListItemCollection eventItems = calendar.GetItems(camlQuery);
                        clientContext.Load(eventItems);
                        clientContext.ExecuteQuery();
                        foreach (ListItem eventToDelete in eventItems)
                        {
                            eventToDelete.DeleteObject();
                        }
                        clientContext.ExecuteQuery();
                    }
                }
                return(RedirectToAction("events", "Home", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
        }