Ejemplo n.º 1
0
        public async Task <ActionResult> ListMyFiles()
        {
            HomeViewModel model = new HomeViewModel();

            var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
            AuthenticationHelper authenticationHelper = new AuthenticationHelper();

            authenticationHelper.EnsureAuthenticationContext(new ADALTokenCache(signInUserId));

            MyFilesHelper myFilesHelper = new MyFilesHelper(authenticationHelper);
            var           myFiles       = await myFilesHelper.GetMyFiles();

            model.Office365ActionResult = String.Format("Found {0} my files! Showing first 10, if any.", myFiles.Count());

            foreach (var item in myFiles.Take(10))
            {
                model.Items.Add(String.Format(
                                    "URL: {0}",
                                    !String.IsNullOrEmpty(item.WebUrl) ? item.WebUrl : String.Empty));
            }

            return(View("UseOffice365API", model));
        }
Ejemplo n.º 2
0
        protected async void ListFilesCommand_Click(object sender, EventArgs e)
        {
            var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
            AuthenticationHelper authenticationHelper = new AuthenticationHelper();

            authenticationHelper.EnsureAuthenticationContext(new ADALTokenCache(signInUserId));

            MyFilesHelper myFilesHelper = new MyFilesHelper(authenticationHelper);
            var           myFiles       = await myFilesHelper.GetMyFiles();

            List <String> results = new List <String>();

            commandResult.Text = String.Format("Found {0} my files! Showing first 10, if any.", myFiles.Count());

            foreach (var item in myFiles.Take(10))
            {
                results.Add(String.Format(
                                "URL: {0}",
                                !String.IsNullOrEmpty(item.WebUrl) ? item.WebUrl : String.Empty));
            }

            resultsList.DataSource = results;
            resultsList.DataBind();
        }
Ejemplo n.º 3
0
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            Uri authorityUri;
            Uri sharePointTenantUri;
            Uri siteCollectionUri;

            Regex mailRegex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");

            // Input parameters sanity check
            if ((String.IsNullOrEmpty(this.Authority.Text) || !Uri.TryCreate(this.Authority.Text, UriKind.Absolute, out authorityUri)) ||
                (String.IsNullOrEmpty(this.SharePointTenantUri.Text) || !Uri.TryCreate(this.SharePointTenantUri.Text, UriKind.Absolute, out sharePointTenantUri)) ||
                (String.IsNullOrEmpty(this.SiteCollectionUri.Text) || !Uri.TryCreate(this.SiteCollectionUri.Text, UriKind.Absolute, out siteCollectionUri)) ||
                (String.IsNullOrEmpty(this.MailAddressTo.Text) || !mailRegex.IsMatch(this.MailAddressTo.Text)) ||
                (String.IsNullOrEmpty(this.FileToUploadPath.Text) || !File.Exists(this.FileToUploadPath.Text)))
            {
                MessageBoxResult msgBoxResult = MessageBox.Show("Please fill all the input parameters!");
                return;
            }

            try
            {
                PrintHeader("Authentication Phase");
                AuthenticationHelper authenticationHelper = new AuthenticationHelper();
                authenticationHelper.EnsureAuthenticationContext(this.Authority.Text);

                PrintHeader("Discovery API demo");
                DiscoveryHelper discoveryHelper = new DiscoveryHelper(authenticationHelper);
                var             t = await discoveryHelper.DiscoverMyFiles();

                PrintSubHeader("Current user information");
                PrintAttribute("OneDrive URL", t.ServiceEndpointUri);

                var t3 = await discoveryHelper.DiscoverMail();

                PrintAttribute("Mail URL", t3.ServiceEndpointUri);

                PrintHeader("Files API demo");
                // Read all files on your onedrive
                PrintSubHeader("List TOP 20 files and folders in the OneDrive");

                MyFilesHelper myFilesHelper = new MyFilesHelper(authenticationHelper);
                var           allMyFolders  = await myFilesHelper.GetMyFolders();

                var allMyFiles = await myFilesHelper.GetMyFiles();

                foreach (var item in allMyFiles.Take(20))
                {
                    PrintAttribute("URL", item.WebUrl);
                }

                // Upload a file to the "Shared with everyone" folder
                PrintSubHeader("Upload a file to OneDrive");
                if (allMyFolders.Any())
                {
                    await myFilesHelper.UploadFile(this.FileToUploadPath.Text, allMyFolders.First().Id);
                }
                else
                {
                    await myFilesHelper.UploadFile(this.FileToUploadPath.Text);
                }
                // Shared with everyone

                // Iterate over the "Shared with everyone" folder
                PrintSubHeader("List all files and folders in the Shared with everyone folder");
                var myFiles = await myFilesHelper.GetMyFiles(allMyFolders.First().Id);

                foreach (var item in myFiles)
                {
                    PrintAttribute("URL", item.WebUrl);
                }

                PrintHeader("Mail API demo");

                //Get mails
                PrintSubHeader("Retrieve mails from INBOX");
                MailHelper mailHelper = new MailHelper(authenticationHelper);
                var        mails      = await mailHelper.GetMessages();

                PrintSubHeader(String.Format("Printing TOP 10 mails of {0}", mails.Count()));
                foreach (var item in mails.Take(10))
                {
                    PrintAttribute("From ", String.Format("{0} / {1}", item.From != null ? item.From.EmailAddress.Address : "", item.Subject));
                }

                //Send mail
                PrintSubHeader("Send a mail");
                await mailHelper.SendMail(this.MailAddressTo.Text, "Let's Hack-A-Thon - Office365Api.Demo", "This will be <B>fun...</B>");

                //Create message in drafts folder
                PrintSubHeader("Store a mail in the drafts folder");
                await mailHelper.DraftMail(this.MailAddressTo.Text, "Let's Hack-A-Thon - Office365Api.Demo", "This will be fun (in draft folder)...");

                PrintHeader("Active Directory API demo");
                ActiveDirectoryHelper activeDirectoryHelper = new ActiveDirectoryHelper(authenticationHelper);
                var allADUsers = await activeDirectoryHelper.GetUsers();

                PrintSubHeader(String.Format("Printing TOP 10 users of {0}", allADUsers.Count()));
                foreach (var user in allADUsers.Take(10))
                {
                    PrintAttribute("User", user.UserPrincipalName);
                }

                PrintHeader("All done...");
            }
            catch (Exception ex)
            {
                string message = "";
                if (ex is AggregateException)
                {
                    message = ex.InnerException.Message;
                }
                else
                {
                    message = ex.Message;
                }

                PrintException(message);
            }
        }