Ejemplo n.º 1
0
        /// <summary>
        ///     Get the captions of all the open word documents
        /// </summary>
        /// <returns></returns>
        public static IEnumerable <OneNotePage> GetPages()
        {
            using (var oneNoteApplication = ComWrapper.GetOrCreateInstance <IOneNoteApplication>())
            {
                if (oneNoteApplication == null)
                {
                    yield break;
                }

                // ReSharper disable once RedundantAssignment
                var notebookXml = "";
                oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010);
                if (string.IsNullOrEmpty(notebookXml))
                {
                    yield break;
                }

                using (var reader = new StringReader(notebookXml))
                    using (var xmlReader = new XmlTextReader(reader))
                    {
                        OneNoteSection  currentSection  = null;
                        OneNoteNotebook currentNotebook = null;
                        while (xmlReader.Read())
                        {
                            switch (xmlReader.Name)
                            {
                            case "one:Notebook":
                            {
                                var id = xmlReader.GetAttribute("ID");
                                if (id != null && (currentNotebook == null || !id.Equals(currentNotebook.ID)))
                                {
                                    currentNotebook = new OneNoteNotebook
                                    {
                                        ID   = xmlReader.GetAttribute("ID"),
                                        Name = xmlReader.GetAttribute("name")
                                    };
                                }

                                break;
                            }

                            case "one:Section":
                            {
                                var id = xmlReader.GetAttribute("ID");
                                if (id != null && (currentSection == null || !id.Equals(currentSection.ID)))
                                {
                                    currentSection = new OneNoteSection
                                    {
                                        ID     = xmlReader.GetAttribute("ID"),
                                        Name   = xmlReader.GetAttribute("name"),
                                        Parent = currentNotebook
                                    };
                                }

                                break;
                            }

                            case "one:Page":
                                // Skip deleted items
                                if ("true".Equals(xmlReader.GetAttribute("isInRecycleBin")))
                                {
                                    continue;
                                }
                                var page = new OneNotePage
                                {
                                    Parent = currentSection,
                                    Name   = xmlReader.GetAttribute("name"),
                                    ID     = xmlReader.GetAttribute("ID")
                                };
                                if (page.ID == null || page.Name == null)
                                {
                                    continue;
                                }
                                page.IsCurrentlyViewed = "true".Equals(xmlReader.GetAttribute("isCurrentlyViewed"));
                                yield return(page);

                                break;
                            }
                        }
                    }
            }
        }
        public async Task <ActionResult> Process(string code)
        {
            OneNoteViewModel model  = new OneNoteViewModel();
            StringBuilder    status = new StringBuilder();
            Tuple <string, OneNoteNotebook> notebookApiResult = null;
            Tuple <string, OneNoteSection>  sectionApiResult  = null;

            if (code != null)
            {
                LiveLoginResult loginStatus = await liveAuthClient.ExchangeAuthCodeAsync(this.HttpContext);

                // get the selected project
                string projectId            = (string)this.HttpContext.Session["onenote-project"];
                ProjectDetailViewModel data = await GetProjectAndReferencesFromSharePoint(projectId);

                OneNoteRepository repository = new OneNoteRepository(loginStatus.Session);

                status.Append("GetNotebook: ");
                notebookApiResult = await repository.GetNotebook("name eq 'Project Research Tracker'");

                status.Append(notebookApiResult.Item1 + "<br/>");
                OneNoteNotebook notebook = notebookApiResult.Item2;

                if (notebook == null)
                {
                    status.Append("CreateNotebook: ");
                    notebookApiResult = await repository.CreateNotebook();

                    status.Append(notebookApiResult.Item1 + "<br/>");
                    notebook = notebookApiResult.Item2;
                }

                if (notebook != null)
                {
                    model.NewNotebookLink = notebook.links.oneNoteWebUrl.href;

                    string filter = String.Format("name eq '{0}'", data.Project.Title);
                    status.Append("GetSection: ");
                    sectionApiResult = await repository.GetNotebookSection(notebook, filter);

                    status.Append(sectionApiResult.Item1 + "<br/>");
                    OneNoteSection section = sectionApiResult.Item2;

                    if (section == null)
                    {
                        status.Append("CreateSection: ");
                        sectionApiResult = await repository.CreateSection(notebook, data.Project.Title);

                        status.Append(sectionApiResult.Item1 + "<br/>");
                        section = sectionApiResult.Item2;
                    }

                    if (section != null)
                    {
                        foreach (Reference reference in data.References)
                        {
                            status.Append("CreatePage: ");
                            string result = await repository.CreatePageForReference(section.pagesUrl, reference);

                            status.Append(result + "<br/>");
                        }
                    }
                }

                model.ResponseMessage = status.ToString();
                return(View(model));
            }
            else
            {
                RedirectToAction("Index", "Home");
            }

            return(null);
        }