Ejemplo n.º 1
0
        public static string CreateMsTeamsChannelFolder(string aadAccessToken, string teamId, string channelName)
        {
            Tuple <string, string> fileExists = Utils.FileAttachments.CheckIfFileExistsOnTeamsChannel(aadAccessToken, teamId, "/" + channelName);

            if (fileExists.Item1 != "")
            {
                Console.WriteLine("Channel folder exists " + fileExists);
                return(fileExists.Item1);
            }

            var authHelper = new O365.AuthenticationHelper()
            {
                AccessToken = aadAccessToken
            };

            Microsoft.Graph.GraphServiceClient gcs = new Microsoft.Graph.GraphServiceClient(authHelper);

            Microsoft.Graph.DriveItem driveItem = new Microsoft.Graph.DriveItem();
            driveItem.Name = channelName;
            var folder = new Microsoft.Graph.Folder();

            driveItem.Folder = folder;

            try
            {
                var result = gcs.Groups[teamId].Drive.Root.Children.Request().AddAsync(driveItem).Result;
                Console.WriteLine("Folder ID is " + result.Id + " with path " + result.WebUrl);
                return(result.Id);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Folder creation failure: " + ex.InnerException);
                return("");
            }
        }
Ejemplo n.º 2
0
        public static Tuple <string, string> CheckIfFileExistsOnTeamsChannel(string aadAccessToken, string selectedTeamId, string pathToItem)
        {
            var authHelper = new Utils.O365.AuthenticationHelper()
            {
                AccessToken = aadAccessToken
            };

            Microsoft.Graph.GraphServiceClient gcs = new Microsoft.Graph.GraphServiceClient(authHelper);

            Microsoft.Graph.DriveItem fileExistsResult = null;
            try
            {
                fileExistsResult = gcs.Groups[selectedTeamId].Drive.Root.ItemWithPath(pathToItem).
                                   Request().GetAsync().Result;
            }
            catch
            {
                fileExistsResult = null;
            }

            if (fileExistsResult == null)
            {
                return(new Tuple <string, string>("", ""));
            }
            Console.WriteLine("Attachment already exists.  We won't replace it. " + pathToItem);
            return(new Tuple <string, string>(fileExistsResult.Id, fileExistsResult.WebUrl));
        }
Ejemplo n.º 3
0
        private static async Task ProcessDriveItem(GraphService graphClient, Microsoft.Graph.DriveItem item)
        {
            if (item.File != null)
            {
                // Query the database
                var teams = await DatabaseHelper.GetFlightTeamsAsync(f => f.SharePointListItemId.Equals(item.Id));

                var team = teams.FirstOrDefault();

                if (item.Deleted != null && team != null)
                {
                    // Remove the team
                    await TeamProvisioning.ArchiveTeamAsync(team);

                    // Remove the database item
                    await DatabaseHelper.DeleteFlightTeamAsync(team.Id);

                    return;
                }

                // Get the file's list data
                var listItem = await graphClient.GetDriveItemListItem(item.ParentReference.DriveId, item.Id);

                if (listItem == null)
                {
                    return;
                }

                if (team == null)
                {
                    team = FlightTeam.FromListItem(item.Id, listItem);
                    if (team == null)
                    {
                        // Item was added to list but required metadata
                        // isn't filled in yet. No-op.
                        return;
                    }

                    // New item, provision team
                    team.TeamId = await TeamProvisioning.ProvisionTeamAsync(team);

                    await DatabaseHelper.CreateFlightTeamAsync(team);
                }
                else
                {
                    var updatedTeam = FlightTeam.FromListItem(item.Id, listItem);
                    updatedTeam.TeamId = team.TeamId;

                    // Existing item, process changes
                    await TeamProvisioning.UpdateTeamAsync(team, updatedTeam);

                    updatedTeam.Id = team.Id;
                    await DatabaseHelper.UpdateFlightTeamAsync(team.Id, updatedTeam);
                }
            }
        }
        public DriveItem(Microsoft.Graph.DriveItem graphItem)
        {
            var fileParts = graphItem.Name.Split(".");

            if (fileParts.Length > 1)
            {
                Title    = fileParts[0];
                FileType = fileParts[1];
            }
            else
            {
                Title = graphItem.Name;
                if (graphItem.Folder != null)
                {
                    FileType = "folder";
                }
                if (graphItem.Package != null)
                {
                    FileType = "onetoc";
                }
            }
            WebUrl = graphItem.WebUrl;
        }
Ejemplo n.º 5
0
        public static async Task <Tuple <string, string> > UploadFileToTeamsChannel(string aadAccessToken, string selectedTeamId, string filePath, string pathToItem)
        {
            var authHelper = new Utils.O365.AuthenticationHelper()
            {
                AccessToken = aadAccessToken
            };

            Microsoft.Graph.GraphServiceClient gcs = new Microsoft.Graph.GraphServiceClient(authHelper);

            var fileExists = CheckIfFileExistsOnTeamsChannel(aadAccessToken, selectedTeamId, pathToItem);

            if (fileExists.Item1 != "")
            {
                return(new Tuple <string, string>(fileExists.Item1, fileExists.Item2));
            }

            Microsoft.Graph.UploadSession uploadSession = null;

            uploadSession = await gcs.Groups[selectedTeamId].Drive.Root.ItemWithPath(pathToItem).
                            CreateUploadSession().Request().PostAsync();

            try
            {
                Console.WriteLine("Trying to upload file to MS Teams SPo Folder " + filePath);
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    //upload a file in a single shot.  this is great if all files are below the allowed maximum size for a single shot upload.
                    //however, we're not going to be clever and chunk all files.
                    //{
                    //var result = await gcs.Groups[selectedTeamId].Drive.Root.ItemWithPath("/" + channelName + "/channelsurf/fileattachments/" + fileId).
                    //Content.Request().PutAsync<Microsoft.Graph.DriveItem>(fs);
                    //}

                    // don't be clever: assume you have to chunk all files, even those below the single shot maximum
                    // credit to https://stackoverflow.com/questions/43974320/maximum-request-length-exceeded-when-uploading-a-file-to-onedrive/43983895

                    var maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default.

                    var chunkedUploadProvider = new Microsoft.Graph.ChunkedUploadProvider(uploadSession, gcs, fs, maxChunkSize);

                    var chunkRequests     = chunkedUploadProvider.GetUploadChunkRequests();
                    var readBuffer        = new byte[maxChunkSize];
                    var trackedExceptions = new List <Exception>();

                    Microsoft.Graph.DriveItem itemResult = null;
                    //upload the chunks
                    foreach (var request in chunkRequests)
                    {
                        var result = await chunkedUploadProvider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions);

                        if (result.UploadSucceeded)
                        {
                            itemResult = result.ItemResponse;
                        }
                    }
                    Console.WriteLine("Upload of attachment to MS Teams completed " + pathToItem);
                    Console.WriteLine("SPo ID is " + itemResult.Id);
                    return(new Tuple <string, string>(itemResult.Id, itemResult.WebUrl));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: attachment could not be uploaded" + ex.InnerException);
            }

            return(new Tuple <string, string>("", ""));
        }