Return to the Panopto API index page
I made this to for https://helpdesk.panopto.com/entries/20823271-Bulk-movement-of-captures-to-another-folder so it uses some parameters from the Panopto GitHub.
Most of this code is the paginated session list on page 203. We’ll be using MoveSessions, which requires a list of session guids and a folder ID to move the sessions to.
We create our variables and make a List
1 2 3 4 |
bool lastPage = false; int resultsPerPage = 50; int page = 0; List<Guid> sessionGuids = new List<Guid>(); |
[Edit: 13/06/2014: This code was in the full code below, but it is important enough to mention]
We need to specify the Guid of the FolderID we want to pull sessions from. Without this code you will move every video from every folder.
1 2 |
PanoptoSessionManagement.ListSessionsRequest request = new PanoptoSessionManagement.ListSessionsRequest { Pagination = pagination, FolderId = oldFolder}; ListSessionsResponse response = sessionMgr.GetSessionsList(sessionAuthInfo, request, null); |
And while we loop we add the Guids to the list
1 2 3 4 5 6 7 8 |
if (response.Results.Length > 0) { foreach (Session session in response.Results) { sessionGuids.Add(session.Id); Console.WriteLine("Moving: {0}", session.Name); } } |
Convert the list to an array
1 |
Guid[] guidArray = sessionGuids.ToArray(); |
Then call the command in a single line
1 |
sessionMgr.MoveSessions(sessionAuthInfo, guidArray, newFolder); |
Full code below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Net; using System.Security.Cryptography.X509Certificates; using ConsolePanoptoAPI.PanoptoSessionManagement; // ANY NEW NAMESPACES SHOULD BE ADDED HERE // END OF NAMESPACES namespace ConsolePanoptoAPI { class Program { static bool hasBeenInitialized = false; // Command line args static string user; static string password; static Guid oldFolder; static Guid newFolder; const int numArguments = 4; /// <summary> /// Print command line usage /// </summary> static void Usage() { Console.WriteLine("MoveSessionsToANewFolder -u [user] -p [password] -o [oldfolderid] -n [newfolderid]"); } /// <summary> /// Parse command line arguments /// </summary> static bool ParseArgs(string[] args) { bool done = false; if (args.Length == (numArguments * 2)) { for (int i = 0; i < args.Length && !done; i += 2) { if (args[i].Length != 2 || args[i][0] != '-' || i + 1 >= args.Length) { break; } switch (args[i][1]) { case 'u': user = args[i + 1]; break; case 'p': password = args[i + 1]; break; case 'o': oldFolder = new Guid(args[i + 1]); break; case 'n': newFolder = new Guid(args[i + 1]);; break; default: done = true; break; } } } return user != null && password != null && oldFolder != null && newFolder != null; } static void Assert(bool condition, string errorMsg) { if (!condition) { throw new Exception(errorMsg); } } static void Main(string[] args) { EnsureCertificateValidation(); // START WRITING CODE HERE try { // Parse args if (!ParseArgs(args)) { Usage(); return; } // Instantiate service clients ISessionManagement sessionMgr = new SessionManagementClient(); // Create auth info. For this sample, we will be passing auth info into all of the PublicAPI web service calls // instead of using IAuth.LogonWithPassword() (which is the alternative method). PanoptoSessionManagement.AuthenticationInfo sessionAuthInfo = new PanoptoSessionManagement.AuthenticationInfo() { UserKey = user, Password = password }; bool lastPage = false; int resultsPerPage = 50; int page = 0; List<Guid> sessionGuids = new List<Guid>(); while (!lastPage) { PanoptoSessionManagement.Pagination pagination = new PanoptoSessionManagement.Pagination { MaxNumberResults = resultsPerPage, PageNumber = page }; PanoptoSessionManagement.ListSessionsRequest request = new PanoptoSessionManagement.ListSessionsRequest { Pagination = pagination, FolderId = oldFolder}; ListSessionsResponse response = sessionMgr.GetSessionsList(sessionAuthInfo, request, null); if (resultsPerPage * (page + 1) >= response.TotalNumberResults) { lastPage = true; } if (response.Results.Length > 0) { foreach (Session session in response.Results) { sessionGuids.Add(session.Id); Console.WriteLine("Moving: {0}", session.Name); } } else { Console.WriteLine("No sessions found"); } page++; } Guid[] guidArray = sessionGuids.ToArray(); // instance method sessionMgr.MoveSessions(sessionAuthInfo, guidArray, newFolder); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } /// /// Ensures that our custom certificate validation has been applied /// public static void EnsureCertificateValidation() { if (!hasBeenInitialized) { ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(CustomCertificateValidation); hasBeenInitialized = true; } } /// /// Ensures that server certificate is authenticated /// private static bool CustomCertificateValidation(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error) { return true; } /// /// Creates an auth code. Used when we want to authenticate a user, but don't know their password. /// ///The instance name as set in Panopto > System > Identity Providors ///Username as defined by Panopto ///The full server name as defined by Panopto > System > Settings > General site settings > Web server FQDN ///The key produced through Panopto > System > Identity Providors /// private static string CreateAuthCode(string identityProviderInstanceName, string username, string serverFqdn, string applicationKey) { string payload = identityProviderInstanceName + @"\" + username + "@" + serverFqdn.ToLower() + "|" + applicationKey.ToLower(); var data = Encoding.ASCII.GetBytes(payload); var hashData = new System.Security.Cryptography.SHA1Managed().ComputeHash(data); var hash = string.Empty; foreach (var b in hashData) hash += b.ToString("X2"); return hash; } } } |
Return to the Panopto API index page
After accidentally moving every video on my dev server into a folder, I’ve added a code snippet that specifically mentions how to search only in one folder.
The full code hasn’t changed and was correct.